# 策略 Demo1:
import sys
import json
from functools import partial
import kungfu.yijinjing.time as kft
import kungfu.wingchun.utils as wc_utils
from kungfu.wingchun.constants import *
import pyyjj
#如果需要米筐数据,需要引入rqdatac
#import rqdatac as rq
#from rqdatac import*
source = Source.XTP
exchange_sse = Exchange.SSE
exchange_sze = Exchange.SZE
account_str = "15003934"
# 静态行情整个交易日都不会变,启动策略时取到所有证券的静态数据后,需要时直接使用
# 而不需要用一次查一次,性能很差(因为需要读写文件)
# key的格式是:证券代码.证券市场;比如:600000.SSE和000001.SZE
quotes={}
def test_timer(context, event):
context.log.info('test timer')
def test_timer_with_parameter_x(context, event, x):
context.log.info('test timer, x={}'.format(x))
def test_time_interval(context, event):
context.log.info('test time interval')
def test_time_interval_with_parameter_y(context, event, y):
context.log.info('test time interval, y={}'.format(y))
def cancel_order(context, event, order_id):
if order_id in context.book._orders and context.book._orders[order_id].active:
context.cancel_order(order_id)
def print_books(context):
if context.all_books_ready():
strategy_book = context.book #策略的资金与持仓
context.log.info("strategy available cash:{}".format(strategy_book.avail))
context.log.info("strategy position as follow :")
for position in strategy_book.positions:
context.log.info("account instrument_id:{}, volume:{},account sellable volume:{},account avg_open_price :{}".format(position.instrument_id , position.volume - position.frozen_total , position.yesterday_volume - position.frozen_yesterday ,position.avg_open_price ))
account_book = context.get_account_book(source, account_str ) #某资金账户的资金与持仓
context.log.warning("[account available cash]:{}".format( account_book.avail))
pos_es = account_book.positions
for pos in pos_es:
context.log.info("account instrument_id:{}, volume:{},account sellable volume:{},account avg_open_price :{}".format(pos.instrument_id ,pos.volume - pos.frozen_total , pos.yesterday_volume - pos.frozen_yesterday ,pos.avg_open_price ))
else:
context.log.warning("book is not ready,please wait one more moment!")
#启动前
def pre_start(context):
context.log.info("pre run strategy")
# 加载ETF数据,尽量在TD启动三分钟之后再启动策略,防止TD还没有收集齐ETF数据
load_ret = context.load_etf_info()
context.log.info("load etf: {}.".format(load_ret))
if load_ret:
# 只有加载成功后,才能进行以下操作
# 1、获取全市场的ETF清单,返回的map是以“ETF市场_ETF代码”组合成的字符串作为key,比如:SZE_159646
bases = context.get_all_etfs()
basket_prt = False
for etf_code, base in bases.items():
if not basket_prt:
basket_prt = True
# 2、获取指定ETF的所有成分股
baskets = context.get_etf_all_baskets(etf_code)
for etf, basket in baskets.items():
context.log.info("etf code: {} basket code: {}.".format(basket.get_class_key, basket.get_major_key))
context.log.info("etf code: {}, num: {}.".format(etf_code, len(context.get_etf_all_baskets(etf_code))))
# 3、获取单市场的所有ETF清单,市场不能为空,EXCHANGE_SSE:沪市;EXCHANGE_SZE:深市
sh_bases = context.get_etf_base(Exchange.SSE, "")
for sh, sh_base in sh_bases.items():
context.log.info("all shanghai etf code: {}.".format(sh))
context.log.info("all shanghai etf total num: {}.".format(len(sh_bases)))
# 4、获取指定某支ETF清单,市场和ETF代码都传即可;若两个参数都正确传值,返回的map大小为1;否则为空
sh_bases = context.get_etf_base(Exchange.SSE, "588060")
for sh, sh_base in sh_bases.items():
# 5、获取指定ETF的某个市场的所有成分股,市场不能为空,返回的map是以“成分股市场_成分股代码”组合成的字符串作为key,比如:SSE_688772
baskets = context.get_etf_baskets(sh, Exchange.SSE, "")
context.log.info("shanghai etf code: {}, info: {} basket num: {}.".format(sh, sh_base, len(baskets)))
for etf, basket in baskets.items():
context.log.info("etf code: {} shanghai basket code: {}.".format(basket.get_class_key, basket.get_major_key))
# 6、获取指定ETF的某个市场的某支成分股;若三个参数都正确传值,返回的map大小为1;否则为空
baskets = context.get_etf_baskets(sh, Exchange.SSE, "688772")
for etf, basket in baskets.items():
context.log.info("etf code: {} 688772 basket code: {}, info: {}.".format(basket.get_class_key, basket.get_major_key, basket))
context.book_ready_flag = False #该变量为自定义的全局变量,用于标记策略是否已经拿到策略自己以及所使用账号的持仓;策略在使用资金账户之前要先通过context.add_account添加资金账户。
context.add_account(source, account_str, 100000000.0)
# 查询新发行的债
# 备注:context.query_bondIPO_infolist 函数从2.1.1版本开始支持
context.query_bondIPO_infolist(account_str)
#登录xtp算法交易服务器
#备注:1.context.login_algo_server()函数从2.0.12版本开始支持
# 2.参数需要根据实际情况填写
#context.login_algo_server(source, account_str, "algo_user", "algo_password")
context.subscribe(source, ["600000", "601988"], exchange_sse)
# 订阅逆回购品种
#context.subscribe_market_data(Source.XTP, ["204001"], exchange_sse)
#获取沪深交易所全量静态行情
# 静态行情数量大于零,说明获取成功
global quotes
quotes=context.getAllStaticQuote()
context.log.info('static quotes num: {}.'.format(len(quotes)))
quote = quotes['600000.SSE']
context.log.info('quotes[600000.SSE]={}'.format(quote))
context.log.info('instrument_id={}, exchange_id={}, upper_limit_price={}, lower_limit_price={}, '.format(quote.instrument_id, quote.exchange_id, quote.upper_limit_price, quote.lower_limit_price))
#遍历quotes
#for key in quotes:
# quote = quotes[key]
# context.log.info("key={}, instrument_id={}, exchange_id={}".format(key, quote.instrument_id, quote.exchange_id))
#getAllStaticQuote()函数的使用方法,详见“kungfu SDK API 文档”
#获取新三板/北交所全量静态行情
# 静态行情数量大于零,说明获取成功
# 备注:context.getAllNQStaticQuote()函数从 2.1.3 版本开始支持
global nq_quotes
nq_quotes=context.getAllNQStaticQuote()
context.log.info('nq static quotes num: {}.'.format(len(nq_quotes)))
# 加载自定义库文件,库文件需跟当前策略文件放在同一目录下
# luck_package.py为自定义库文件的名字,可以根据实际情况自行替换和修改
# 该功能要求系统版本大于等于2.0.8
# cust_lib = context.import_local_lib('luck_package.py')
# result = cust_lib.test()
#获取参数配置文件路径的参考代码
#path = context.getParamFileDir()
#config_file = os.path.join(path, "param.csv")
# 订阅买一卖一队列行情
#备注:context.subscribe_bid1_ask1()函数从2.0.11版本开始支持
#context.subscribe_bid1_ask1(Source.XTP, ["600000", "601988"], exchange_sse)
#context.subscribe_tick_by_tick(source, ["600000", "601988"], exchange_sse)
#context.subscribe_order_book(source, ["600000", "601988"], exchange_sse)
# 订阅全市场指数通行情
# context.subscribe_all_market(Source.XTP, "", AllMarketDataType.type_index_press)
# 订阅全市场港股通行情
# context.subscribe_all_market(Source.XTP, Exchange.HKEX, AllMarketDataType.type_market_data)
# 获取交易日历
# 备注:context.get_trading_dates()函数从2.1.8版本开始支持
# trading_dates = context.get_trading_dates()
# if trading_dates:
# context.log.info("trading dates num: {}, first: {}, last: {}.".format(len(trading_dates), trading_dates[0], trading_dates[-1]))
# 查询融资融券标的
# 备注:context.query_credit_tickers()函数从2.1.8版本开始支持
# credit_tickers = context.query_credit_tickers()
# for i, ticker in enumerate(credit_tickers):
# context.log.info("[{}] {} | {} | 融资:{} 融券:{} | 融资保证金:{} 融券保证金:{}".format(i + 1, ticker.instrument_id, ticker.exchange_id, ticker.credit_fund_ctl, ticker.credit_stk_ctl, ticker.fi_margin_rate, ticker.sl_margin_rate))
#context.subscribe('bar', ["600000", "601988", "601600", "600589"], Exchange.SSE)#订阅bar行情
#context.add_timer(pyyjj.now_in_nano()+100, partial(test_timer))
#context.add_timer(pyyjj.now_in_nano()+100, partial(test_timer_with_parameter_x, x=1)) #带参数x的定时函数
#context.add_time_interval(kft.NANO_PER_SECOND, partial(test_time_interval)) #kft.NANO_PER_SECOND是一个系统定义的数值常量,为1000000000,单位是纳秒,也即1秒钟;
#客户可根据实际需要,自行对该值进行替换
#context.add_time_interval(kft.NANO_PER_SECOND, partial(test_time_interval_with_parameter_y, y=1)) #带参数y的定时函数
#调用米筐数据的样例
#rq.init("username","password",('IP地址',端口))
#rqt = rq.get_price('000001.XSHE','2020-9-28','2020-9-28','tick')
#context.log.info(rqt.shape)
#信用账户查询相关示例
#请求查询信用账户特有信息
#备注:context.query_credit_fund_info()函数从2.0.8版本开始支持
#context.query_credit_fund_info(source, account_str)
#融资融券业务中请求现金直接还款
#备注:context.credit_cash_repay()函数从2.0.8版本开始支持
#context.credit_cash_repay(source, account_str, 50000.00)
#请求查询信用账户可融券头寸信息
#备注:context.query_credit_ticker_assign_info()函数从2.0.11版本开始支持
#context.query_credit_ticker_assign_info(source, account_str, "600000", exchange_sse)
#请求查询信用账户负债合约信息
#备注:context.query_credit_debt_info()函数从2.0.11版本开始支持
#context.query_credit_debt_info(source, account_str)
#请求查询信用账户指定证券负债未还信息
#备注:context.query_credit_ticker_debt_info()函数从2.0.11版本开始支持
#context.query_credit_ticker_debt_info(source, account_str, "600000", exchange_sse)
#请求查询信用账户待还资金信息
#备注:context.query_credit_asset_debt_info()函数从2.0.11版本开始支持
#context.query_credit_asset_debt_info(source, account_str)
#请求债转股信息
#备注:context.query_bondswapstock_info()函数从2.1.2版本开始支持
#查询所有的债转股信息
context.query_bondswapstock_info(account_str)
#查询单支债转股信息
#context.query_bondswapstock_info(account_str, "110001", exchange_sse)
# 查询用户新股申购额度信息
# 备注:context.query_IPO_quota_info()函数从2.1.8版本开始支持
context.query_IPO_quota_info(source, account_str);
# 查询今日可申购新股信息列表
# 备注:context.query_IPO_info_list()函数从2.1.8版本开始支持
context.query_IPO_info_list(source, account_str);
#启动后
def post_start(context):
print_books(context)
context.log.info("today is trading day? {}".format(context.is_trading_day()))
# 请求查询资金划拨
# serial_id = 12345 #测试时需将该值替换成要查询的序列号
# context.query_fund_transfer(account, serial_id)
# 查询两融担保品标的券
# cgs = context.pull_credit_guarantee_stk()
# 查询除权除息数据
# dar = context.pull_divident_and_right()
pass
#策略退出前,可在此做一些回收工作
def pre_stop(context):
context.log.info("strategy pre_stop.")
#退出后
def post_stop(context):
pass
#bar 分钟级别行情回调
def on_bar(context, bar):
if not context.book_ready_flag: #此判断逻辑非必须,如果策略不关心策略或者账户资金与持仓,直接注掉该if语句块即可。
context.logger.warning("book has not been ready")
return
context.logger.info("bar received: [instrument_id]{} [exchange_id]{}".format(bar.instrument_id, bar.exchange_id))
pass
#行情数据更新回调
def on_quote(context, quote):
if not context.book_ready_flag: #此判断逻辑非必须,如果策略不关心策略或者账户资金与持仓,直接注掉该if语句块即可。
context.logger.warning("book has not been ready")
return
context.log.info("quote received: [time]{} [instrument_id]{} [last_price]{}".format(quote.data_time, quote.instrument_id, quote.last_price))
#一般来说可以在此处进行逻辑判断,符合下单条件下单即可。
order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account_str , quote.ask_price[0], 200, PriceType.Limit, Side.Buy, Offset.Open, HedgeFlag.Speculation)
#信用交易示例
#担保品买入
#order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account, quote.ask_price[0],
# 200, PriceType.Limit, Side.Buy, Offset.Open, HedgeFlag.Speculation, BusinessType.MARGIN)
#担保品卖出
#order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account, quote.bid_price[0],
# 200, PriceType.Limit, Side.Sell, Offset.Open, HedgeFlag.Speculation, BusinessType.MARGIN)
#融资买入
#order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account, quote.bid_price[0],
# 200, PriceType.Limit, Side.MarginTrade, Offset.Open, HedgeFlag.Speculation, BusinessType.MARGIN)
#卖券还款
#order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account, quote.ask_price[0],
# 200, PriceType.Limit, Side.RepayMargin, Offset.Open, HedgeFlag.Speculation, BusinessType.MARGIN)
#融券卖出
#order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account, quote.ask_price[0],
# 200, PriceType.Limit, Side.ShortSell, Offset.Open, HedgeFlag.Speculation, BusinessType.MARGIN)
#买券还券
#order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account, quote.ask_price[0],
# 200, PriceType.Limit, Side.RepayStock, Offset.Open, HedgeFlag.Speculation, BusinessType.MARGIN)
#现券还券
#order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account, quote.ask_price[0],
# 200, PriceType.Limit, Side.StockRepayStock, Offset.Open, HedgeFlag.Speculation, BusinessType.MARGIN)
#逆回购下单 入参注意事项:
#1.交易方向为卖即Side.Sell
#2.价格类型为限价即PriceType.Limit
#3.业务类型为回购业务即BusinessType.REPO
#4.交易数量:根据最新调整,沪深交易所逆回购一手均为10张
#if(quote.instrument_id == "204001" and quote.exchange_id == exchange_sse):
# order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account_xtp, quote.bid_price[0], 10, PriceType.Limit, Side.Sell, Offset.Open, HedgeFlag.Speculation, BusinessType.REPO)
#elif (quote.instrument_id == "131810" and quote.exchange_id == exchange_sze):
# order_id = context.insert_order(quote.instrument_id, quote.exchange_id, account_xtp, quote.bid_price[0], 10, PriceType.Limit, Side.Sell, Offset.Open, HedgeFlag.Speculation, BusinessType.REPO)
#下单后一段时间通过定时函数进行撤单
#if order_id>0:
# context.add_timer(pyyjj.now_in_nano()+kft.NANO_PER_SECOND, partial(cancel_order, order_id = order_id))
#策略向客户端发送自定义字符串。
#备注:入参的长度,应小于10000字节
#context.send_msg("insert_order({},{})".format(quote.instrument_id, quote.exchange_id))
#策略用户在双中心间进行资金划转(从远端节点转入到本节点200元)。
#account_str是当前下单用的资金账户;“123456”是账户的密码,需根据实际情况设定
#context.fund_transfer(account_str, “123456”, FundTransferType.FundInterTransferIn 200.0)
#策略用户查询双中心间资金划转情况。
#account_str是当前下单用的资金账户;“123456”是账户的密码,需根据实际情况设定
#context.query_other_server_fund(account_str, “123456”, FundQueryType.FundQueryInternal)
#指数通数据更新回调
def on_index_press(context, index_press):
context.log.info('index_press received: [instrument_id]{}'.format(index_press.instrument_id))
pass
#港股通实时额度数据更新回调
def on_hkc_realtime_limit(context, hkc_rtl):
context.log.info('hkc_realtime_limit received: [初始额度]{} [剩余额度]{}'.format(hkc_rtl.threshold_amount, hkc_rtl.pos_amt))
pass
#订单簿数据更新回调
def on_order_book(context, order_book):
context.log.info('order_book received: [instrument_id]{} [exchange_id]{}'.format(order_book.instrument_id, order_book.exchange_id))
pass
#买一卖一队列数据更新回调
#备注:on_bid1ask1(context, b1a1)函数从2.0.11版本开始支持
def on_bid1ask1(context, b1a1):
context.log.info('bid1ask1 received: [instrument_id]{} [exchange_id]{}'.format(b1a1.instrument_id, b1a1.exchange_id))
pass
#逐笔成交更新回调
def on_transaction(context, transaction):
context.log.info("{} {}".format(transaction.instrument_id, transaction.exchange_id))
pass
#逐笔委托更新回调
def on_entrust(context, entrust):
context.log.info("{} {}".format(entrust.instrument_id, entrust.exchange_id))
pass
#订单信息更新回调
def on_order(context, order):
context.log.info('order received: [instrument_id]{} [volume]{} [price]{}'.format(order.instrument_id, order.volume, order.limit_price))
#订单成交回报回调
def on_trade(context, trade):
context.log.info('trade received: {} [trade_id]{} [volume]{} [price]{}'.format(kft.strftime(trade.trade_time), trade.order_id, trade.volume, trade.price))
#订单操作错误回调 撤单反馈信息回调
def on_order_action_error(context, error):
context.log.info('order_action_error received: [order_id]{} [error_id]{}'.format(error.order_id, error.error_id))
pass
# #撤单反馈信息回调
# def on_cancelorder_feedback(context, cofeedback):
# context.log.info('cancelorder_feedback received: [feedback_type]{} [order_id]{}'.format(cofeedback.feedback_type, cofeedback.order_id))
# pass
#资金账户信息更新回调
#备注:on_asset(context, asset)函数从2.0.11版本开始支持
def on_asset(context, asset):
context.log.info('asset received: [source_id]{} [account_id]{}'.format(asset.source_id, asset.account_id))
pass
#接收前端发送回来的客户自定义消息
def on_client_msg(context, msg):
context.log.info('on_client_msg received: {}'.format(msg))
#msg类型为字符串,内容是客户自定义的json结构字符串,比如:“{\"A\"=1,\"B\"=2,\"C\"=3,}”
#可通过如下语句解析:
#j = json.loads(msg)
#A = j["A"]
#B = j["B"]
#C = j["C"]
#context.log.info('A: {}, B: {}, C: {} '.format(A, B, C))
pass
#融资融券特有帐户数据回调
#备注:on_query_credit_fund_info(context, crdfundinfo)函数从2.0.8版本开始支持
def on_query_credit_fund_info(context, crdfundinfo):
context.log.info('crdfundinfo received: [all_asset]{} [all_debt]{}'.format(crdfundinfo.all_asset, crdfundinfo.all_debt))
pass
#融资融券业务中现金直接还款的响应回调
#备注:on_credit_cash_repay(context, crd_cash_repay_info)函数从2.0.8版本开始支持
def on_credit_cash_repay(context, crd_cash_repay_info):
context.log.info('crd_cash_repay_info received: [request_amount]{} [cash_repay_amount]{}'.format(crd_cash_repay_info.request_amount, crd_cash_repay_info.cash_repay_amount))
pass
#信用账户可融券头寸信息更新回调
#备注:on_query_credit_ticker_assign_info(context, crd_position_stk_info)函数从2.0.11版本开始支持
def on_query_credit_ticker_assign_info(context, crd_position_stk_info):
context.log.info('credit_ticker_assign_info received: [instrument_id]{} [exchange_id]{}'.format(crd_position_stk_info.instrument_id, crd_position_stk_info.exchange_id))
pass
#信用账户负债信息更新回调
#备注:on_query_credit_debt_info(context, crd_debt_info)函数从2.0.11版本开始支持
def on_query_credit_debt_info(context, crd_debt_info):
context.log.info('credit_debt_info received: [instrument_id]{} [exchange_id]{}'.format(crd_debt_info.instrument_id, crd_debt_info.exchange_id))
pass
#信用账户指定证券负债未还信息更新回调
#备注:on_query_credit_ticker_debt_info(context, crd_debt_stk_info)函数从2.0.11版本开始支持
def on_query_credit_ticker_debt_info(context, crd_debt_stk_info):
context.log.info('credit_ticker_debt_info received: [instrument_id]{} [exchange_id]{}'.format(crd_debt_stk_info.instrument_id, crd_debt_stk_info.exchange_id))
pass
#信用账户待还资金信息更新回调
#备注:on_query_credit_asset_debt_info(context, remain_amount)函数从2.0.11版本开始支持
def on_query_credit_asset_debt_info(context, remain_amount):
context.log.info('credit_asset_debt_info received: [remain_amount]{}'.format(remain_amount))
pass
#资金划转结果回调
def on_fund_transfer(context, feedback):
context.log.info('on_fund_transfer received: [feedback]{}'.format(feedback))
pass
#资金查询结果回调
def on_query_other_server_fund(context, feedback):
context.log.info('on_query_other_server_fund received: [feedback]{}'.format(feedback))
pass
#xtp算法交易服务器登录反馈信息回调
#备注:1.on_login_algo_server(context, success)函数从2.0.12版本开始支持
# 2.参数需要根据实际情况填写
def on_login_algo_server(context, success):
context.log.info('login_algo_server result is: {} '.format(success))
if success:
strategy_param = {}
strategy_param["buyDateTime"] = ["09:30", "09:40"]
strategy_param["stop_selling_while_buy_overflow"] = False
strategy_param["cancel_overflow_order_after_clear_time"] = True
strategy_param["start_time"] = "09:30:00"
strategy_param["end_time"] = "09:40:00"
strategy_param["trade_list"] = []
trade0 = {}
trade0["market"] = "SZ"
trade0["clientStrategyId"] = "202010200902"
trade0["quantity"] = 20000
trade0["side"] = "BUY"
trade0["ticker"] = "002513"
trade1 = {}
trade1["market"] = "SZ"
trade1["clientStrategyId"] = "202010200902"
trade1["quantity"] = 20000
trade1["side"] = "BUY"
trade1["ticker"] = "001914"
strategy_param["trade_list"].append(trade0)
strategy_param["trade_list"].append(trade1)
strategy_param["business_type"] = "CASH"
context.create_algo_strategy(account_str, 2001, 202010200902, json.dumps(strategy_param))
#备注:变量strategy_param的数据格式,由context.create_algo_strategy()函数的第二个参数来决定,具体细节需咨询算法交易相关人员
pass
#xtp算法交易创建策略反馈信息回调
#备注: on_create_algo_strategy(context, algo_strategy_info)函数从2.0.12版本开始支持
def on_create_algo_strategy(context, algo_strategy_info):
context.log.info('algo_strategy_info received: {} '.format(algo_strategy_info))
context.start_algo_strategy(account_xtp, algo_strategy_info.xtp_strategy_id)
pass
#xtp算法交易查询策略反馈信息回调
#备注: on_query_algo_strategy(context, algo_strategy_info)函数从2.0.12版本开始支持
def on_query_algo_strategy(context, algo_strategy_info):
context.log.info('algo_strategy_info received: {} '.format(algo_strategy_info))
pass
#xtp算法交易开始运行策略反馈信息回调
#备注: on_start_algo_strategy(context, algo_strategy_info)函数从2.0.12版本开始支持
def on_start_algo_strategy(context, algo_strategy_info):
context.log.info('algo_strategy_info received: {} '.format(algo_strategy_info))
pass
#xtp算法交易停止运行策略反馈信息回调
#备注: on_stop_algo_strategy(context, algo_strategy_info)函数从2.0.12版本开始支持
def on_stop_algo_strategy(context, algo_strategy_info):
context.log.info('algo_strategy_info received: {} '.format(algo_strategy_info))
pass
#xtp算法交易销毁策略反馈信息回调
#备注: on_destroy_algo_strategy(context, algo_strategy_info)函数从2.0.12版本开始支持
def on_destroy_algo_strategy(context, algo_strategy_info):
context.log.info('algo_strategy_info received: {} '.format(algo_strategy_info))
pass
#xtp算法交易策略信息更新回调
#备注:on_algo_strategy_info(context, algo_strategy_info)函数从2.0.12版本开始支持
def on_algo_strategy_info(context, algo_strategy_info):
context.log.info('algo_strategy_info received: {} '.format(algo_strategy_info))
pass
#xtp算法交易运行时策略状态更新回调
#备注: on_algo_strategy_state_report(context, algo_strategy_state_report)函数从2.0.12版本开始支持
def on_algo_strategy_state_report(context, algo_strategy_state_report):
context.log.info('algo_strategy_state_report received: {} '.format(algo_strategy_state_report))
pass
#接收查询到的新债信息
# 备注:on_query_bondIPO_info函数从2.1.1版本开始支持
def on_query_bondIPO_info(context, IPOInfo):
if 0 == IPOInfo.error_id:
context.log.info('bond ipo info: {}'.format(IPOInfo))
else:
context.log.warn('error bond ipo info: {}'.format(IPOInfo))
pass
#完成接收查询到的新债信息
# 备注:on_query_bondIPO_end函数从2.1.1版本开始支持
def on_query_bondIPO_end(context):
context.log.info('bond ipo collected finish.')
pass
#接收查询到的债转股信息
def on_query_bondswapstock_info(context, swap_info):
if 0 == swap_info.error_id:
context.log.info('bond swap stock info: {}'.format(swap_info))
else:
context.log.warn('error bond swap stock info: {}'.format(swap_info))
pass
#完成接收查询到的债转股信息
def on_query_bondswapstock_end(context):
context.log.info('bond swap stock collected finish.')
pass
#接收查询到的用户新股申购额度信息; 从2.1.8版本开始支持
def on_query_IPO_quota_info(context, quota_info):
if 0 == quota_info.error_id:
context.log.info('IPO quota info: {}'.format(quota_info))
else:
context.log.warn('error IPO quota info: {}'.format(quota_info))
pass
#完成接收查询到的用户新股申购额度信息; 从2.1.8版本开始支持
def on_query_IPO_quota_info_end(context):
context.log.info('IPO quota info collected finish.')
pass
#接收查询到的今日可申购新股信息; 从2.1.8版本开始支持
def on_query_IPO_info_list(context, ipo_info):
if 0 == ipo_info.error_id:
context.log.info('IPO ticker info: {}'.format(ipo_info))
else:
context.log.warn('error IPO ticker info: {}'.format(ipo_info))
pass
#完成接收查询到的今日可申购新股信息; 从2.1.8版本开始支持
def on_query_IPO_info_list_end(context):
context.log.info('IPO ticker info collected finish.')
pass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585