Skip to content

Commit 6522ac1

Browse files
committed
modify order leg
1 parent d905107 commit 6522ac1

File tree

4 files changed

+46
-59
lines changed

4 files changed

+46
-59
lines changed

tigeropen/common/util/order_utils.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,15 @@ def trail_order(account, contract, action, quantity, trailing_percent=None, aux_
7474
return Order(account, contract, action, 'TRAIL', quantity, trailing_percent=trailing_percent, aux_price=aux_price)
7575

7676

77-
def order_leg(type, stop_loss_price=None, stop_loss_tif='DAY', profit_taker_price=None, profit_taker_tif='DAY'):
77+
def order_leg(leg_type, price, time_in_force='DAY', outside_rth=None):
7878
"""
7979
附加订单
80-
:param type: 附加订单类型. PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈). 必选
81-
:param stop_loss_price: 附加止损单价格. type 为 LOSS 或 BRACKETS 时必选
82-
:param stop_loss_tif: 附加止损单有效期. 'DAY'(当日有效)和'GTC'(取消前有效 Good-Til-Canceled).
83-
:param profit_taker_price: 附加止盈单价格. type 为 PROFIT 或 BRACKETS 时必选
84-
:param profit_taker_tif: 附加止盈单有效期. 'DAY'(当日有效)和'GTC'(取消前有效).
80+
:param leg_type: 附加订单类型. PROFIT 止盈单类型, LOSS 止损单类型
81+
:param price: 附加订单价格.
82+
:param time_in_force: 附加订单有效期. 'DAY'(当日有效)和'GTC'(取消前有效 Good-Til-Canceled).
83+
:param outside_rth: 附加订单是否允许盘前盘后交易(美股专属). True 允许, False 不允许.
8584
"""
86-
return OrderLeg(type=type, stop_loss_price=stop_loss_price, stop_loss_tif=stop_loss_tif,
87-
profit_taker_price=profit_taker_price, profit_taker_tif=profit_taker_tif)
85+
return OrderLeg(leg_type=leg_type, price=price, time_in_force=time_in_force, outside_rth=outside_rth)
8886

8987

9088
def limit_order_with_legs(account, contract, action, quantity, limit_price, order_legs=None):
@@ -95,7 +93,7 @@ def limit_order_with_legs(account, contract, action, quantity, limit_price, orde
9593
:param action: BUY/SELL
9694
:param quantity:
9795
:param limit_price: 限价单价格
98-
:param order_legs: 附加订单
96+
:param order_legs: 附加订单列表
9997
:return:
10098
"""
10199
return Order(account, contract, action, 'LMT', quantity, limit_price=limit_price, order_legs=order_legs)

tigeropen/examples/trade_client_demo.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ def trade_apis():
9898
print(result)
9999

100100
# 限价单 + 附加订单 (仅主订单为限价单时支持附加订单)
101+
stop_loss_order_legs = order_leg('LOSS', 8.0, time_in_force='GTC') # 附加止损
102+
profit_taker_order_legs = order_leg('PROFIT', 12.0, time_in_force='GTC') # 附加止盈
103+
101104
main_order = openapi_client.create_order(account, contract, 'BUY', 'LMT', quantity=100, limit_price=10.0,
102-
order_legs=order_leg(type='BRACKETS', stop_loss_price=8.0,
103-
stop_loss_tif='GTC', profit_taker_price=12.0,
104-
profit_taker_tif='GTC'))
105+
order_legs=[stop_loss_order_legs, profit_taker_order_legs])
105106
# 本地构造限价单 + 附加订单
106-
# main_order = limit_order_with_legs(account, contract, 'BUY', 100, limit_price=10.0, order_legs=order_leg(
107-
# type='LOSS', stop_loss_price=8.0, stop_loss_tif='DAY'))
107+
# main_order = limit_order_with_legs(account, contract, 'BUY', 100, limit_price=10.0,
108+
# order_legs=[stop_loss_order_legs])
109+
108110
openapi_client.place_order(main_order)
109111
print(main_order)
110112
# 查询主订单所关联的附加订单

tigeropen/trade/domain/order.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def __init__(self, account, contract, action, order_type, quantity, limit_price=
4141
- percent_offset: None,
4242
- order_type: 订单类型, 'MKT' 市价单 / 'LMT' 限价单 / 'STP' 止损单 / 'STP_LMT' 止损限价单 / 'TRAIL' 跟踪止损单
4343
- time_in_force: 有效期,'DAY' 日内有效 / 'GTC' 撤销前有效
44-
- outside_rth: 是否允许盘前盘后交易(美股专属). True 允许, False 不允许
44+
- outside_rth: 是否允许盘前盘后交易(outside of regular trading hours 美股专属). True 允许, False 不允许
4545
- contract: 合约对象
4646
- status: Order_Status 的枚举, 表示订单状态
4747
- remaining: 未成交的数量
48-
- order_legs: 附加订单
48+
- order_legs: 附加订单列表
4949
"""
5050

5151
self.id = id
@@ -124,31 +124,17 @@ class OrderLeg(object):
124124
附加订单
125125
"""
126126

127-
def __init__(self, type, stop_loss_price=None, stop_loss_tif=None, stop_loss_rth=None,
128-
stop_loss_order_id=None, profit_taker_price=None, profit_taker_tif=None, profit_taker_rth=None,
129-
profit_taker_order_id=None):
127+
def __init__(self, leg_type, price, time_in_force='DAY', outside_rth=None):
130128
"""
131-
:param type: 附加订单类型(仅限价单支持). PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈)
132-
:param stop_loss_price: 附加止损单价格
133-
:param stop_loss_tif: 附加止损单有效期. 'DAY'(当日有效)和'GTC'(取消前有效). 同 time_in_force 字段
134-
:param stop_loss_rth: 附加止损单是否允许盘前盘后交易(美股专属). True 允许, False 不允许. 同 outside_rth 字段
135-
:param stop_loss_order_id: 附加止损单号. 可以通过订单号接口获取, 如果传0或为空, 则服务器端会自动生成止损单号
136-
:param profit_taker_price: 附加止盈单价格
137-
:param profit_taker_tif: 附加止盈单有效期. 'DAY'(当日有效)和'GTC'(取消前有效). 同 time_in_force 字段
138-
:param profit_taker_rth: 附加止盈单是否允许盘前盘后交易(美股专属). True 允许, False 不允许. 同 outside_rth 字段
139-
:param profit_taker_order_id: 附加止盈单号. 可以通过订单号接口获取, 如果传0或为空, 则服务器端会自动生成止盈单号
129+
:param leg_type: 附加订单类型(仅限价单支持). PROFIT 止盈单类型, LOSS 止损单类型
130+
:param price: 附加订单价格
131+
:param time_in_force: 附加订单有效期. 'DAY'(当日有效)和'GTC'(取消前有效).
132+
:param outside_rth: 附加订单是否允许盘前盘后交易(美股专属). True 允许, False 不允许.
140133
"""
141-
self.type = type
142-
if self.type == 'LOSS' or self.type == 'BRACKETS':
143-
self.stop_loss_price = stop_loss_price
144-
self.stop_loss_tif = stop_loss_tif
145-
self.stop_loss_rth = stop_loss_rth
146-
self.stop_loss_order_id = stop_loss_order_id
147-
if self.type == 'PROFIT' or self.type == 'BRACKETS':
148-
self.profit_taker_price = profit_taker_price
149-
self.profit_taker_tif = profit_taker_tif
150-
self.profit_taker_rth = profit_taker_rth
151-
self.profit_taker_order_id = profit_taker_order_id
134+
self.type = leg_type
135+
self.price = price
136+
self.time_in_force = time_in_force
137+
self.outside_rth = outside_rth
152138

153139
def to_dict(self):
154140
return self.__dict__

tigeropen/trade/request/model.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -555,26 +555,27 @@ def to_openapi_dict(self):
555555
if self.outside_rth is not None:
556556
params['outside_rth'] = self.outside_rth
557557

558-
if self.order_legs and self.order_legs.type is not None:
559-
params['attach_type'] = self.order_legs.type
560-
if self.order_legs.type in ('PROFIT', 'BRACKETS'):
561-
if self.order_legs.profit_taker_order_id is not None:
562-
params['profit_taker_order_id'] = self.order_legs.profit_taker_order_id
563-
if self.order_legs.profit_taker_price is not None:
564-
params['profit_taker_price'] = self.order_legs.profit_taker_price
565-
if self.order_legs.profit_taker_tif is not None:
566-
params['profit_taker_tif'] = self.order_legs.profit_taker_tif
567-
if self.order_legs.profit_taker_rth is not None:
568-
params['profit_taker_rth'] = self.order_legs.profit_taker_rth
569-
if self.order_legs.type in ('LOSS', 'BRACKETS'):
570-
if self.order_legs.stop_loss_order_id is not None:
571-
params['stop_loss_order_id'] = self.order_legs.stop_loss_order_id
572-
if self.order_legs.stop_loss_price is not None:
573-
params['stop_loss_price'] = self.order_legs.stop_loss_price
574-
if self.order_legs.stop_loss_tif is not None:
575-
params['stop_loss_tif'] = self.order_legs.stop_loss_tif
576-
if self.order_legs.stop_loss_rth is not None:
577-
params['stop_loss_rth'] = self.order_legs.stop_loss_rth
558+
if self.order_legs:
559+
for order_leg in self.order_legs:
560+
if order_leg.type == 'PROFIT':
561+
params['attach_type'] = 'PROFIT'
562+
if order_leg.price is not None:
563+
params['profit_taker_price'] = order_leg.price
564+
if order_leg.time_in_force is not None:
565+
params['profit_taker_tif'] = order_leg.time_in_force
566+
if order_leg.outside_rth is not None:
567+
params['profit_taker_rth'] = order_leg.outside_rth
568+
if order_leg.type == 'LOSS':
569+
params['attach_type'] = 'LOSS'
570+
if order_leg.price is not None:
571+
params['stop_loss_price'] = order_leg.price
572+
if order_leg.time_in_force is not None:
573+
params['stop_loss_tif'] = order_leg.time_in_force
574+
if order_leg.outside_rth is not None:
575+
params['stop_loss_rth'] = order_leg.outside_rth
576+
# 括号订单(止盈和止损)
577+
if len(self.order_legs) > 1:
578+
params['attach_type'] = 'BRACKETS'
578579

579580
return params
580581

0 commit comments

Comments
 (0)