Browse Source

重要修订!!
positions_list改为字典 获得stock_code 及volume

Daniel 2 years ago
parent
commit
b61479076a
2 changed files with 28 additions and 25 deletions
  1. 8 8
      QMT/download_data.py
  2. 20 17
      QMT/real_time.py

+ 8 - 8
QMT/download_data.py

@@ -61,12 +61,12 @@ if __name__ == '__main__':
     eng_b = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qmt_stocks?charset=utf8')
     eng_f = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qmt_stocks_front?charset=utf8')
 
-    # download_data(stocks, eng_b, eng_f)
+    download_data(stocks, eng_b, eng_f)
 
-    scheduler = BlockingScheduler()
-    scheduler.add_job(func=download_data, trigger='cron', hour='15', minute='45', args=[stocks, eng_b, eng_f],
-                      timezone="Asia/Shanghai")
-    try:
-        scheduler.start()
-    except (KeyboardInterrupt, SystemExit):
-        pass
+    # scheduler = BlockingScheduler()
+    # scheduler.add_job(func=download_data, trigger='cron', hour='15', minute='45', args=[stocks, eng_b, eng_f],
+    #                   timezone="Asia/Shanghai")
+    # try:
+    #     scheduler.start()
+    # except (KeyboardInterrupt, SystemExit):
+    #     pass

+ 20 - 17
QMT/real_time.py

@@ -84,26 +84,26 @@ def ma_judge(data, stock_list, results):
     print('RRRRRRR,', results)
 
 
-def sell_trader(data, positions_list):
+def sell_trader(data, positions_dict):
     # for m in data:
     #     print(m, data[m]['lastPrice'])
     print('卖出函数:', dt.now())
     # positions = xt_trader.query_stock_positions(acc)
     # print('持仓总数:', len(positions_list))
-    for stock in data:
-        if stock in positions_list:
-            print('持仓', stock, data[stock])
-            current_price = data[stock]['lastPrice']
-            open_price = data[stock]['open']
-            MA5 = ma(stock, 5, data)
-            MA5_1 = ma_1(stock, 5)
-            print('价格:', current_price, open_price, MA5, MA5_1)
-            if current_price < MA5 or MA5 < MA5_1 or current_price > MA5 * 1.07:
-                print('卖出信号!!!!!!', stock, current_price)
 
-            #     order_id = xt_trader.order_stock(acc, stock, xtconstant.STOCK_SELL,
-            #                                     i.volume, xtconstant.LATEST_PRICE, 0, 'strategy1', 'order_test')
-            # print(order_id, i)
+    for stock, volume in positions_dict.items():
+        print('持仓', stock, volume)
+        current_price = data[stock]['lastPrice']
+        open_price = data[stock]['open']
+        MA5 = ma(stock, 5, data)
+        MA5_1 = ma_1(stock, 5)
+        print('价格:', current_price, open_price, MA5, MA5_1)
+        if current_price < MA5 or MA5 < MA5_1 or current_price > MA5 * 1.07:
+            print('卖出信号!!!!!!', stock, current_price)
+
+            order_id = xt_trader.order_stock(acc, stock, xtconstant.STOCK_SELL, volume,
+                                             xtconstant.LATEST_PRICE, 0, 'strategy1', 'order_test')
+        print(order_id, stock, volume)
 
 
 def buy_trader(data):
@@ -203,10 +203,13 @@ def trader(data):
     print(type(positions))
     print('持仓数量', len(positions))
     if len(positions) != 0:
-        positions_list = [positions[x].stock_code for x in range(0, len(positions))]
-        print(positions_list)
+        positions_dict = {positions[x].stock_code:positions[x].volume for x in range(0, len(positions))}
+
+        print(positions_dict)
+
+
 
-        sell_trader(data, positions_list)
+        sell_trader(data, positions_dict)
 
     # 买入条件
     buy_trader(data)