5.【PTrade使用指南】- 公共交易函数

量化软件 2025-02-03 5461
量化软件:恒生PTrade使用指南 5.【PTrade使用指南】- 公共交易函数  第1张
5.【PTrade使用指南】- 公共交易函数  第2张

公共交易函数

order_tick - tick行情触发买卖

order_tick(sid, amount, priceGear='1', limit_price=None)

使用场景

该函数仅在交易模块可用

接口说明

该接口用于在tick_data模块中进行买卖股票下单,可设定价格档位进行委托。

注意事项:


该函数只能在tick_data模块中使用

参数

sid:股票代码(str);

amount:交易数量,正数表示买入,负数表示卖出(int)

priceGear:盘口档位,level1:1~5买档/-1~-5卖档,level2:1~10买档/-1~-10卖档(str)

limit_price:买卖限价,当输入参数中也包含priceGear时,下单价格以limit_price为主(float);

返回

返回一个委托流水编号(str)

示例

def initialize(context):g.security = "600570.SS"set_universe(g.security)def tick_data(context,data):security = g.security
current_price = eval(data[security]['tick']['bid_grp'][0])[1][0]if current_price > 56 and current_price < 57:# 以买一档下单order_tick(g.security, -100, "1")# 以卖二档下单order_tick(g.security, 100, "-2")# 以指定价格下单order_tick(g.security, 100, limit_price=56.5)def handle_data(context, data):pass

cancel_order - 撤单

cancel_order(order_param)

使用场景

该函数仅在回测、交易模块可用

接口说明

该接口用于取消订单,根据Order对象或order_id取消订单。

注意事项:


参数

order_param: Order对象或者order_id(Order/str)

返回

None

示例

def initialize(context):g.security = '600570.SS'set_universe(g.security)def handle_data(context, data):_id = order(g.security, 100)cancel_order(_id)log.info(get_order(_id))

cancel_order_ex - 撤单

cancel_order_ex(order_param)

使用场景

该函数仅在交易模块可用

接口说明

该接口用于取消订单,根据get_all_orders返回列表中的单个字典取消订单。

注意事项:


该函数仅可撤get_all_orders函数返回的可撤状态订单。

账户多个交易运行时调用该函数会撤销其他交易产生的订单,可能对其他正在运行的交易策略产生影响。

参数

order_param:  get_all_orders函数返回列表的单个字典(dict)

返回

None

示例

def initialize(context):g.security = '600570.SS'set_universe(g.security)g.count = 0def handle_data(context, data):if g.count == 0:log.info("当日全部订单为:%s" % get_all_orders())# 遍历账户当日全部订单,对已报、部成状态订单进行撤单操作for _order in get_all_orders():if _order['status'] in ['2', '7']:
  cancel_order_ex(_order)if g.count == 1:# 查看撤单是否成功log.info("当日全部订单为:%s" % get_all_orders())g.count += 1

debt_to_stock_order - 债转股委托

debt_to_stock_order(security, amount)

使用场景

该函数仅在交易模块可用

接口说明

该接口用于可转债转股操作。

注意事项:


参数

security: 可转债代码(str)

amount: 委托数量(int)

返回

Order对象中的id或者None。如果创建订单成功,则返回Order对象的id,失败则返回None(str)。

示例

def initialize(context):g.security = "600570.SS"set_universe(g.security)def before_trading_start(context, data):g.count = 0def handle_data(context, data):if g.count == 0:# 对持仓内的国贸转债进行转股操作debt_to_stock_order("110033.SS", -1000)g.count += 1# 查看委托状态log.info(get_orders())g.count += 1

get_open_orders - 获取未完成订单

get_open_orders(security=None)

使用场景

该函数仅在回测、交易模块可用

接口说明

该接口用于获取当天所有未完成的订单,或按条件获取指定未完成的订单。

注意事项:


参数

security:标的代码,如'600570.SS',不传时默认为获取所有未成交订单(str);

返回

返回一个list,该list中包含多个Order对象(list[Order,...])。

示例

def initialize(context):g.security = ['600570.SS', '000001.SZ']set_universe(g.security)def handle_data(context, data):for _sec in g.security:_id = order(_sec, 100, limit_price = 30)# 当运行周期为分钟则可获取本周期及之前所有未完成的订单dict_list = get_open_orders()log.info(dict_list)# 当运行周期为天,可在after_trading_end中调用此函数获取当天未完成的订单def after_trading_end(context, data):dict_list = get_open_orders()log.info(dict_list)

get_order - 获取指定订单

get_order(order_id)

使用场景

该函数仅在回测、交易模块可用

接口说明

该接口用于获取指定编号订单。

注意事项:


获取指定编号订单。

参数

order_id:订单编号(str)

返回

返回一个list,该list中只包含一个Order对象(list[Order])。

示例

def initialize(context):g.security = '600570.SS'set_universe(g.security)def handle_data(context, data):order_id = order(g.security, 100)current_order = get_order(order_id)log.info(current_order)

get_orders - 获取全部订单

get_orders(security=None)

使用场景

该函数仅在回测、交易模块可用

接口说明

该接口用于获取策略内所有订单,或按条件获取指定订单。

注意事项:


参数

security:标的代码,如'600570.SS',不传时默认为获取所有订单(str);

返回

返回一个list,该list中包含多个Order对象(list[Order,...])。

示例

def initialize(context):g.security = '600570.SS'set_universe(g.security)def handle_data(context, data):_id = order(g.security, 100)order_obj = get_orders()log.info(order_obj)

get_all_orders - 获取账户当日全部订单

get_all_orders(security=None)

使用场景

该函数仅在交易模块可用

接口说明

该接口用于获取账户当日所有订单(包含非本交易的订单记录),或按条件获取指定代码的订单。

注意事项:


1、该函数返回账户当日在柜台的全部委托记录,不能查询策略中待报、未报状态的委托。

2、该函数返回的可撤委托仅可通过cancel_order_ex函数进行撤单,且非本交易的委托进行撤单仅可通过本函数查询委托状态更新。

参数

security:标的代码,如'600570.SS',不传时默认为获取所有订单(str);

返回

返回一个list,该list中包含多条订单记录(list[dict, ...]):

[{'symbol': 股票代码(str), 'entrust_no': 委托编号(str), 'amount':  委托数量(int), 'entrust_bs': 委托方向(int), 'price': 委托价格(float),  'status': 委托状态(str), 'filled_amount': 成交数量(int)}, ...]

字段备注:

  • entrust_bs -- 成交方向,1-买,2-卖;

  • status -- 委托状态,详见Order对象中status字段;

示例

def initialize(context):g.security = '600570.SS'set_universe(g.security)def handle_data(context, data):# 获取账户当日委托600570代码的全部订单log.info('当日委托600570代码的全部订单:%s' % get_all_orders(g.security))# 获取账户当日全部订单log.info('当日全部订单:%s' % get_all_orders())

get_trades - 获取当日成交订单

get_trades()

使用场景

该函数仅在回测、交易模块可用

接口说明

该接口用于获取策略内当日已成交订单详情。

注意事项:


1、为减小对柜台压力,该函数在股票交易模块中同一分钟内多次调用返回当前分钟首次查询的缓存数据;

2、该接口会返回当日截止到当前时间段内的成交数据;

参数

返回

返回数据:

一笔订单对应一笔成交:{'订单编号': [ [成交编号,委托编号, 标的代码, 买卖类型,  成交数量, 成交价格, 成交金额, 成交时间]]},如下:

注意:标的代码尾缀为四位,上证为XSHG,深圳为XSHE,如需对应到代码请做代码尾缀兼容

{'ba6a80d9746347a99c050b29069807c7': [[5001, 700001, '600570.XSHG', '买', 100000, 86.60, 8660000.0, datetime.datetime(2021, 7, 13, 15, 0)]]}

一笔订单对应多笔成交:{'订单编号': [ [成交编号1,委托编号, 标的代码,  买卖类型,成交数量,成交价格,成交金额,成交时间],[成交编号2,委托编号, 标的代码,  买卖类型,成交数量,成交价格,成交金额,成交时间]]}(dict[str:list[list[int,int,str,str,int,int,numpy.float64,numpy.float64,datetime.datetime]]])

示例

def initialize(context):g.security = '600570.SS'set_universe(g.security)def handle_data(context, data):#获取当日成交数据trades_info = get_trades()log.info(trades_info)

get_position - 获取持仓信息

get_position(security)

使用场景

该函数仅在回测、交易模块可用

接口说明

该接口用于获取某个标的持仓信息详情。

注意事项:


参数

security:标的代码,如'600570.SS',不传时默认为获取所有持仓(str);

返回

返回一个Position对象(Position)。

示例

def initialize(context):g.security = '600570.SS'set_universe(g.security)def handle_data(context, data):position = get_position(g.security)log.info(position)
4.【PTrade使用指南】- 策略接口介绍
« 上一篇 2025-02-03
Python自动化炒股:使用Dash和Plotly构建交互式股票数据可视化应用的最佳实践
下一篇 » 2025-02-03