Skip to content

交易函数

分钟撮合前请确保已下载对应标的的分钟线数据,否则下单可能失败(返回 None)。

按数量买卖标的

order**- 按数量买卖标的**

python
order(security, amount, style=None, side='long', close_today=False)

功能说明 按“数量(股/份/手)”下单买卖标的。成功返回 order_id(str),失败返回 None。 可通过 get_orders(order_id) 查看订单详情,通过 get_trades() 查看成交。

参数

参数名类型必填说明
securitystr标的代码,支持股票/期货/场内基金/可转债(以你接入的行情与交易品种为准)
amountint交易数量:正数买入,负数卖出
styleOrderStyle / None下单方式:MarketOrderStyle(0) 市价;LimitOrderStyle(limit_price) 限价;默认市价
sidestr'long'/'short'。股票/ETF:当前版本传入 'short' 会被忽略,仍按 'long' 执行;期货按多空生效
close_todaybool期货专用:影响平今/平昨手续费计算。股票忽略

返回

  • 成功:order_id: str(示例:ZLT-0000000-sh600000)
  • 失败:None

失败常见原因(回测/分钟撮合实测相关)

  • 标的代码错误/未上市/已退市
  • 停牌(或当根bar不可交易)
  • 股票尝试卖出但 closeable_amount 不足(A股 T+1:当日买入不可卖)
  • 限价单价格不在涨跌停允许范围内(如低于 low_limit 或高于 high_limit)会直接创建失败返回 None

举例(实测输出片段)

python
oid = order('sh600000', 100)
print(oid)
print(get_orders(oid))
print(get_trades())
order_id: ZLT-0000000-sh600000
get_orders(order_id): UserOrder({'order_id':ZLT-0000000-sh600000,'status':9,'add_time':2025-01-02 09:32:00,'security':sh600000,'amount':100,'filled':100,'side':long,'action':open,'price':10.38,'commission':5.0,'style':MarketOrderStyle: _limit_price=0.0,'profit':0.0})
get_trades(): {'ZLT-0000000-sh600000': Trade({'trade_id': ZLT-0000000-sh600000-1, 'order_id': ZLT-0000000-sh600000, 'time': 2025-01-02 09:32:00, 'amount': 100, 'price': 10.38})}

调整标的数量到目标值

order_target - 调整标的数量到目标值

python
order_target(security, amount, style=None, side='long', close_today=False)

功能说明 将标的最终持仓数量调整到 amount(目标数量)。

  • amount > 当前持仓:会买入差额
  • amount < 当前持仓:会卖出差额(需要可卖数量足够)

参数

参数名类型必填说明
securitystr标的代码
amountint目标最终数量(股/份/手)
styleOrderStyle / None市价/限价,默认市价
sidestr'long'/'short'(股票/ETF:'short' 会被忽略)
close_todaybool期货专用

返回

  • 成功:order_id: str
  • 失败:None

重要说明:A股 T+1(实测确认)

  • 股票当日买入后 closeable_amount=0,当日无法卖出。
  • 当 order_target 需要卖出但 closeable_amount 不足时,函数可能返回 None。

举例:次日可卖出时将仓位调到 0(实测输出片段)

python
pos = context.portfolio.positions.get('sh600000')
print('closeable_amount:', pos.closeable_amount)
oid = order_target('sh600000', 0)
print(oid)
print(get_orders(oid))
[Day2] closeable_amount: 2000
order_id: ZLT-0000003-sh600000
UserOrder({'order_id':ZLT-0000003-sh600000,'status':9,'add_time':2025-01-03 09:32:00,'security':sh600000,'amount':2000,'filled':2000,'side':long,'action':close,'price':10.1,'commission':16.16,'style':MarketOrderStyle: _limit_price=0.0,'profit':-617.0})

调整标的仓位到目标价值

order_target_value - 调整标的仓位到目标价值

python
order_target_value(security, value, style=None, side='long', close_today=False)

功能说明 将标的最终持仓“市值/名义价值”调整到 value。系统会按最新价把目标价值换算成目标数量,并下单调整差额。

参数

参数名类型必填说明
securitystr标的代码
valuefloat / int目标最终价值(元)。股票为目标市值;期货为名义/保证金口径以平台实现为准
styleOrderStyle / None市价/限价,默认市价
sidestr'long'/'short'(股票/ETF:'short' 会被忽略)
close_todaybool期货专用

返回

  • 成功:order_id: str
  • 失败:None

股票价值换算规则(实测确认,关键!)

  • A股按 100 股一手向下取整: target_shares = floor(value / price / 100) * 100
  • 这意味着:目标价值换算后不会精确到 1 股,会“少一点点”,属于正常现象。

举例(实测输出片段)

python
oid = order_target_value('sh600000', 20000)
print(get_orders(oid))
UserOrder({'order_id':ZLT-0000002-sh600000,'status':9,'add_time':2025-01-02 09:34:00,'security':sh600000,'amount':1900,'filled':1900,'side':long,'action':open,'price':10.41,'commission':5.93,'style':MarketOrderStyle: _limit_price=0.0,'profit':0.0})

按指定价值买卖标的

order_value - 按指定价值买卖标的

python
order_value(security, value, style=None, side='long', close_today=False)

功能说明 按“价值金额(元)”下单。系统按最新价把 value 换算成数量后下单。

  • value > 0:买入指定价值
  • value < 0:卖出指定价值(需可卖数量足够)

参数

参数名类型必填说明
securitystr标的代码
valuefloat / int下单价值(元)
styleOrderStyle / None市价/限价,默认市价
sidestr'long'/'short'(股票/ETF:'short' 会被忽略)
close_todaybool期货专用

返回

  • 成功:order_id: str
  • 失败:None

举例(实测输出片段)

python
oid = order_value('sh600000', 10000)
print(get_orders(oid))
UserOrder({'order_id':ZLT-0000003-sh600000,'status':9,'add_time':2025-01-02 09:35:00,'security':sh600000,'amount':900,'filled':900,'side':long,'action':open,'price':10.4,'commission':5.0,'style':MarketOrderStyle: _limit_price=0.0,'profit':0.0})

获取当天未完成委托

get_open_orders()

获取当天所有未完成状态的委托订单详情(未成交/部分成交/已报未结等)。返回 dict,key 为 order_id,value 为订单对象(实测为 UserOrder)。

函数定义

python
get_open_orders()

参数

返回

返回一个 dict:

  • key:order_id: str
  • value:UserOrder(订单对象)

订单对象字段(UserOrder)

字段名字段类型说明
order_idstr订单编号(示例:ZLT-0000039-sh600000)
statusint订单状态码(见下方 status 说明)
add_timedatetime.datetime委托创建时间(策略时间)
securitystr标的代码
amountint委托数量(股票为股)
filledint已成交数量
sidestrlong/short
actionstropen/close
pricefloat委托价(限价单为限价;市价单常回填为成交价)
commissionfloat手续费(未成交通常为 0.0)
styleOrderStyle下单方式对象(MarketOrderStyle / LimitOrderStyle)
profitfloat平台回填字段(未成交多为 0.0)

举例

python
opens = get_open_orders()
print(opens)
# 输出示例:
# {
#   'ZLT-0000039-sh600000': UserOrder({
#       'order_id':ZLT-0000039-sh600000,'status':5,'add_time':2025-09-02 09:30:00,
#       'security':sh600000,'amount':100,'filled':0,'side':long,'action':open,
#       'price':12.08,'commission':0.00,'style':LimitOrderStyle: _limit_price=12.08,'profit':0.00
#   })
# }

获取委托详情

get_orders-获取指定订单对象 / 当天所有订单委托详情。

函数定义

python
get_orders(order_id=None)

参数

参数名类型必填说明
order_idstr / None订单编号。传入则返回该订单对象;不传则返回当天所有订单 dict

返回

  • order_id 传入:返回 UserOrder(或 None,若找不到)
  • order_id 不传:返回 dict[str, UserOrder](当天所有订单,含已成交、未成交等)

举例 1:获取当天所有订单

python
all_orders = get_orders()
print(all_orders)
# 输出示例:
# {
#   'ZLT-0000038-sh600000': UserOrder({'order_id':ZLT-0000038-sh600000,'status':9,'add_time':2025-09-02 09:30:00,'security':sh600000,'amount':100,'filled':100,'side':long,'action':open,'price':13.52,'commission':5.00,'style':MarketOrderStyle: _limit_price=0.0,'profit':0.0}),
#   'ZLT-0000039-sh600000': UserOrder({'order_id':ZLT-0000039-sh600000,'status':5,'add_time':2025-09-02 09:30:00,'security':sh600000,'amount':100,'filled':0,'side':long,'action':open,'price':12.08,'commission':0.00,'style':LimitOrderStyle: _limit_price=12.08,'profit':0.0})
# }

举例 2:获取单笔订单

python
oid = 'ZLT-0000038-sh600000'
od = get_orders(oid)
print(od)
# 输出示例:
# UserOrder({'order_id':ZLT-0000038-sh600000,'status':9,'add_time':2025-09-02 09:30:00,'security':sh600000,'amount':100,'filled':100,'side':long,'action':open,'price':13.52,'commission':5.0,'style':MarketOrderStyle: _limit_price=0.0,'profit':0.0})

订单 status 状态码

status含义典型表现
5未完成(已报/未成交)限价单挂出后 filled=0,会出现在 get_open_orders()
9已完成(已成交)市价单当根撮合成交后 filled=amount,不会出现在 get_open_orders()

获取当天成交详情

get_trades()-获取当天成交明细。

返回 dict,key 为 order_id,value 为 Trade 对象。

函数定义

python
get_trades()

参数

返回

返回一个 dict:

  • key:str(实测为 order_id,如 ZLT-0000038-sh600000
  • value:Trade(成交对象)

Trade 字段示例(实测输出)

字段名字段类型说明
trade_idstr成交编号(示例:ZLT-0000038-sh600000-1)
order_idstr所属订单编号
timedatetime.datetime成交时间(策略时间)
amountint成交数量
pricefloat成交价格

举例

python
trades = get_trades()
print(trades)
# 输出示例:
# {
#   'ZLT-0000038-sh600000': Trade({
#       'trade_id': ZLT-0000038-sh600000-1,
#       'order_id': ZLT-0000038-sh600000,
#       'time': 2025-09-02 09:30:00,
#       'amount': 100,
#       'price': 13.52
#   })
# }

注意事项

  • 该接口返回的是当日成交,未成交订单不会出现在返回结果中。
  • key 实测为 order_id;若后续版本改为 trade_id,请以实测结果更新文档。

撤单

cancel_order(order)-撤销未完成委托订单。

函数定义

python
cancel_order(order)

参数

参数名类型必填说明
orderUserOrder撤单对象。实测在当前版本中,应传入 get_open_orders() 返回的 UserOrder 对象

返回

  • 实测返回:None(无显式返回值)

举例

python
opens = get_open_orders()
print("before cancel, get_open_orders() =>", opens)
# before cancel, get_open_orders() =>
#   ZLT-0000039-sh600000 => UserOrder({'order_id':ZLT-0000039-sh600000,'status':5,'add_time':2025-09-02 09:30:00,'security':sh600000,'amount':100,'filled':0,'side':long,'action':open,'price':12.08,'commission':0.00,'style':LimitOrderStyle: _limit_price=12.08,'profit':0.00})

any_oid = list(opens.keys())[0]
any_order = opens[any_oid]
print("cancel target:", any_oid, any_order)
#cancel target: ZLT-0000039-sh600000 UserOrder({'order_id':ZLT-0000039-sh600000,'status':5,'add_time':2025-09-02 
ret = cancel_order(any_order)
print("cancel_order return:", ret)
 # cancel_order return: None

print("after cancel, get_open_orders() =>", get_open_orders())
print("get_orders() =>", get_orders())
# after cancel, get_open_orders() =>
#   ZLT-0000039-sh600000 => UserOrder({'order_id':ZLT-0000039-sh600000,'status':5,'add_time':2025-09-02 09:30:00,'security':sh600000,'amount':100,'filled':0,'side':long,'action':open,'price':12.08,'commission':0.00,'style':LimitOrderStyle: _limit_price=12.08,'profit':0.00})

账户出入金

inout_cash(cash)-账户出入金

账户转入或转出资金。cash 为正表示入金,cash 为负表示出金。

函数定义

python
inout_cash(cash)

参数

参数名类型必填说明
cashfloat / int出入金金额。正数为入金,负数为出金

返回

  • None(无显式返回值)

举例

python
before_cash = context.portfolio.available_cash
before_total = context.portfolio.total_value
print("before: available_cash=", before_cash, "total_value=", before_total)
# before: available_cash= 1256142.0 total_value= 1283602.0
inout_cash(10000)
print("inout_cash(+10000) called")
# inout_cash(+10000) called
mid_cash = context.portfolio.available_cash
mid_total = context.portfolio.total_value
print("after +10000: available_cash=", mid_cash, "total_value=", mid_total)
# after +10000: available_cash= 1266142.0 total_value= 1293602.0
inout_cash(-5000)
print("inout_cash(-5000) called")
# inout_cash(-5000) called
after_cash = context.portfolio.available_cash
after_total = context.portfolio.total_value
print("after -5000: available_cash=", after_cash, "total_value=", after_total)
# after -5000: available_cash= 1271142.0 total_value= 1298602.0

文档版本: 1.0.0 | 发布于 2025-01-29