下单交易
place_oder 下单
TradeClient.place_order():
说明
交易下单接口。关于如何选择标的、订单类型、方向数量等,请见下方说明。请在运行程序前结合本文档的概述部分及FAQ-交易-支持的订单列表部分,检查您的账户是否支持所请求的订单,并检查交易规则是否允许在程序运行时段对特定标的下单。若下单失败,可首先阅读文档FAQ-交易部分排查
place_order
调用成功后,参数里的order对象的订单id即被填充(order.id
),可用于后续的查询或撤单,此时此方法会返回订单id,但此时只表示订单提交成功,不代表订单已被成功执行,订单的执行是异步的,提交订单后,订单会根据情况进入下一步状态,比如成交、被拒绝等。因此,建议在对订单进行下一步操作之前,使用get_order
或get_orders
方法查询对应订单的状态,
CAUTION
1、市价单(MKT)和止损单(STP)不支持盘前盘后阶段交易,在调用下单接口时,需要把 outside_rth 设置为 false
2、对于可做空标的,暂不支持锁仓功能,因此无法同时持有相同标的多头和空头持仓
3、附加订单的主订单类型暂时仅支持限价单
4、市价单(MKT)和模拟账号,对'time_in_force'参数都不支持GTC
5、模拟账号暂不支持窝轮和牛熊证的订单
订单状态说明
如何判断综合和模拟账号的部分成交状态?
当订单状态不是FILLED(有可能是NEW,CANCELLED,EXPIRED,REJECTED其中一种)时,都有可能是部分成交的状态,可以通过订单成交数量是否大于0来判断
如何判断环球账号部分成交状态?
订单状态是 FILLED,且订单成交数量大于0
订单状态变化流程:
其他说明
- 指数期权除 IWM/SPY/QQQ外,不支持非周期权的交易
- 不可以直接开反向仓位,如持仓100股,直接卖出200股是不允许的,需先平仓
参数
Order
对象 (tigeropen.trade.domain.order.Order
)
可用 tigeropen.common.util.order_utils
下的工具函数,如 limit_order(), market_order(), 根据您需要的具体订单类型和参数,在本地生成订单对象。创建方法详见Order对象-构建方法部分 或者用 TradeClient.create_order()
向服务端请求订单号,然后生成订单对象(不推荐)
参数
参数 | 类型 | 描述 | 市价单 | 限价单 | 止损单 | 止损限价单 | 跟踪止损单 |
---|---|---|---|---|---|---|---|
account | str | 用户授权账户:402901 | |||||
order_id | int | 订单编号,作用是防止重复下单。可以通过订单号接口获取。如果传0,则服务器端会自动生成订单编号,传0时无法防止重复下单,请谨慎选择 | 选填 | 选填 | 选填 | 选填 | 选填 |
symbol | str | 股票代码 如:AAPL;(sec_typ为窝轮牛熊证时,在app窝轮/牛熊证列表中名称下面的5位数字) | |||||
sec_type | str | 合约类型 (STK 股票 OPT 美股期权 WAR 港股窝轮 IOPT 港股牛熊证 FUT 期货) | |||||
action | str | 交易方向 BUY/SELL | |||||
order_type | str | 订单类型. MKT(市价单), LMT(限价单), STP(止损单), STP_LMT(止损限价单), TRAIL(跟踪止损单) | MKT | LMT | STP | STP_LMT | TRAIL |
quantity | int | 订单数量(港股,沪港通,窝轮,牛熊证有最小数量限制) | |||||
quantity_scale | int | 下单数量的偏移量,默认为0。碎股 quantity 和 quantity_scale 结合起来代表真实下单数量,如 quantity=111 quantity_scale=2,那么真实 quantity=111*10^(-2)=1.11 | |||||
total_cash_amount | float | 下单总金额,按股数下单时为 None | |||||
filled_cash_amount | float | 已成交金额,按股数下单时为 None | |||||
refund_cash_amount | float | 退回金额,等于下单总金额-已成交金额。按股数下单或订单未终结时为 None | |||||
limit_price | float | 限价,当 order_type 为LMT,STP_LMT时该参数必需 | |||||
aux_price | float | 股票订单止损触发价。含义为价差,与trailing_percent同时存在时被trailing_percent覆盖。当 order_type 为STP,STP_LMT时该参数必需,当 order_type 为 TRAIL时,为跟踪额 | 选填 | ||||
trailing_percent | float | 跟踪止损单-止损百分比 。当 order_type 为 TRAIL时,aux_price和trailing_percent两者互斥,优先使用trailing_percent | 选填 | ||||
outside_rth | bool | true: 允许盘前盘后交易(美股专属), false: 不允许,默认为允许。(市价单、止损单、跟踪止损单只在盘中有效,将忽略outside_rth参数) | 选填 | 选填 | 选填 | ||
adjust_limit | float | 价格微调幅度(默认为0表示不调整,正数为向上调整,负数向下调整),对传入价格自动调整到合法价位上。例如:0.001 代表向上调整且幅度不超过 0.1%;-0.001 代表向下调整且幅度不超过 0.1%。默认 0 表示不调整 | 选填 | 选填 | 选填 | 选填 | |
market | str | 市场 (美股 US 港股 HK 沪港通 CN) | 选填 | 选填 | 选填 | 选填 | 选填 |
currency | str | 货币(美股 USD 港股 HKD 沪港通 CNH) | 选填 | 选填 | 选填 | 选填 | 选填 |
time_in_force | str | 订单有效期,只能是 DAY(当日有效)、GTC(取消前有效,最长有效时间180天)、GTD(在指定时间前有效),默认为DAY | 选填 | 选填 | 选填 | 选填 | 选填 |
expire_time | int | 订单有效的截止时间, 13位的时间戳,精确到秒(time_in_force为GTD时为必填,其他类型时无效) | 选填 | 选填 | 选填 | ||
exchange | str | 交易所 (美股 SMART 港股 SEHK 沪港通 SEHKNTL 深港通 SEHKSZSE)否 | 选填 | 选填 | 选填 | 选填 | 选填 |
expiry | str | 过期日(期权、窝轮、牛熊证专属) | 选填 | 选填 | 选填 | 选填 | 选填 |
strike | str | 底层价格(期权、窝轮、牛熊证专属) | 选填 | 选填 | 选填 | 选填 | 选填 |
right | str | 期权方向 PUT/CALL(期权、窝轮、牛熊证专属) | 选填 | 选填 | 选填 | 选填 | 选填 |
multiplier | float | 1手单位(期权、窝轮、牛熊证专属) | 选填 | 选填 | 选填 | 选填 | 选填 |
local_symbol | str | 窝轮牛熊证该字段必填,在app窝轮/牛熊证列表中名称下面的5位数字 | 选填 | 选填 | 选填 | 选填 | 选填 |
secret_key | str | 机构用户专用,交易员密钥 | 选填 | 选填 | 选填 | 选填 | 选填 |
user_mark | str | 下单备注信息,下单后不能修改,查询订单时返回userMark信息 | 选填 | 选填 | 选填 | 选填 | 选填 |
- 附加订单参数
附加订单(Attached Order )是指能通过附加的子订单对主订单起到止盈或止损效果的订单,可以附加的子订单类型有限价单(可用于止盈)、止损限价单/止损单(可用于止损)。通过增加以下参数可以实现附加订单
参数 | 类型 | 描述 | 附加止损 | 附加止盈 | 附加跟踪止损 | 附加括号 |
---|---|---|---|---|---|---|
attach_type | str | 附加订单类型,下附加订单时必填。(order_type应为LMT): PROFIT-止盈单,LOSS-止损单,BRACKETS-括号订单(包含止盈单和止损单,综合账号暂不支持附加括号单,即不支持同时附加止损和止盈单) | ||||
profit_taker_order_id | int | 止盈单编号,可以通过订单号接口获取。如果传0,则服务器端会自动生成止盈单编号 | 选填 | 选填 | 选填 | |
profit_taker_price | float | 止盈单价格,下止盈单时必填 | ||||
profit_taker_tif | str | 同time_in_force字段,订单有效期,只能是 DAY(当日有效)和GTC(取消前有效),下止盈单时必填 | ||||
profit_taker_rth | boolean | 同outside_rth字段 | ||||
stop_loss_order_id | int | 止损单编号,可以通过订单号接口获取。如果传0,则服务器端会自动生成止损单编号 | ||||
stop_loss_price | float | 止损单价格(止损单的触发价),下止损单时必填 | ||||
stop_loss_limit_price | float | 止损单的执行限价(暂只对综合账号有效)。止损单的限价没有填写时,为附加止损市价单;填写时为附加止损限价单,当主订单交易方向为 BUY时,需要满足stop_loss_price > stop_loss_limit_price,反之主订单交易方向为 SELL需要满足stop_loss_price < stop_loss_limit_price | 选填 | 选填 | ||
stop_loss_tif | str | 同time_in_force字段,订单有效期,只能是 DAY(当日有效)和GTC(取消前有效),下止损单时必填 | ||||
stop_loss_trailing_percent | float | 跟踪止损单-止损百分比,当下跟踪止损单时,止损百分比(stopLossTrailingPercent)和止损额(stopLossTrailingAmount)其中一项必填,如果都填时,会使用止损百分比作为参数。 | 选填 | 选填 | ||
stop_loss_trailing_amount | float | 跟踪止损单-止损额,当下跟踪止损单时,止损百分比(stopLossTrailingPercent)和止损额(stopLossTrailingAmount)其中一项必填,如果都填时,会使用止损百分比作为参数。 | 选填 | 选填 |
返回
如果下单成功则返回订单id,失败抛出异常。 若成功下单,则参数Order对象的id会被填充为实际的订单号
构建合约对象示例
from tigeropen.common.util.contract_utils import stock_contract, option_contract, option_contract_by_symbol, \
future_contract, war_contract_by_symbol, iopt_contract_by_symbol
# 美股
contract = stock_contract(symbol='TIGR', currency='USD')
# 港股
contract = stock_contract(symbol='00700', currency='HKD')
# 期权
contract = option_contract(identifier='AAPL 190118P00160000')
contract = option_contract_by_symbol('JD', expiry='20211015', strike=45.0, put_call='PUT', currency='USD')
# 期货
# 综合/模拟
from tigeropen.common.util.contract_utils import future_contract
contract = future_contract(symbol='CL2312', currency='USD')
# 环球
from tigeropen.common.util.contract_utils import future_contract
contract = future_contract(symbol='CL', currency='USD', expiry='20190328', multiplier=1.0, exchange='SGX')
# 港股窝轮
contract = war_contract_by_symbol('01810', '20221116', 14.52, 'CALL', local_symbol='14759', multiplier=2000,
currency='HKD')
# 港股牛熊证
contract = iopt_contract_by_symbol('02318', '20200420', 87.4, 'CALL', local_symbol='63379', currency='HKD')
限价单 (LMT)
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import limit_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成股票合约
contract = stock_contract(symbol='AAPL', currency='USD')
# 生成订单对象
order = limit_order(account=client_config.account, contract=contract, action='BUY', limit_price=0.1, quantity=1)
# 下单
oid = trade_client.place_order(order)
print(order)
# >>> Order({'account': '111111', 'id': 2498911111111111111, 'order_id': None, 'parent_id': None, 'order_time': None, 'reason': None, 'trade_time': None, 'action': 'BUY', 'quantity': 1, 'filled': 0, 'avg_fill_price': 0, 'commission': None, 'realized_pnl': None, 'trail_stop_price': None, 'limit_price': 0.1, 'aux_price': None, 'trailing_percent': None, 'percent_offset': None, 'order_type': 'LMT', 'time_in_force': None, 'outside_rth': None, 'order_legs': None, 'algo_params': None, 'secret_key': None, 'contract': AAPL/STK/USD, 'status': 'NEW', 'remaining': 1})
print(order.status) # 订单状态
print(order.reason) # 若下单失败,reason为失败原因
# 若下单成功,则 order.id 为订单的id,此后可用该id查询订单或撤单
my_order = trade_client.get_order(id=order.id)
oid = trade_client.cancel_order(id=order.id)
# 或操作order对象进行改单
trade_client.modify_order(order, limit_price=190.5)
市价单 (MKT)
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import market_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成股票合约
contract = stock_contract(symbol='AAPL', currency='USD')
# 生成订单对象
order = market_order(account=client_config.account, contract=contract, action='BUY', quantity=1)
# 下单
oid = trade_client.place_order(order)
止损单 (STP)
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import stop_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成股票合约
contract = stock_contract(symbol='AAPL', currency='USD')
# 生成订单对象
order = stop_order(account=client_config.account, contract=contract, action='SELL', aux_price=1, quantity=1)
# 下单
oid = trade_client.place_order(order)
限价止损单(STP_LMT)
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import stop_limit_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成股票合约
contract = stock_contract(symbol='AAPL', currency='USD')
# 生成订单对象
order = stop_limit_order(account=client_config.account, contract=contract, action='SELL', limit_price=200.0, aux_price=180.0, quantity=1)
# 下单
oid = trade_client.place_order(order)
移动止损单(TRAIL)
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import trail_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成股票合约
contract = stock_contract(symbol='AAPL', currency='USD')
# 生成订单对象
order = trail_order(account=client_config.account, contract=contract, action='SELL', quantity=1, trailing_percent=8.0, )
# 下单
oid = trade_client.place_order(order)
附加订单
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import limit_order, limit_order_with_legs, order_leg, algo_order_params, \
algo_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 限价单 + 附加订单 (仅主订单为限价单时支持附加订单)
stop_loss_order_leg = order_leg('LOSS', 8.0, time_in_force='GTC') # 附加止损
stop_loss_order_leg1 = order_leg('LOSS', 8.0, limit_price=7.5) # 附加止损限价单
stop_loss_order_leg2 = order_leg('LOSS', 8.0, trailing_percent=0.8) # 附加止损追踪单(按比例)
stop_loss_order_leg3 = order_leg('LOSS', 8.0, trailing_amount=2.0) # 附加止损追踪单(按金额)
profit_taker_order_leg = order_leg('PROFIT', 12.0, time_in_force='GTC') # 附加止盈
main_order = limit_order_with_legs(account, contract, 'BUY', 100, limit_price=10.0,
order_legs=[profit_taker_order_leg, stop_loss_order_leg])
oid = trade_client.place_order(main_order)
# 查询主订单所关联的附加订单
order_legs = trade_client.get_open_orders(account, parent_id=main_order.id)
print(order_legs)
主订单+附加止损单
附加的止损单可以为市价单,限价单,追踪止损单
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import limit_order, limit_order_with_legs, order_leg, algo_order_params, \
algo_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 限价单 + 附加订单 (仅主订单为限价单时支持附加订单)
stop_loss_order_leg = order_leg('LOSS', 8.0, time_in_force='GTC') # 附加止损市价单
stop_loss_order_leg1 = order_leg('LOSS', 8.0, limit_price=7.5) # 附加止损限价单
stop_loss_order_leg2 = order_leg('LOSS', 8.0, trailing_percent=0.8) # 附加止损追踪单(按比例)
stop_loss_order_leg3 = order_leg('LOSS', 8.0, trailing_amount=2.0) # 附加止损追踪单(按金额)
main_order = limit_order_with_legs(account, contract, 'BUY', 100, limit_price=10.0,
order_legs=[stop_loss_order_leg])
oid = trade_client.place_order(main_order)
# 查询主订单所关联的附加订单
order_legs = trade_client.get_open_orders(account, parent_id=main_order.id)
print(order_legs)
主订单+附加止盈单
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import limit_order, limit_order_with_legs, order_leg, algo_order_params, \
algo_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 限价单 + 附加订单 (仅主订单为限价单时支持附加订单)
profit_taker_order_leg = order_leg('PROFIT', 12.0, time_in_force='GTC') # 附加止盈
main_order = limit_order_with_legs(account, contract, 'BUY', 100, limit_price=10.0,
order_legs=[profit_taker_order_leg])
oid = trade_client.place_order(main_order)
# 查询主订单所关联的附加订单
order_legs = trade_client.get_open_orders(account, parent_id=main_order.id)
print(order_legs)
主订单+附加括号单
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import limit_order, limit_order_with_legs, order_leg, algo_order_params, \
algo_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 限价单 + 附加订单 (仅主订单为限价单时支持附加订单)
stop_loss_order_leg = order_leg('LOSS', 8.0, time_in_force='GTC') # 附加止损
profit_taker_order_leg = order_leg('PROFIT', 12.0, time_in_force='GTC') # 附加止盈
main_order = limit_order_with_legs(account, contract, 'BUY', 100, limit_price=10.0,
order_legs=[profit_taker_order_leg, stop_loss_order_leg])
oid = trade_client.place_order(main_order)
# 查询主订单所关联的附加订单
order_legs = trade_client.get_open_orders(account, parent_id=main_order.id)
print(order_legs)
OCA 订单
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import order_leg, oca_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
contract = stock_contract(symbol='BILI', currency=Currency.USD)
# stop_loss_order_leg = order_leg('STP', price=8.0)
stop_loss_order_leg = order_leg('STP_LMT', price=8.0, limit_price=7.5)
profit_taker_order_leg = order_leg('LMT', limit_price=30.0)
my_oca_order = oca_order(client_config.account, contract, 'BUY',
order_legs=[stop_loss_order_leg, profit_taker_order_leg],
quantity=1000)
trade_client.place_order(my_oca_order)
算法订单
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import limit_order, limit_order_with_legs, order_leg, algo_order_params, \
algo_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 算法订单
contract = stock_contract(symbol='AAPL', currency='USD')
# TWAP 不支持 participation_rate 参数
params = algo_order_params(start_time='2022-01-19 23:00:00', end_time='2022-11-19 23:50:00', participation_rate=0.1)
order = algo_order(account, contract, 'BUY', 1000, 'VWAP', algo_params=params, limit_price=100.0)
oid = trade_client.place_order(order)
竞价单
auction_limit_order 竞价限价单auction_market_order 竞价市价单
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import market_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成股票合约
contract = stock_contract(symbol='01810', currency='HKD')
# 竞价限价单, time_in_force 设置为 OPG 表示参与盘前竞价,盘前没成交的盘中时间继续挂单,不参与盘后竞价;设置为 DAY 表示只在盘后有效
order = auction_limit_order(account=client_config.account, contract=contract, action='BUY', quantity=200,
limit_price='10', time_in_force='OPG')
# 竞价市价单
# order = auction_market_order(account=client_config.account, contract=contract, action='BUY', quantity=200, time_in_force='OPG')
# 下单
oid = trade_client.place_order(order)
其他示例
下单港股
下单港股时,下单数量需要符合股票的每手股数。比如 00700
的每手股数为 100, 则下单股数只能为100, 200, 500等每手股数的整数倍。 可提前用 QuoteClient.get_trade_metas
获取股票的每手股数。
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import limit_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
quote_client = QuoteClient(client_config)
symbol = '00700'
# 获取每手股数
metas = quote_client.get_trade_metas([symbol])
lot_size = int(metas['lot_size'].iloc[0])
# 生成股票合约
contract = stock_contract(symbol=symbol, currency='HKD')
# 生成订单对象
order = limit_order(account=client_config.account, contract=contract, action='BUY', limit_price=400.0, quantity=2 * log_size)
# 下单
oid = trade_client.place_order(order)
下单期货
from tigeropen.common.util.contract_utils import future_contract
from tigeropen.common.util.order_utils import limit_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成期货合约
contract = future_contract(symbol='CL', currency='USD')
# 生成订单对象
order = limit_order(account=client_config.account, contract=contract, action='BUY', limit_price=0.1, quantity=1)
# 下单
oid = trade_client.place_order(order)
下单期权
from tigeropen.common.util.contract_utils import option_contract, option_contract_by_symbol
from tigeropen.common.util.order_utils import limit_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成期权合约
contract = option_contract(identifier='AAPL 190118P00160000')
# 或
contract = option_contract_by_symbol('AAPL', '20200110', strike=280.0, put_call='PUT', currency='USD')
# 生成订单对象
order = limit_order(account=client_config.account, contract=contract, action='BUY', limit_price=0.1, quantity=1)
# 下单
oid = trade_client.place_order(order)
下单期权组合
from tigeropen.common.consts import ComboType
from tigeropen.common.util.contract_utils import option_contract, option_contract_by_symbol
from tigeropen.common.util.order_utils import combo_order, contract_leg
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
contract_leg1 = contract_leg(symbol='TSLA', sec_type='OPT', expiry='20230616', strike=220, put_call='CALL',
action='BUY', ratio=1)
contract_leg2 = contract_leg(symbol='TSLA', sec_type='OPT', expiry='20230616', strike=225, put_call='CALL',
action='SELL', ratio=1)
order = combo_order(client_config.account, [contract_leg1, contract_leg2], combo_type=ComboType.VERTICAL,
action='BUY', quantity=1, order_type='LMT', limit_price=1.0)
res = trade_client.place_order(order)
print(res)
下单基金
from tigeropen.common.util.contract_utils import fund_contract
from tigeropen.common.util.order_utils import market_order_by_amount
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成基金合约
contract = fund_contract(symbol='000001', currency='USD')
# 生成订单对象
order = market_order_by_amount(account=client_config.account, contract=contract, action='BUY', amount=100.0)
# 下单
oid = trade_client.place_order(order)
查询合约并下单
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 获取股票合约(不推荐)
contract = trade_client.get_contract('AAPL', sec_type=SecurityType.STK)
# 获取订单对象(不推荐)
order = trade_client.create_order(account=client_config.account, contract=contract, action='SELL', order_type='LMT', quantity=1, limit_price=200.0)
# 下单
oid = trade_client.place_order(order)
设置订单属性 订单不常用属性使用示例. 如 user_mark
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.consts import TradingSession
from tigeropen.common.util.order_utils import limit_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成股票合约
contract = stock_contract(symbol='AAPL', currency='USD')
# 生成订单对象
order = limit_order(account=client_config.account, contract=contract, action='BUY', limit_price=0.1, quantity=1)
# 修改订单自定义备注 (环球账号需提前联系平台启用该功能)
order.user_mark = 'my-custom-remark'
# 修改订单有效期为 GTC
# order.time_in_force = 'GTC'
# 夜盘交易
# order.trading_session_type = TradingSession.OverNight
# 修改订单为 GTD(good till date,指定日期前有效)
#order.time_in_force = 'GTD'
#order.expire_time = 1700496000000 # 必须指定过期时间
# 如果用时间字符串
#from tigeropen.common.util.common_utils import date_str_to_timestamp
#order.expire_time = date_str_to_timestamp('2023-07-01 11:00:00', 'US/Eastern')
# 下单
trade_client.place_order(order)
my_order = trade_client.get_order(id=order.id)
print(my_order.user_mark)
校正下单价格 使用合约信息和PriceUtil
校正下单价格。 合约价格处于不同档位时,会有不同的精度,当指定不合适精度的价格作为下单价格时,会返回错误。 此处使用查询到的合约tick规格数据,用工具修正价格。
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.price_util import PriceUtil
from tigeropen.common.consts import SecurityType
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 获取股票合约
contract = trade_client.get_contract(symbol='AAPL', sec_type=SecurityType.STK)
limit_price = 150.173
# 检查价格是否符合合约的tick规格
is_price_ok = PriceUtil.match_tick_size(price, contract.tick_sizes)
# 修正价格(若 is_up 参数设置为 True,则价格向上调整. 默认 False, 价格向下调整)
# 假如合约价格在 1~1000 时,tick规格为 0.01, 那么 150.173 调整后为 150.17; 向上调整后为 150.18
limit_price = PriceUtil.fix_price_by_tick_size(price, contract.tick_sizes)
# 生成订单对象
order = limit_order(account=client_config.account, contract=contract, action='BUY', limit_price=limit_price, quantity=1)
# 下单
trade_client.place_order(order)
碎股下单
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.common.util.order_utils import limit_order
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
# 生成股票合约
contract = stock_contract(symbol='AAPL', currency='USD')
# 生成订单对象
order = limit_order(account=client_config.account, contract=contract, action='BUY', limit_price=170, quantity=5)
# quantity_scale 设置为 1, 实际下单数量为 5 * 10^-1 = 0.5 股; 若quantity_scale 设置为 2, 实际下单数量为 5 * 10^-2 = 0.05 股
order.quantity_scale = 1
# 下单
oid = trade_client.place_order(order)
print(order)
create_order 请求创建订单
TradeClient.create_order():
说明
请求订单号,创建订单对象。不推荐使用,建议使用 tigeropen.common.util.order_utils
下的工具函数本地创建订单,如 limit_order
, market_order
.
参数
参数名 | 类型 | 描述 | 市价单 | 限价单 | 止损单 | 止损限价单 | 跟踪止损单 | 竞价限价单 | 竞价市价单 |
---|---|---|---|---|---|---|---|---|---|
account | str | 账户id,不传则返回所有关联的 account | 选填 | 选填 | 选填 | 选填 | 选填 | 选填 | 选填 |
contract | Contract | 合约对象 | |||||||
action | str | 买卖方向, 'BUY':买入, 'SELL':卖出 | |||||||
order_type | str | 订单类型, 'MKT' 市价单 / 'LMT' 限价单 / 'STP' 止损单 / 'STP_LMT' 止损限价单 / 'TRAIL' 跟踪止损单 | MKT | LMT | STP | STP_LMT | TRAIL | AL | AM |
quantity | int | 下单数量, 为大于0的整数 | |||||||
limit_price | float | 限价单价格,当订单类型为LMT或STP_LMT或AL时必填 | 必填 | ||||||
aux_price | float | 在止损单表示止损价格; 在跟踪止损单表示价差 | 选填 | 选填 | 选填 | ||||
trail_stop_price | float | 跟踪止损单--触发止损单的价格 | |||||||
trailing_percent | float | 跟踪止损单--百分比(跟踪止损单aux_price和trailing_percent至少填一个) | 选填 | ||||||
adjust_limit | float | 价格微调幅度(默认为0表示不调整,正数为向上调整,负数向下调整),对传入价格自动调整到合法价位上。例如:0.001 代表向上调整且幅度不超过 0.1%;-0.001 代表向下调整且幅度不超过 0.1%。默认 0 表示不调整 | 选填 | 选填 | 选填 | 选填 | |||
time_in_force | str | 订单有效期,只能是 DAY(当日有效)和GTC(取消前有效),默认为DAY. | 选填 | 选填 | 选填 | 选填 | 选填 | 选填(港股竞价单盘前下单需设置为 OPG,盘后需为 DAY) | 选填(港股竞价单盘前下单需设置为 OPG,盘后需为 DAY) |
outside_rth | bool | True: 允许盘前盘后交易(美股专属), False: 不允许, 默认为True。(市价单只在盘中有效,将忽略outside_rth参数) | 选填 | 选填 | 选填 | 必填(固定为True) | 必填(固定为True) | ||
order_legs | object | 附加订单列表,参数上面示例 | 选填 | 选填 | 选填 | 选填 | 选填 | 不填 | 不填 |
algo_params | object | 算法订单参数,参考上面示例 | 选填 | 选填 | 选填 | 选填 | 选填 | 不填 | 不填 |
secret_key | str | 机构用户专用,交易员密钥 | 选填 | 选填 | 选填 | 选填 | 选填 | 选填 | 选填 |
user_mark | str | 下单备注信息,下单后不能修改,查询订单时返回userMark信息(环球账号需要配置开发者个人信息) | 选填 | 选填 | 选填 | 选填 | 选填 | 选填 | 选填 |
示例
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
contract = stock_contract(symbol='AAPL', currency='USD')
order = openapi_client.create_order(account, contract, 'BUY', 'LMT', 100, limit_price=5.0)
trade_client.place_order(order)
place_forex_order 换汇下单
说明
换汇订单,返回 Order 对象
示例
from tigeropen.common.util.contract_utils import stock_contract
from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')
trade_client = TradeClient(client_config)
order = trade_client.place_forex_order(seg_type='FUT', source_currency='USD', target_currency='HKD',
source_amount=50)
print(order)
返回示例
Order({'account': '11111', 'id': 30323340487950336, 'order_id': 1447, 'parent_id': None, 'order_time': 1680247928000, 'reason': '', 'trade_time': 1680247928000, 'action': 'SELL', 'quantity': 50, 'filled': 0, 'avg_fill_price': 0.0, 'commission': 0.0, 'realized_pnl': 0.0, 'trail_stop_price': None, 'limit_price': 7.74991, 'aux_price': None, 'trailing_percent': None, 'percent_offset': None, 'order_type': 'LMT', 'time_in_force': 'DAY', 'outside_rth': False, 'order_legs': None, 'algo_params': None, 'algo_strategy': 'LMT', 'secret_key': None, 'liquidation': False, 'discount': 0.0, 'attr_desc': None, 'source': 'OpenApi', 'adjust_limit': None, 'sub_ids': None, 'user_mark': '', 'update_time': 1680247928000, 'expire_time': None, 'can_modify': True, 'contract': USD.HKD/FOREX/HKD, 'status': 'NEW', 'remaining': 50})