PDF下载 下载

算法交易函数

阅读 14792

算法 SDK

python算法SDK包含在gm3.0.126版本及以上版本,不需要引入新库
仅支持实时模式,部分券商版本可用

algo_order 算法交易委托

委托算法母单

函数原型:

  1. algo_order(symbol, volume, side, order_type,position_effect, price, algo_name, algo_param)

参数:

参数 类型 说明
symbol str 标的代码
volume int 数量
side int OrderSide_Buy = 1 买入OrderSide_Sell = 2 卖出
order_type int OrderType_Limit = 1 限价委托,OrderType_Market = 2 市价委托
position_effect int PositionEffect_Open = 1 开仓PositionEffect_Close = 2 平仓,
price int 基准价格(ATS-SMART算法不生效)
algo_name str 算法名称,ATS-SMART、ZC-POV
algo_param dict 算法参数

返回值:

类型 说明
list[order] 委托对象列表,参见委托

当algo_name = ‘ATS-SMART’时
algo_param的参数为

参数 类型 说明
start_time str 开始时间
end_time_referred str 结束参考时间(不能超过14:55:00)
time_end str 结束时间(不能超过14:55:00)
end_time_valid int 结束时间是否有效,如设为无效,则以收盘时间为结束时间, 1为有效, 0为无效
stop_sell_when_DL int 涨停时是否停止卖出, 1为是,0为否
cancel_when_PL int 跌停时是否撤单, 1为是, 0为否
min_trade_amount int 最小交易金额

示例:

  1. # 下算法母单,设定母单的执行参数
  2. algo_param = {'start_time': '09:00:00', 'end_time_referred':'14:55:00', 'end_time': '14:55:00', 'end_time_valid': 1, 'stop_sell_when_dl': 1,
  3. 'cancel_when_pl': 0, 'min_trade_amount': 100000}
  4. aorders = algo_order(symbol='SHSE.600000', volume=20000, side=OrderSide_Buy, order_type=OrderType_Limit,
  5. position_effect=PositionEffect_Open, price=5, algo_name='ATS-SMART', algo_param=algo_param)
  6. print(aorders)

输出:

  1. [{'strategy_id': '6f534238-2883-11eb-a8fe-fa163ef85f63', 'account_id': '927f9095-27e5-11eb-bb81-fa163ef85f63', 'account_name': '1001000002', 'cl_ord_id': '03f13690-2d64-11eb-9e36-fa163ef85f63', 'symbol': 'SHSE.600000', 'side': 1, 'position_effect': 1, 'order_type': 1, 'status': 10, 'price': 5.0, 'order_style': 1, 'volume': 20000, 'created_at': datetime.datetime(2020, 11, 23, 16, 15, 15, 105141, tzinfo=tzfile('PRC')), 'updated_at': datetime.datetime(2020, 11, 23, 16, 15, 15, 105141,tzinfo=tzfile('PRC')), 'algo_name': 'ATS-SMART', 'algo_param': 'start_time&&1606093200||end_time_referred&&1606114500||end_time&&1606114500||end_time_valid&&1||stop_sell_when_dl&&1||cancel_when_pl&&0||min_trade_amount&&100000', 'order_id': '', 'ex_ord_id': '', 'position_side': 0, 'order_business': 0, 'order_duration': 0, 'order_qualifier': 0, 'order_src': 0, 'position_src': 0, 'ord_rej_reason': 0, 'ord_rej_reason_detail': '', 'stop_price': 0.0, 'value': 0.0, 'percent': 0.0, 'target_volume': 0, 'target_value': 0.0, 'target_percent': 0.0, 'filled_volume': 0, 'filled_vwap': 0.0, 'filled_amount': 0.0, 'filled_commission': 0.0, 'algo_status': 0, 'algo_comment': ''}]

当algo_name = ‘ZC-POV’时
algo_param的参数为

参数 类型 说明
part_rate float 市场参与率(0~45),单位%,默认30,即30%

示例:

  1. # 下算法母单,设定母单的执行参数
  2. algo_param = {"participation_rate" : 15}
  3. aorder = algo_order(symbol=symbol, volume=1000, side=OrderSide_Buy, order_type=OrderSide_Buy,
  4. position_effect=PositionEffect_Open, price=price, algo_name=algo_name, algo_param=algo_param)
  5. print(aorder)

输出:

  1. [{'strategy_id': '6f534238-2883-11eb-a8fe-fa163ef85f63', 'account_id': '15b7afb1-e91d-11eb-953b-025041000001', 'account_name': 'b6b2819b-e864-11eb-b146-00163e0a4100', 'cl_ord_id': 'e0f4ca3f-f97a-11eb-acee-165afc004509', 'symbol': 'SHSE.600007', 'side': 1, 'position_effect': 1, 'order_type': 1, 'status': 10, 'price': 15.470000267028809, 'order_style': 1, 'volume': 1000, 'created_at': datetime.datetime(2021, 8, 10, 9, 32, 52, 39737, tzinfo=tzfile('PRC')), 'updated_at': datetime.datetime(2021, 8, 10, 9, 32, 52, 42738, tzinfo=tzfile('PRC')), 'algo_name': 'ZC-POV', 'algo_param': 'TimeStart&&1628559000||TimeEnd&&1628578800||PartRate&&0.150000||MinAmount&&1000', 'order_id': '', 'ex_ord_id': '', 'position_side': 0, 'order_business': 0, 'order_duration': 0, 'order_qualifier': 0, 'order_src': 0, 'position_src': 0, 'ord_rej_reason': 0, 'ord_rej_reason_detail': '', 'stop_price': 0.0, 'value': 0.0, 'percent': 0.0, 'target_volume': 0, 'target_value': 0.0, 'target_percent': 0.0, 'filled_volume': 0, 'filled_vwap': 0.0, 'filled_amount': 0.0, 'filled_commission': 0.0, 'algo_status': 0, 'algo_comment': '', 'properties': {}]

注意: 回测模式不支持算法单

algo_order_cancel 撤销算法委托

撤销母单委托

函数原型:

  1. algo_order_cancel(wait_cancel_orders)

参数:

参数 类型 说明
wait_cancel_orders str 撤单算法委托. 传入单个字典. 或者list字典. 每个字典包含key:cl_ord_id key:account_id

cl_ord_id为委托id, account_id为账户id

返回值:

类型 说明
list[order] 委托对象列表,参见委托

示例:

  1. aorders = get_algo_orders(account='')
  2. wait_cancel_orders = [{'cl_ord_id': aorders[0]['cl_ord_id'], 'account_id': aorders[0]['account_id']}]
  3. algo_order_cancel(wait_cancel_orders)

get_algo_orders 查询算法委托

查询母单委托

函数原型:

  1. algo_order_cancel(account)

参数:

参数 类型 说明
account str account_id 默认帐号时为 ‘’

返回值:

类型 说明
list[order] 委托对象列表,参见委托

示例:

  1. get_algo_orders(account='')

输出:

  1. [{'strategy_id': '6f534238-2883-11eb-a8fe-fa163ef85f63', 'account_id': '927f9095-27e5-11eb-bb81-fa163ef85f63', 'account_name': '1001000002', 'cl_ord_id': 'fe0ec2d3-2d50-11eb-9e36-fa163ef85f63', 'symbol': 'SHSE.510300', 'side': 1, 'position_effect': 1, 'order_type': 1, 'status': 10, 'price': 5.0, 'order_style': 1, 'volume': 20000, 'created_at': datetime.datetime(2020, 11, 23, 13, 59, 4,794594, tzinfo=tzfile('PRC')), 'updated_at': datetime.datetime(2020, 11, 23, 13, 59, 4, 795571, tzinfo=tzfile('PRC')), 'algo_name': 'ATS-SMART', 'algo_param': 'start_time&&1606093200||end_time_referred&&1606114500||end_time&&1606114500||end_time_valid&&1||stop_sell_when_dl&&1||cancel_when_pl&&0||min_trade_amount&&100000', 'order_id': '', 'ex_ord_id': '', 'position_side': 0, 'order_business': 0, 'order_duration': 0, 'order_qualifier': 0, 'order_src': 0, 'position_src': 0, 'ord_rej_reason': 0, 'ord_rej_reason_detail': '', 'stop_price': 0.0, 'value': 0.0, 'percent': 0.0, 'target_volume': 0, 'target_value': 0.0, 'target_percent': 0.0, 'filled_volume': 0, 'filled_vwap': 0.0, 'filled_amount': 0.0, 'filled_commission': 0.0, 'algo_status': 0, 'algo_comment': ''}]

algo_order_pause 暂停或重启或者撤销算法委托

函数原型:

  1. algo_order_pause(alorders)

参数:

参数 类型 说明
alorders str 传入单个字典. 或者list字典. 每个字典包含key:cl_ord_id, key:account_id key:algo_status

cl_ord_id为委托id, account_id为账户id,algo_status为算法单状态(1 - 重启 2 - 暂停 3 -暂停并撤子单)

返回值:

类型 说明
list[order] 委托对象列表,参见委托
  1. aorders = get_algo_orders(account='')
  2. # 暂停订单,修改订单结构的母单状态字段
  3. alorders01 = [{'cl_ord_id': aorders[0]['cl_ord_id'], 'account_id': aorders[0]['account_id'], 'algo_status': 3}]
  4. algo_order_pause(alorders01)

注意: ATS-SMART算法暂不支持此接口

get_algo_child_orders 查询算法委托的所有子单

函数原型:

  1. get_algo_child_orders(cl_ord_id, account='')

参数:

参数 类型 说明
cl_ord_id str 传入单个字典. 或者list字典. 每个字典包含key:cl_ord_id
account str account_id 默认帐号时为 ‘’

返回值:

类型 说明
list[order] 委托对象列表,参见委托

示例:

  1. aorders = get_algo_orders(account='')
  2. child_order= get_algo_child_orders(aorders[0]['cl_ord_id'], account='')
  3. print(child_order[0])

输出:

  1. [{'account_id': '17ceec74-2efb-11eb-b437-00ff5a669ee2', 'account_name': '0000001', 'cl_ord_id': '1606294231_9', 'order_id': '1606294231_9', 'symbol': 'SZSE.000001', 'side': 1, 'position_effect': 1, 'order_type': 1, 'status': 3, 'price': 19.06, 'volume': 100, 'filled_volume': 100, 'filled_vwap': 19.06, 'filled_amount': 1905.9999999999998, 'algo_order_id': '453b3064-2efb-11eb-b437-00ff5a669ee2', 'strategy_id': '', 'ex_ord_id': '', 'position_side': 0, 'order_business': 0, 'order_duration': 0, 'order_qualifier': 0, 'order_src': 0, 'ord_rej_reason': 0, 'ord_rej_reason_detail': '', 'stop_price': 0.0, 'order_style': 0, 'value': 0.0, 'percent': 0.0, 'target_volume': 0, 'target_value': 0.0, 'target_percent': 0.0, 'filled_commission': 0.0, 'created_at': None, 'updated_at': None}]

on_algo_order_status 算法单状态事件

响应算法单状态更新事情,下算法单后状态更新时被触发

函数原型:

  1. on_algo_order_status(context, algo_order)

参数:

参数名 类型 说明
context context 上下文
algo_order order 委托

示例:

  1. def on_algo_order_status(context, algo_order):
  2. print(algo_order)

输出:

  1. {'strategy_id': '6f534238-2883-11eb-a8fe-fa163ef85f63', 'account_id': '927f9095-27e5-11eb-bb81-fa163ef85f63', 'account_name': '1001000002', 'cl_ord_id': '09baa735-2e01-11eb-ab6f-fa163ef85f63','symbol': 'SHSE.600000', 'side': 1, 'position_effect': 1,'order_type': 1, 'status':1, 'price': 5.0, 'order_style': 1, 'volume': 20000, 'created_at':datetime.datetime(2020,11, 24, 10, 59, 15, 800453, tzinfo=tzfile('PRC')), 'updated_at': datetime.datetime(2020, 11, 24, 10, 59, 17, 922523, tzinfo=tzfile('PRC')), 'algo_name': 'ATS-SMART', 'algo_param': 'start_time&&1606179600||end_time_referred&&1606200900||end_time&&1606200900||end_time_valid&&1|stop_sell_when_dl&&1||cancel_when_pl&&0||min_trade_amount&&100000', 'order_id': '', 'ex_ord_id':'', 'position_side': 0, 'order_business': 0, 'order_duration': 0, 'order_qualifier':0, 'order_src': 0, 'position_src': 0, 'ord_rej_reason': 0, 'ord_rej_reason_detail': '', 'stop_price': 0.0, 'value': 0.0, 'percent': 0.0, 'target_volume': 0, 'target_value': 0.0, 'target_percent': 0.0, 'filled_volume': 0, 'filled_vwap': 0.0, 'filled_amount': 0.0, 'filled_commission': 0.0, 'algo_status':0, 'algo_comment': ''}
0 篇笔记