on_order_response
该函数会在委托主推回调时响应,比引擎、get_order()和get_orders()函数更新Order状态的速度更快,适合对速度要求比较高的策略。
原型
on_order_response(context, order_list)
适用环境
该函数仅在交易模块可用,对接jz_ufx不支持该函数
释义
该函数会在委托主推回调时响应,比引擎、get_order()和get_orders()函数更新Order状态的速度更快,适合对速度要求比较高的策略。
注意事项
目前可接收股票、可转债、ETF、LOF、期货代码的主推数据。
当接到策略外交易产生的主推时(需券商配置默认不推送),由于没有对应的Order对象,主推信息中order_id字段赋值为""。
当主推先于委托应答返回时,由于无法根据entrust_no匹配对应的Order对象,主推信息中order_id字段赋值为""。
当在主推里调用委托接口时,需要进行判断处理避免无限迭代循环问题。
当券商配置接收策略外交易产生的主推且策略调用set_parameters()并设置receive_other_response="1"时, 策略中将接收非本交易产生的主推。
当策略调用set_parameters()并设置receive_cancel_response="1", 策略接收到撤单成交主推时,主推信息中的order_id为买入或卖出委托Order对象的order_id,entrust_no为撤单委托的委托编号。
撤单委托主推信息中成交数量均处理为正数。
可调用委托接口
| set_parameters(回测/交易) | get_history(研究/回测/交易) | get_price(研究/回测/交易) | get_individual_entrust(交易) | get_individual_transaction(交易) |
|---|---|---|---|---|
| get_tick_direction(交易) | get_sort_msg(交易) | get_underlying_code(交易) | get_etf_info(交易) | get_etf_stock_info(交易) |
| get_gear_price(交易) | get_snapshot(交易) | get_cb_info(研究/交易) | get_trend_data(研究/回测/交易) | get_stock_name(研究/回测/交易) |
| get_stock_info(研究/回测/交易) | get_stock_status(研究/回测/交易) | get_stock_exrights(研究/回测/交易) | get_stock_blocks(研究/回测/交易) | get_index_stocks(研究/回测/交易) |
| get_etf_stock_list(交易) | get_industry_stocks(研究/回测/交易) | get_fundamentals(研究/回测/交易) | get_Ashares(研究/回测/交易) | get_etf_list(交易) |
| get_ipo_stocks(交易) | get_cb_list(交易) | get_reits_list(研究/回测/交易) | get_position(回测/交易) | get_positions(回测/交易) |
| get_all_positions(交易) | order(回测/交易) | order_target(回测/交易) | order_value(回测/交易) | order_target_value(回测/交易) |
| order_market(交易) | ipo_stocks_order(交易) | after_trading_order(交易) | after_trading_cancel_order(交易) | etf_basket_order(交易) |
| etf_purchase_redemption(交易) | cancel_order(回测/交易) | cancel_order_ex(交易) | debt_to_stock_order(交易) | get_open_orders(回测/交易) |
| get_order(回测/交易) | get_orders(回测/交易) | get_all_orders(交易) | get_trades(回测/交易) | margin_trade(回测/交易) |
| margincash_open(交易) | margincash_close(交易) | margincash_direct_refund(交易) | marginsec_open(交易) | marginsec_close(交易) |
| marginsec_direct_refund(交易) | get_margin_contract(交易) | get_margin_contractreal(交易) | get_margin_asset(交易) | get_assure_security_list(交易) |
| get_margincash_open_amount(交易) | get_margincash_close_amount(交易) | get_marginsec_open_amount(交易) | get_marginsec_close_amount(交易) | get_margin_entrans_amount(交易) |
| get_enslo_security_info(交易) | buy_open(回测/交易(期货)) | sell_close(回测/交易(期货)) | sell_open(回测/交易(期货)) | buy_close(回测/交易(期货)) |
| get_MACD(回测/交易) | get_KDJ(回测/交易) | get_RSI(回测/交易) | get_CCI(回测/交易) | |
| log(回测/交易) | check_limit(回测/交易) | send_email(交易) | send_qywx(交易) | create_dir(研究/回测/交易) |
| get_dominant_contract(研究/回测/交易(期货)) | get_crdt_fund(交易) | fund_transfer(交易) | market_fund_transfer(交易) |
参数
context:Context对象,存放有当前的账户及持仓信息;
order_list:一个列表,当前委托单发生变化时,发生变化的委托单列表。委托单以字典形式展现,内容包括:'entrust_no'(委托编号), 'error_info'(错误信息), 'order_time'(委托时间), 'stock_code'(股票代码), 'amount'(委托数量), 'price'(委托价格), 'business_amount'(成交数量), 'status'(委托状态), 'entrust_type'(委托类别), 'entrust_prop'(委托属性), 'order_id'(Order对象编号);
返回值
None
接收到的主推格式如下:
本交易委托产生的主推:[{'price': 32.82, 'status': '2', 'amount': 1100, 'order_id': '0e27467920464390aa10a7a53da4d49a', 'stock_code': '600570.SS', 'order_time': '2022-09-21 14:38:35', 'business_amount': 0.0, 'entrust_type': '0', 'entrust_no': '700104', 'error_info': '', 'entrust_prop': '0'}]
本交易撤单产生的主推:[{'price': 32.82, 'status': '2', 'amount': 1100, 'order_id': '0e27467920464390aa10a7a53da4d49a', 'stock_code': '600570.SS', 'order_time': '2022-09-21 14:38:37', 'business_amount': 0.0, 'entrust_type': '2', 'entrust_no': '700105', 'error_info': '', 'entrust_prop': '0'}]
非本交易委托产生的主推:[{'price': 32.82, 'status': '2', 'amount': 1100, 'order_id': '', 'stock_code': '600570.SS', 'order_time': '2022-09-21 14:41:19', 'business_amount': 0.0, 'entrust_type': '0', 'entrust_no': '700106', 'error_info': '', 'entrust_prop': '0'}]
非本交易撤单产生的主推:[{'price': 32.82, 'status': '2', 'amount': 1100, 'order_id': '', 'stock_code': '600570.SS', 'order_time': '2022-09-21 14:41:30', 'business_amount': 0.0, 'entrust_type': '2', 'entrust_no': '700107', 'error_info': '', 'entrust_prop': '0'}]
示例
def initialize(context):
g.security = ['600570.SS','002416.SZ']
set_universe(g.security)
g.flag = 0
def on_order_response(context, order_list):
log.info(order_list)
if(g.flag==0):
order('600570.SS', 100)
g.flag = 1
else:
log.info("end")
def handle_data(context, data):
order('600570.SS', 100)