Skip to content

Commit d905107

Browse files
committed
modify order legs
1 parent dfa55e2 commit d905107

File tree

5 files changed

+64
-61
lines changed

5 files changed

+64
-61
lines changed

tigeropen/common/util/order_utils.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
@author: gaoan
66
"""
7-
from tigeropen.trade.domain.order import Order, AttachOrder
7+
from tigeropen.trade.domain.order import Order, OrderLeg
88
from tigeropen.common.consts import OrderStatus
99

1010

@@ -74,32 +74,31 @@ 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 attach_order(attach_type, stop_loss_price=None, stop_loss_tif='DAY', profit_taker_price=None,
78-
profit_taker_tif='DAY'):
77+
def order_leg(type, stop_loss_price=None, stop_loss_tif='DAY', profit_taker_price=None, profit_taker_tif='DAY'):
7978
"""
8079
附加订单
81-
:param attach_type: 附加订单类型. PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈). 必选
82-
:param stop_loss_price: 附加止损单价格. attach_type 为 LOSS 或 BRACKETS 时必选
80+
:param type: 附加订单类型. PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈). 必选
81+
:param stop_loss_price: 附加止损单价格. type 为 LOSS 或 BRACKETS 时必选
8382
:param stop_loss_tif: 附加止损单有效期. 'DAY'(当日有效)和'GTC'(取消前有效 Good-Til-Canceled).
84-
:param profit_taker_price: 附加止盈单价格. attach_type 为 PROFIT 或 BRACKETS 时必选
83+
:param profit_taker_price: 附加止盈单价格. type 为 PROFIT 或 BRACKETS 时必选
8584
:param profit_taker_tif: 附加止盈单有效期. 'DAY'(当日有效)和'GTC'(取消前有效).
8685
"""
87-
return AttachOrder(attach_type=attach_type, stop_loss_price=stop_loss_price, stop_loss_tif=stop_loss_tif,
88-
profit_taker_price=profit_taker_price, profit_taker_tif=profit_taker_tif)
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)
8988

9089

91-
def limit_order_with_attach(account, contract, action, quantity, limit_price, attach_order=None):
90+
def limit_order_with_legs(account, contract, action, quantity, limit_price, order_legs=None):
9291
"""
9392
限价单 + 附加订单(仅环球账户支持)
9493
:param account:
9594
:param contract:
9695
:param action: BUY/SELL
9796
:param quantity:
9897
:param limit_price: 限价单价格
99-
:param attach_order: 附加订单
98+
:param order_legs: 附加订单
10099
:return:
101100
"""
102-
return Order(account, contract, action, 'LMT', quantity, limit_price=limit_price, attach_order=attach_order)
101+
return Order(account, contract, action, 'LMT', quantity, limit_price=limit_price, order_legs=order_legs)
103102

104103

105104
def get_order_status(value):

tigeropen/examples/trade_client_demo.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
from tigeropen.trade.trade_client import TradeClient
1515
from tigeropen.quote.request import OpenApiRequest
1616
from tigeropen.examples.client_config import get_client_config
17-
# from tigeropen.common.util.contract_utils import stock_contract, option_contract, future_contract
18-
from tigeropen.common.util.order_utils import limit_order, limit_order_with_attach, attach_order
17+
# from tigeropen.common.util.contract_utils import stock_contract, option_contract_by_symbol, future_contract, \
18+
# war_contract_by_symbol, iopt_contract_by_symbol
19+
from tigeropen.common.util.order_utils import limit_order, limit_order_with_legs, order_leg
1920

2021
logging.basicConfig(level=logging.INFO,
2122
format='%(asctime)s %(levelname)s %(message)s',
@@ -98,17 +99,17 @@ def trade_apis():
9899

99100
# 限价单 + 附加订单 (仅主订单为限价单时支持附加订单)
100101
main_order = openapi_client.create_order(account, contract, 'BUY', 'LMT', quantity=100, limit_price=10.0,
101-
attach_order=attach_order(attach_type='BRACKETS', stop_loss_price=8.0,
102-
stop_loss_tif='GTC', profit_taker_price=12.0,
103-
profit_taker_tif='GTC'))
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'))
104105
# 本地构造限价单 + 附加订单
105-
# main_order = limit_order_with_attach(account, contract, 'BUY', 100, limit_price=10.0, attach_order=attach_order(
106-
# attach_type='LOSS', stop_loss_price=8.0, stop_loss_tif='DAY'))
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'))
107108
openapi_client.place_order(main_order)
108109
print(main_order)
109110
# 查询主订单所关联的附加订单
110-
attach_orders = openapi_client.get_open_orders(account, parent_id=main_order.order_id)
111-
print(attach_orders)
111+
order_legs = openapi_client.get_open_orders(account, parent_id=main_order.order_id)
112+
print(order_legs)
112113

113114

114115
if __name__ == '__main__':

tigeropen/trade/domain/order.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class Order(object):
1414
__slots__ = ["account", "id", "order_id", "parent_id", "order_time", "reason", "trade_time", "contract", "action",
1515
"quantity", "filled", "_remaining", "avg_fill_price", "commission", "realized_pnl", "_status",
1616
"trail_stop_price", "limit_price", "aux_price", "trailing_percent", "percent_offset", "action",
17-
"order_type", "time_in_force", "outside_rth", "attach_order"]
17+
"order_type", "time_in_force", "outside_rth", "order_legs"]
1818

1919
def __init__(self, account, contract, action, order_type, quantity, limit_price=None, aux_price=None,
2020
trail_stop_price=None, trailing_percent=None, percent_offset=None, time_in_force=None,
2121
outside_rth=None, filled=0, avg_fill_price=0, commission=None, realized_pnl=None,
22-
id=None, order_id=None, parent_id=None, order_time=None, trade_time=None, attach_order=None):
22+
id=None, order_id=None, parent_id=None, order_time=None, trade_time=None, order_legs=None):
2323
"""
2424
- account: 订单所属的账户
2525
- id: 全局订单 id
@@ -45,7 +45,7 @@ def __init__(self, account, contract, action, order_type, quantity, limit_price=
4545
- contract: 合约对象
4646
- status: Order_Status 的枚举, 表示订单状态
4747
- remaining: 未成交的数量
48-
- attach_order: 附加订单
48+
- order_legs: 附加订单
4949
"""
5050

5151
self.id = id
@@ -72,7 +72,7 @@ def __init__(self, account, contract, action, order_type, quantity, limit_price=
7272
self.percent_offset = percent_offset
7373
self.order_time = order_time
7474
self.trade_time = trade_time
75-
self.attach_order = attach_order
75+
self.order_legs = order_legs
7676

7777
def to_dict(self):
7878
dct = {name: getattr(self, name) for name in self.__slots__ if name not in ORDER_FIELDS_TO_IGNORE}
@@ -119,16 +119,16 @@ def __unicode__(self):
119119
return text_type(repr(self))
120120

121121

122-
class AttachOrder(object):
122+
class OrderLeg(object):
123123
"""
124124
附加订单
125125
"""
126126

127-
def __init__(self, attach_type=None, stop_loss_price=None, stop_loss_tif=None, stop_loss_rth=None,
127+
def __init__(self, type, stop_loss_price=None, stop_loss_tif=None, stop_loss_rth=None,
128128
stop_loss_order_id=None, profit_taker_price=None, profit_taker_tif=None, profit_taker_rth=None,
129129
profit_taker_order_id=None):
130130
"""
131-
:param attach_type: 附加订单类型(仅限价单支持). PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈)
131+
:param type: 附加订单类型(仅限价单支持). PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈)
132132
:param stop_loss_price: 附加止损单价格
133133
:param stop_loss_tif: 附加止损单有效期. 'DAY'(当日有效)和'GTC'(取消前有效). 同 time_in_force 字段
134134
:param stop_loss_rth: 附加止损单是否允许盘前盘后交易(美股专属). True 允许, False 不允许. 同 outside_rth 字段
@@ -138,18 +138,20 @@ def __init__(self, attach_type=None, stop_loss_price=None, stop_loss_tif=None, s
138138
:param profit_taker_rth: 附加止盈单是否允许盘前盘后交易(美股专属). True 允许, False 不允许. 同 outside_rth 字段
139139
:param profit_taker_order_id: 附加止盈单号. 可以通过订单号接口获取, 如果传0或为空, 则服务器端会自动生成止盈单号
140140
"""
141-
self.attach_type = attach_type
142-
self.stop_loss_price = stop_loss_price
143-
self.stop_loss_tif = stop_loss_tif
144-
self.stop_loss_rth = stop_loss_rth
145-
self.stop_loss_order_id = stop_loss_order_id
146-
self.profit_taker_price = profit_taker_price
147-
self.profit_taker_tif = profit_taker_tif
148-
self.profit_taker_rth = profit_taker_rth
149-
self.profit_taker_order_id = profit_taker_order_id
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
150152

151153
def to_dict(self):
152154
return self.__dict__
153155

154156
def __repr__(self):
155-
return "AttachOrder(%s)" % self.to_dict()
157+
return "OrderLeg(%s)" % self.to_dict()

tigeropen/trade/request/model.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def __init__(self):
502502
self.percent_offset = None
503503
self.time_in_force = None
504504
self.outside_rth = None
505-
self.attach_order = None
505+
self.order_legs = None
506506

507507
def to_openapi_dict(self):
508508
params = dict()
@@ -555,25 +555,26 @@ def to_openapi_dict(self):
555555
if self.outside_rth is not None:
556556
params['outside_rth'] = self.outside_rth
557557

558-
if self.attach_order:
559-
if self.attach_order.attach_type is not None:
560-
params['attach_type'] = self.attach_order.attach_type
561-
if self.attach_order.profit_taker_order_id is not None:
562-
params['profit_taker_order_id'] = self.attach_order.profit_taker_order_id
563-
if self.attach_order.profit_taker_price is not None:
564-
params['profit_taker_price'] = self.attach_order.profit_taker_price
565-
if self.attach_order.profit_taker_tif is not None:
566-
params['profit_taker_tif'] = self.attach_order.profit_taker_tif
567-
if self.attach_order.profit_taker_rth is not None:
568-
params['profit_taker_rth'] = self.attach_order.profit_taker_rth
569-
if self.attach_order.stop_loss_order_id is not None:
570-
params['stop_loss_order_id'] = self.attach_order.stop_loss_order_id
571-
if self.attach_order.stop_loss_price is not None:
572-
params['stop_loss_price'] = self.attach_order.stop_loss_price
573-
if self.attach_order.stop_loss_tif is not None:
574-
params['stop_loss_tif'] = self.attach_order.stop_loss_tif
575-
if self.attach_order.stop_loss_rth is not None:
576-
params['stop_loss_rth'] = self.attach_order.stop_loss_rth
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
577578

578579
return params
579580

tigeropen/trade/trade_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def get_order(self, account=None, id=None, order_id=None, is_brief=False):
379379

380380
def create_order(self, account, contract, action, order_type, quantity, limit_price=None, aux_price=None,
381381
trail_stop_price=None, trailing_percent=None, percent_offset=None, time_in_force=None,
382-
outside_rth=None, attach_order=None):
382+
outside_rth=None, order_legs=None):
383383
"""
384384
创建订单对象.
385385
:param account:
@@ -394,7 +394,7 @@ def create_order(self, account, contract, action, order_type, quantity, limit_pr
394394
:param percent_offset:
395395
:param time_in_force: 订单有效期, 'DAY'(当日有效)和'GTC'(取消前有效)
396396
:param outside_rth: 是否允许盘前盘后交易(美股专属)
397-
:param attach_order: 附加订单
397+
:param order_legs: 附加订单
398398
"""
399399
params = AccountsParams()
400400
params.account = account if account else self._account
@@ -409,7 +409,7 @@ def create_order(self, account, contract, action, order_type, quantity, limit_pr
409409
aux_price=aux_price, trail_stop_price=trail_stop_price,
410410
trailing_percent=trailing_percent, percent_offset=percent_offset,
411411
time_in_force=time_in_force, outside_rth=outside_rth, order_id=order_id,
412-
attach_order=attach_order)
412+
order_legs=order_legs)
413413
return order
414414
else:
415415
raise ApiException(response.code, response.message)
@@ -479,7 +479,7 @@ def place_order(self, order):
479479
params.percent_offset = order.percent_offset
480480
params.time_in_force = order.time_in_force
481481
params.outside_rth = order.outside_rth
482-
params.attach_order = order.attach_order
482+
params.order_legs = order.order_legs
483483

484484
request = OpenApiRequest(PLACE_ORDER, biz_model=params)
485485
response_content = self.__fetch_data(request)

0 commit comments

Comments
 (0)