waditu / czsc

缠中说禅技术分析工具;缠论;股票;期货;Quant;量化交易
Other
2.88k stars 977 forks source link

ValueError Bugfix #67

Closed asterocean closed 2 years ago

asterocean commented 2 years ago
diff --git a/czsc/cobra/backtest.py b/czsc/cobra/backtest.py
index bc9a032..bc15a3d 100644
--- a/czsc/cobra/backtest.py
+++ b/czsc/cobra/backtest.py
@@ -67,8 +67,8 @@ def long_trade_estimator(pairs: List[dict]):
     """
     if not pairs:
         return {
-            '标的代码': pairs[0]['标的代码'],
-            '交易次数': len(pairs),
+            '标的代码': '', #pairs[0]['标的代码'],
+            '交易次数': 0, #len(pairs),
             '累计收益(%)': 0,
             '单笔收益(%)': 0,
             '平均持仓分钟': 0,
@@ -86,11 +86,11 @@ def long_trade_estimator(pairs: List[dict]):
         '平均持仓分钟': int(df['持仓分钟'].mean()),
         '累计收益(%)': x_round(df['盈亏(%)'].sum()),
         '单笔收益(%)': x_round(df['盈亏(%)'].mean()),
-        '胜率(%)': int(len(df[df['盈亏(%)'] > 0]) / len(df) * 10000) / 100,
-        '累计盈亏比': int(df[df['盈亏(%)'] > 0]['盈亏(%)'].sum() /
-                     abs(df[df['盈亏(%)'] < 0]['盈亏(%)'].sum()) * 100) / 100,
-        '单笔盈亏比': int(df[df['盈亏(%)'] > 0]['盈亏(%)'].mean() /
-                     abs(df[df['盈亏(%)'] < 0]['盈亏(%)'].mean()) * 100) / 100,
+        '胜率(%)': None if len(df)==0 else int(len(df[df['盈亏(%)'] > 0]) / len(df) * 10000) / 100,
+        '累计盈亏比': None if pd.isna(df[df['盈亏(%)'] > 0]['盈亏(%)'].sum()) or abs(df[df['盈亏(%)'] < 0]['盈亏(%)'].sum())==0 else int(df[df['盈亏(%)'] > 0]['盈亏(%)'].sum() /
+                                                                                                             abs(df[df['盈亏(%)'] < 0]['盈亏(%)'].sum()) * 100) / 100,
+        '单笔盈亏比': None if pd.isna(df[df['盈亏(%)'] > 0]['盈亏(%)'].mean()) or pd.isna(abs(df[df['盈亏(%)'] < 0]['盈亏(%)'].mean())) else int(df[df['盈亏(%)'] > 0]['盈亏(%)'].mean() /
+                                                                                                                   abs(df[df['盈亏(%)'] < 0]['盈亏(%)'].mean()) * 100) / 100 ,
     }

     res['持仓每分钟BP'] = round(res['累计收益(%)'] * 100 / df['持仓分钟'].sum(), 4)
liuts commented 2 years ago

建议直接提个request