To build a trading strategy, you need to implement a callback class inheriting from XtQuantTraderCallback to handle real-time market and trade events, then initialize the XtQuantTrader with your client path and a unique session_id.
#coding=utf-8
from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
from xtquant.xtquant import StockAccount
from xtquant import xtconstant
class MyXtQuantTraderCallback(XtQuantTraderCallback):
def on_disconnected(self):
print("connection lost")
def on_stock_order(self, order):
print("on order callback:", order.stock_code, order.order_status, order.order_sysid)
def on_stock_asset(self, asset):
print("on asset callback", asset.account_id, asset.cash, asset.total_asset)
def on_stock_trade(self, trade):
print("on trade callback", trade.account_id, trade.stock_code, trade.order_id)
def on_stock_position(self, position):
print("on position callback", position.stock_code, position.volume)
def on_order_error(self, order_error):
print("on order_error callback", order_error.order_id, order_error.error_id, order_error.error_msg)
def on_cancel_error(self, cancel_error):
print("on cancel_error callback", cancel_error.order_id, cancel_error.error_id, cancel_error.error_msg)
def on_order_stock_async_response(self, response):
print("on_order_stock_async_response", response.account_id, response.order_id, response.seq)
if __name__ == "__main__":
path = 'D:\\迅投极速交易终端 睿智融科版\\userdata_mini'
session_id = 123456
xt_trader = XtQuantTrader(path, session_id)
acc = StockAccount('1000000365')
callback = MyXtQuantTraderCallback()
xt_trader.register_callback(callback)
xt_trader.start()
if xt_trader.connect() == 0:
xt_trader.subscribe(acc)
# ... trading logic ...
xt_trader.run_forever()