|  | @@ -0,0 +1,115 @@
 | 
											
												
													
														|  | 
 |  | +# coding:utf-8
 | 
											
												
													
														|  | 
 |  | +from datetime import datetime as dt
 | 
											
												
													
														|  | 
 |  | +import os
 | 
											
												
													
														|  | 
 |  | +import pandas as pd
 | 
											
												
													
														|  | 
 |  | +from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
 | 
											
												
													
														|  | 
 |  | +from xtquant.xttype import StockAccount
 | 
											
												
													
														|  | 
 |  | +from xtquant import xtdata, xtconstant
 | 
											
												
													
														|  | 
 |  | +import time
 | 
											
												
													
														|  | 
 |  | +from sqlalchemy import create_engine
 | 
											
												
													
														|  | 
 |  | +from jqdatasdk import *
 | 
											
												
													
														|  | 
 |  | +import pymysql
 | 
											
												
													
														|  | 
 |  | +import multiprocessing as mp
 | 
											
												
													
														|  | 
 |  | +import math
 | 
											
												
													
														|  | 
 |  | +import psutil
 | 
											
												
													
														|  | 
 |  | +from apscheduler.schedulers.blocking import BlockingScheduler
 | 
											
												
													
														|  | 
 |  | +import sys
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +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__':
 | 
											
												
													
														|  | 
 |  | +    auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    mp.freeze_support()
 | 
											
												
													
														|  | 
 |  | +    print('cpu_count =', mp.cpu_count())
 | 
											
												
													
														|  | 
 |  | +    pus = psutil.Process()
 | 
											
												
													
														|  | 
 |  | +    pus.cpu_affinity([6, 7])
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    # 指定客户端所在路径
 | 
											
												
													
														|  | 
 |  | +    path = r'c:\\qmt\\userdata_mini'
 | 
											
												
													
														|  | 
 |  | +    # 生成session id 整数类型 同时运行的策略不能重复
 | 
											
												
													
														|  | 
 |  | +    session_id = int(time.time())
 | 
											
												
													
														|  | 
 |  | +    xt_trader = XtQuantTrader(path, session_id)
 | 
											
												
													
														|  | 
 |  | +    # 创建资金账号为 800068 的证券账号对象
 | 
											
												
													
														|  | 
 |  | +    acc = StockAccount('920000207040')
 | 
											
												
													
														|  | 
 |  | +    # 创建交易回调类对象,并声明接收回调
 | 
											
												
													
														|  | 
 |  | +    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)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    stock = '110074.SZ'
 | 
											
												
													
														|  | 
 |  | +    volume = 10
 | 
											
												
													
														|  | 
 |  | +    current_price = xtconstant.LATEST_PRICE
 | 
											
												
													
														|  | 
 |  | +    order_id = xt_trader.order_stock(acc, stock, xtconstant.STOCK_BUY, volume,
 | 
											
												
													
														|  | 
 |  | +                                     xtconstant.LATEST_PRICE, 0, 'KZZ', 'TEST')
 | 
											
												
													
														|  | 
 |  | +    print(order_id)
 |