Browse Source

修正stock_pool步长

daniel-ali 2 years ago
parent
commit
c9c7a8f7a8
1 changed files with 14 additions and 10 deletions
  1. 14 10
      QMT/real_time.py

+ 14 - 10
QMT/real_time.py

@@ -11,6 +11,7 @@ from jqdatasdk import *
 import pymysql
 import multiprocessing as mp
 import math
+import psutil
 
 auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
 db_pool = pymysql.connect(host='localhost',
@@ -70,9 +71,9 @@ def his_vol(stock, num):
 
 
 def ma_judge(data, stock_list, results):
-    print(f'这个ma_judge的PID为:{os.getpid()},收到的data数据为:{len(data.keys())}')
+    print(f'这个ma_judge的PID为:{os.getpid()},收到的data数据为:{len(data.keys())},stock_pool长度为{len(stock_list)},now is {dt.now()}')
     list_judge = list(set(data.keys()) & set(stock_list))
-    print(len(list_judge), list_judge)
+    print(f'本轮计算:{len(list_judge)}个股')
     for stock in list_judge:
         i = stock.replace('XSHG', 'SH').replace('XSHE', 'SZ')
         current_price, open_price = data[i]['lastPrice'], data[i]['open']
@@ -109,7 +110,7 @@ def sell_trader(data, positions_dict):
 
 
 def buy_trader(data, positions):
-    print('买入函数:', dt.now())
+    print('买入函数:', dt.now(), f'接受到{len(data.keys())}个个股')
     results = mp.Manager().list()
     mp_list = []
     engine_hlfx_pool = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8')
@@ -136,10 +137,9 @@ def buy_trader(data, positions):
                     print('append')
     '''
 
-    step = math.ceil(len(stock_pool) / mp.cpu_count())
+    step = math.ceil(len(stock_pool) / (mp.cpu_count()/2))
     print('step:', step)
-    print('cpu_count =', mp.cpu_count())
-    for i in range(0, len(stock_pool), math.ceil(len(stock_pool) / mp.cpu_count())):
+    for i in range(0, len(stock_pool), step):
         p = mp.Process(target=ma_judge, args=(data, stock_pool[i:i + step], results))
         mp_list.append(p)
         p.start()
@@ -180,18 +180,18 @@ def buy_trader(data, positions):
 
         keep_stocks = results_industry.split(",")
         new_keep_stock = [stock.replace('XSHG', 'SH').replace('XSHE', 'SZ') for stock in keep_stocks]
-        print(new_keep_stock)
+        print(f'new_keep_stock is:{new_keep_stock}')
 
-        for stock in data:
+        for stock in new_keep_stock:
             asset = xt_trader.query_stock_asset(acc)
             cash = asset.cash
-            print(cash)
             if cash > 2000 and len(positions) < 12:
                 if stock in new_keep_stock:
                     current_price = data[stock]['lastPrice']
                     volume = int((cash / 2 / current_price) // 100 * 100)
                     print('买入信号!!!!!!', stock, volume, current_price)
-                    order_id = xt_trader.order_stock(acc, stock, xtconstant.STOCK_BUY, volume, xtconstant.LATEST_PRICE, current_price, 'strategy1', 'order_test')
+                    order_id = xt_trader.order_stock(acc, stock, xtconstant.STOCK_BUY, volume, xtconstant.LATEST_PRICE,
+                                                     current_price, 'strategy1', 'order_test')
                     print(order_id)
     engine_hlfx_pool.dispose()
     print('一轮结束了,现在时间是:', dt.now())
@@ -280,6 +280,10 @@ if __name__ == '__main__':
     auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
 
     mp.freeze_support()
+    print('cpu_count =', mp.cpu_count())
+    pus = psutil.Process()
+    pus.cpu_affinity([4, 5, 6, 7])
+
     print("start")
     # 指定客户端所在路径
     path = r'c:\\qmt\\userdata_mini'