Преглед изворни кода

改变穷举进程模式,多策略但完整stock_list运算

daniel пре 2 година
родитељ
комит
a1869e0213
1 измењених фајлова са 49 додато и 42 уклоњено
  1. 49 42
      futures_backtrader.py

+ 49 - 42
futures_backtrader.py

@@ -9,6 +9,7 @@ import math
 import threading
 from datetime import datetime as dt
 import multiprocessing as mp
+from multiprocessing import Manager
 from backtrader.feeds import PandasData
 import os
 # import multiprocessing
@@ -131,8 +132,14 @@ class TestStrategy(bt.Strategy):
 
 
 
-def backtrader(table_list, result, result_change,result_change_fall, num, Volatility, rate, err_list):
+def backtrader(table_list, num, Volatility, rate, ns):
+    stattime = dt.now()
+    print(type(ns.df), ns.df)
     engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks_data?charset=utf8')
+    result = mp.Manager().list()
+    result_change = mp.Manager().list()
+    result_change_fall = mp.Manager().list()
+    err_list = mp.Manager().list()
     for stock in table_list:
         # print(stock)
         stk_df = pd.read_sql_table(stock, engine)
@@ -186,7 +193,26 @@ def backtrader(table_list, result, result_change,result_change_fall, num, Volati
                 else:
                     result_change_fall.append((1-cerebro.broker.getvalue() / 10000))
                     # print('aaaaaaaaaaa')
-
+    print(f'以{num}内最低值波动{Volatility}为支撑、{rate}%为乖离率,结果状态为:')
+    print('正盈利的个股为:', len(result_change), '成功率为:', len(result)/len(table_list))
+    print(f'总盈利:{np.sum(result_change)} 平均盈利:{np.mean(result_change)},最大盈利:{np.max(result_change)}, 最小盈利:{np.min(result_change)}')
+    print(
+        f'总亏损:{np.sum(result_change_fall)},平均亏损:{np.mean(result_change_fall)},最大亏损:{np.min(result_change_fall)} 最小亏损:{np.max(result_change_fall)}')
+    endtime = dt.now()
+    # ns.df.loc[len(ns.df)] = [num, Volatility, rate, len(result), len(result)/len(table_list), np.sum(result_change),
+    #                                 np.mean(result_change), np.max(result_change), np.min(result_change),
+    #                                 np.sum(result_change_fall), np.mean(result_change_fall),
+    #                                 np.min(result_change_fall), np.max(result_change_fall)]
+    a = pd.DataFrame(data=[[num, Volatility, rate, len(result), len(result)/len(table_list), np.sum(result_change),
+                                    np.mean(result_change), np.max(result_change), np.min(result_change),
+                                    np.sum(result_change_fall), np.mean(result_change_fall),
+                                    np.min(result_change_fall), np.max(result_change_fall)]],
+                     columns=['周期', '波动率', '乖离率', '盈利个数', '盈利比例', '总盈利', '平均盈利', '最大盈利', '最小盈利', '总亏损',
+                               '平均亏损', '最大亏损', '最小亏损'])
+    ns.df = pd.concat([ns.df, a], ignore_index=True)
+    # ns.df = ns.df.append(a)
+    print(ns.df)
+    print('每轮耗时:', endtime-stattime)
 
             # cerebro.plot()
 
@@ -211,55 +237,36 @@ if __name__ == '__main__':
     cursor.execute("show tables like '%%%s%%' " % fre)
     table_list = [tuple[0] for tuple in cursor.fetchall()]
     # print(table_list)
-    # table_list = table_list[0:500]
+    table_list = table_list[0:100]
 
+    mgr = Manager()
+    ns = mgr.Namespace()
     df = pd.DataFrame(columns=['周期', '波动率', '乖离率', '盈利个数', '盈利比例', '总盈利', '平均盈利', '最大盈利', '最小盈利', '总亏损',
                                '平均亏损', '最大亏损', '最小亏损'])
-
-
+    ns.df = df
     sttime = dt.now()
-
-    for num in range(20, 180, 10):
-        for Volatility in range(10, 20, 1):
-            for rate in range(5, 20, 1):
+    thread_list = []
+    for num in range(20, 40, 10):
+        for Volatility in range(10, 12, 1):
+            for rate in range(5, 7, 1):
                 #获得cpu数量,计算进程数
-                step = math.ceil(len(table_list)/cpu_count/100)*100
-                thread_list = []
-                result = mp.Manager().list()
-                result_change = mp.Manager().list()
-                result_change_fall = mp.Manager().list()
-                err_list = mp.Manager().list()
+                # step = math.ceil(len(table_list)/cpu_count/100)*100
                 print(f'{num}天波动率为{Volatility}% 乖离率为{rate}%')
-                for i in range(0, len(table_list), step):
-
-
-                    stattime = dt.now()
+                # for i in range(0, len(table_list), step):
                     # thd = threading.local()
                     # print(i)
-                    p = mp.Process(target=backtrader, args=(table_list[i:i + step], result,
-                                                            result_change, result_change_fall, num, Volatility, rate,
-                                                            err_list))
-                    thread_list.append(p)
+                p = mp.Process(target=backtrader, args=(table_list,  num, Volatility, rate, ns))
+                # p.start()
+                thread_list.append(p)
                     # p.start()
                     # p.join()
-                    # print(thread_list)
-                for thread in thread_list:
-                    thread.start()
-                for thread in thread_list:
-                    thread.join()
-
-                print(f'以{num}内最低值波动{Volatility}为支撑、{rate}%为乖离率,结果状态为:')
-                print('正盈利的个股为:', len(result_change), '成功率为:', len(result)/len(table_list))
-                print(f'总盈利:{np.sum(result_change)} 平均盈利:{np.mean(result_change)},最大盈利:{np.max(result_change)}, 最小盈利:{np.min(result_change)}')
-                print(
-                    f'总亏损:{np.sum(result_change_fall)},平均亏损:{np.mean(result_change_fall)},最大亏损:{np.min(result_change_fall)} 最小亏损:{np.max(result_change_fall)}')
-                endtime = dt.now()
-                df.loc[len(df)] = [num, Volatility, rate, len(result), len(result)/len(table_list), np.sum(result_change),
-                                                np.mean(result_change), np.max(result_change), np.min(result_change),
-                                                np.sum(result_change_fall), np.mean(result_change_fall),
-                                                np.min(result_change_fall), np.max(result_change_fall)]
-                print(df)
-                print('每轮耗时:', endtime-stattime)
+            print(len(thread_list))
+    print('开始轮循!')
+    for thread in thread_list:
+        thread.start()
+    for thread in thread_list:
+        thread.join()
+    print('NS.DF:', '\n', ns.df)
     edtime = dt.now()
     print('总耗时:', edtime - sttime)
-    df.to_csv(path+dt.now().strftime('%Y-%m-%d')+'.csv', index=True)
+    ns.df.to_csv(path+dt.now().strftime('%Y-%m-%d')+'.csv', index=True,encoding='utf_8_sig')