123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #coding:utf-8
- import time, datetime, traceback, sys
- from xtquant import xtdata
- from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
- from xtquant.xttype import StockAccount
- from xtquant import xtconstant
- from sqlalchemy import create_engine
- import pandas as pd
- #定义一个类 创建类的实例 作为状态的容器
- class _a():
- pass
- A = _a()
- A.bought_list = []
- A.hsa = xtdata.get_stock_list_in_sector('沪深A股')
- def interact():
- """执行后进入repl模式"""
- import code
- code.InteractiveConsole(locals=globals()).interact()
- xtdata.download_sector_data()
- def f(data):
- now = datetime.datetime.now()
- for stock in data:
- if stock not in A.hsa:
- continue
- cuurent_price = data[stock]['lastPrice']
- pre_price = data[stock]['lastClose']
- ratio = cuurent_price / pre_price - 1 if pre_price > 0 else 0
- if ratio > 0.09 and stock not in A.bought_list:
- print(f"{now} 最新价 买入 {stock} 200股")
- async_seq = xt_trader.order_stock_async(acc, stock, xtconstant.STOCK_BUY, 200, xtconstant.LATEST_PRICE, -1, 'strategy_name', stock)
- A.bought_list.append(stock)
- xt_trader.query_stock_orders()
- class MyXtQuantTraderCallback(XtQuantTraderCallback):
- def on_disconnected(self):
- """
- 连接断开
- :return:
- """
- print(datetime.datetime.now(),'连接断开回调')
- def on_stock_order(self, order):
- """
- 委托回报推送
- :param order: XtOrder对象
- :return:
- """
- print(datetime.datetime.now(), '委托回调', order.order_remark)
- def on_stock_trade(self, trade):
- """
- 成交变动推送
- :param trade: XtTrade对象
- :return:
- """
- print(datetime.datetime.now(), '成交回调', trade.order_remark)
- def on_order_error(self, order_error):
- """
- 委托失败推送
- :param order_error:XtOrderError 对象
- :return:
- """
- # print("on order_error callback")
- # print(order_error.order_id, order_error.error_id, order_error.error_msg)
- print(f"委托报错回调 {order_error.order_remark} {order_error.error_msg}")
- def on_cancel_error(self, cancel_error):
- """
- 撤单失败推送
- :param cancel_error: XtCancelError 对象
- :return:
- """
- print(datetime.datetime.now(), sys._getframe().f_code.co_name)
- def on_order_stock_async_response(self, response):
- """
- 异步下单回报推送
- :param response: XtOrderResponse 对象
- :return:
- """
- print(f"异步委托回调 {response.order_remark}")
- def on_cancel_order_stock_async_response(self, response):
- """
- :param response: XtCancelOrderResponse 对象
- :return:
- """
- print(datetime.datetime.now(), sys._getframe().f_code.co_name)
- def on_account_status(self, status):
- """
- :param response: XtAccountStatus 对象
- :return:
- """
- print(datetime.datetime.now(), sys._getframe().f_code.co_name)
- if __name__ == '__main__':
- engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qmt_stocks_tech?charset=utf8')
- df = pd.read_sql_table('601155.SH_1d', engine)
- df = df[df['HL'] != '-']
- print(df)
- df.to_csv(f"C:\Daniel\策略\601155.csv", index=True, encoding='utf_8_sig',
- mode='w')
- exit()
- print("start")
- #指定客户端所在路径
- path = r'c:\\qmt\\userdata_mini'
- # 生成session id 整数类型 同时运行的策略不能重复
- session_id = int(time.time())
- xt_trader = XtQuantTrader(path, session_id)
- # 创建资金账号为 800068 的证券账号对象
- acc = StockAccount('920000207040', 'SECURITY')
- # 创建交易回调类对象,并声明接收回调
- callback = MyXtQuantTraderCallback()
- xt_trader.register_callback(callback)
- # 启动交易线程
- xt_trader.start()
- # 建立交易连接,返回0表示连接成功
- connect_result = xt_trader.connect()
- print('建立交易连接,返回0表示连接成功', connect_result)
- # 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
- subscribe_result = xt_trader.subscribe(acc)
- print('对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功', subscribe_result)
- asset = xt_trader.query_stock_asset(acc)
- cash = asset.cash
- positions = xt_trader.query_stock_positions(acc)
- print(cash, positions)
- # xt_trader.run_forever()
- print('run over')
- if '600362.SH' == positions[0].stock_code:
- print('aaa')
- exit()
- # xtdata.subscribe_whole_quote(["SH", "SZ"], callback=f)
- xtdata.run()
- #进入交互模式
- # interact()
|