Skip to content

Commit dfa55e2

Browse files
committed
modify attach_order util
1 parent e6b7a76 commit dfa55e2

File tree

5 files changed

+88
-96
lines changed

5 files changed

+88
-96
lines changed

tigeropen/common/util/order_utils.py

Lines changed: 18 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
7+
from tigeropen.trade.domain.order import Order, AttachOrder
88
from tigeropen.common.consts import OrderStatus
99

1010

@@ -74,25 +74,32 @@ 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 limit_order_with_attach(account, contract, action, quantity, limit_price, attach_type=None, stop_loss_price=None,
78-
stop_loss_tif='DAY', profit_taker_price=None, profit_taker_tif='DAY'):
77+
def attach_order(attach_type, stop_loss_price=None, stop_loss_tif='DAY', profit_taker_price=None,
78+
profit_taker_tif='DAY'):
79+
"""
80+
附加订单
81+
:param attach_type: 附加订单类型. PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈). 必选
82+
:param stop_loss_price: 附加止损单价格. attach_type 为 LOSS 或 BRACKETS 时必选
83+
:param stop_loss_tif: 附加止损单有效期. 'DAY'(当日有效)和'GTC'(取消前有效 Good-Til-Canceled).
84+
:param profit_taker_price: 附加止盈单价格. attach_type 为 PROFIT 或 BRACKETS 时必选
85+
:param profit_taker_tif: 附加止盈单有效期. 'DAY'(当日有效)和'GTC'(取消前有效).
86+
"""
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)
89+
90+
91+
def limit_order_with_attach(account, contract, action, quantity, limit_price, attach_order=None):
7992
"""
8093
限价单 + 附加订单(仅环球账户支持)
8194
:param account:
8295
:param contract:
8396
:param action: BUY/SELL
8497
:param quantity:
8598
:param limit_price: 限价单价格
86-
:param attach_type: 附加订单类型. PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈)
87-
:param stop_loss_price: 附加止损单价格. attach_type 为 LOSS 或 BRACKETS 时必选
88-
:param stop_loss_tif: 附加止损单有效期. 'DAY'(当日有效)和'GTC'(取消前有效 Good-Til-Canceled).
89-
:param profit_taker_price: 附加止盈单价格. attach_type 为 PROFIT 或 BRACKETS 时必选
90-
:param profit_taker_tif: 附加止盈单有效期. 'DAY'(当日有效)和'GTC'(取消前有效).
99+
:param attach_order: 附加订单
91100
:return:
92101
"""
93-
return Order(account, contract, action, 'LMT', quantity, limit_price=limit_price, attach_type=attach_type,
94-
stop_loss_price=stop_loss_price, stop_loss_tif=stop_loss_tif, profit_taker_price=profit_taker_price,
95-
profit_taker_tif=profit_taker_tif)
102+
return Order(account, contract, action, 'LMT', quantity, limit_price=limit_price, attach_order=attach_order)
96103

97104

98105
def get_order_status(value):

tigeropen/examples/trade_client_demo.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
from tigeropen.quote.request import OpenApiRequest
1616
from tigeropen.examples.client_config import get_client_config
1717
# 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
19-
18+
from tigeropen.common.util.order_utils import limit_order, limit_order_with_attach, attach_order
2019

2120
logging.basicConfig(level=logging.INFO,
2221
format='%(asctime)s %(levelname)s %(message)s',
@@ -99,11 +98,12 @@ def trade_apis():
9998

10099
# 限价单 + 附加订单 (仅主订单为限价单时支持附加订单)
101100
main_order = openapi_client.create_order(account, contract, 'BUY', 'LMT', quantity=100, limit_price=10.0,
102-
attach_type='BRACKETS', stop_loss_price=8.0, stop_loss_tif='GTC',
103-
profit_taker_price=12.0, profit_taker_tif='GTC')
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'))
104104
# 本地构造限价单 + 附加订单
105-
# main_order = limit_order_with_attach(account, contract, 'BUY', 100, limit_price=10.0, attach_type='LOSS',
106-
# stop_loss_price=8.0, stop_loss_tif='DAY')
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'))
107107
openapi_client.place_order(main_order)
108108
print(main_order)
109109
# 查询主订单所关联的附加订单

tigeropen/trade/domain/order.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +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_type", "stop_loss_price", "stop_loss_tif",
18-
"stop_loss_rth", "stop_loss_order_id", "profit_taker_price", "profit_taker_tif", "profit_taker_rth",
19-
"profit_taker_order_id"]
17+
"order_type", "time_in_force", "outside_rth", "attach_order"]
2018

2119
def __init__(self, account, contract, action, order_type, quantity, limit_price=None, aux_price=None,
2220
trail_stop_price=None, trailing_percent=None, percent_offset=None, time_in_force=None,
2321
outside_rth=None, filled=0, avg_fill_price=0, commission=None, realized_pnl=None,
24-
id=None, order_id=None, parent_id=None, order_time=None, trade_time=None,
25-
attach_type=None, stop_loss_price=None, stop_loss_tif=None, stop_loss_rth=None,
26-
stop_loss_order_id=None, profit_taker_price=None, profit_taker_tif=None, profit_taker_rth=None,
27-
profit_taker_order_id=None):
22+
id=None, order_id=None, parent_id=None, order_time=None, trade_time=None, attach_order=None):
2823
"""
2924
- account: 订单所属的账户
3025
- id: 全局订单 id
@@ -50,16 +45,7 @@ def __init__(self, account, contract, action, order_type, quantity, limit_price=
5045
- contract: 合约对象
5146
- status: Order_Status 的枚举, 表示订单状态
5247
- remaining: 未成交的数量
53-
54-
- attach_type: 附加订单类型(仅限价单支持附加订单). PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈)
55-
- stop_loss_price: 附加止损单价格
56-
- stop_loss_tif: 附加止损单有效期. 同 time_in_force 字段. 'DAY' 日内有效 / 'GTC' 撤销前有效
57-
- stop_loss_rth: 附加止损单是否允许盘前盘后交易(美股专属). True 允许, False 不允许. 同 outside_rth 字段
58-
- stop_loss_order_id: 附加止损单号. 可以通过订单号接口获取, 如果传0或为空, 则服务器端会自动生成止损单号
59-
- profit_taker_price: 附加止盈单价格
60-
- profit_taker_tif: 附加止盈单有效期. 同 time_in_force 字段. 'DAY' 日内有效 / 'GTC' 撤销前有效
61-
- profit_taker_rth: 附加止盈单是否允许盘前盘后交易(美股专属). True 允许, False 不允许. 同 outside_rth 字段
62-
- profit_taker_order_id: 附加止盈单号. 可以通过订单号接口获取, 如果传0或为空, 则服务器端会自动生成止盈单号
48+
- attach_order: 附加订单
6349
"""
6450

6551
self.id = id
@@ -86,15 +72,7 @@ def __init__(self, account, contract, action, order_type, quantity, limit_price=
8672
self.percent_offset = percent_offset
8773
self.order_time = order_time
8874
self.trade_time = trade_time
89-
self.attach_type = attach_type
90-
self.stop_loss_price = stop_loss_price
91-
self.stop_loss_tif = stop_loss_tif
92-
self.stop_loss_rth = stop_loss_rth
93-
self.stop_loss_order_id = stop_loss_order_id
94-
self.profit_taker_price = profit_taker_price
95-
self.profit_taker_tif = profit_taker_tif
96-
self.profit_taker_rth = profit_taker_rth
97-
self.profit_taker_order_id = profit_taker_order_id
75+
self.attach_order = attach_order
9876

9977
def to_dict(self):
10078
dct = {name: getattr(self, name) for name in self.__slots__ if name not in ORDER_FIELDS_TO_IGNORE}
@@ -139,3 +117,39 @@ def __unicode__(self):
139117
Unicode representation for this object.
140118
"""
141119
return text_type(repr(self))
120+
121+
122+
class AttachOrder(object):
123+
"""
124+
附加订单
125+
"""
126+
127+
def __init__(self, attach_type=None, 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):
130+
"""
131+
:param attach_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或为空, 则服务器端会自动生成止盈单号
140+
"""
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
150+
151+
def to_dict(self):
152+
return self.__dict__
153+
154+
def __repr__(self):
155+
return "AttachOrder(%s)" % self.to_dict()

tigeropen/trade/request/model.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,7 @@ def __init__(self):
502502
self.percent_offset = None
503503
self.time_in_force = None
504504
self.outside_rth = None
505-
self.attach_type = None
506-
self.profit_taker_order_id = None
507-
self.profit_taker_price = None
508-
self.profit_taker_tif = None
509-
self.profit_taker_rth = None
510-
self.stop_loss_order_id = None
511-
self.stop_loss_price = None
512-
self.stop_loss_tif = None
513-
self.stop_loss_rth = None
505+
self.attach_order = None
514506

515507
def to_openapi_dict(self):
516508
params = dict()
@@ -563,24 +555,25 @@ def to_openapi_dict(self):
563555
if self.outside_rth is not None:
564556
params['outside_rth'] = self.outside_rth
565557

566-
if self.attach_type is not None:
567-
params['attach_type'] = self.attach_type
568-
if self.profit_taker_order_id is not None:
569-
params['profit_taker_order_id'] = self.profit_taker_order_id
570-
if self.profit_taker_price is not None:
571-
params['profit_taker_price'] = self.profit_taker_price
572-
if self.profit_taker_tif is not None:
573-
params['profit_taker_tif'] = self.profit_taker_tif
574-
if self.profit_taker_rth is not None:
575-
params['profit_taker_rth'] = self.profit_taker_rth
576-
if self.stop_loss_order_id is not None:
577-
params['stop_loss_order_id'] = self.stop_loss_order_id
578-
if self.stop_loss_price is not None:
579-
params['stop_loss_price'] = self.stop_loss_price
580-
if self.stop_loss_tif is not None:
581-
params['stop_loss_tif'] = self.stop_loss_tif
582-
if self.stop_loss_rth is not None:
583-
params['stop_loss_rth'] = self.stop_loss_rth
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
584577

585578
return params
586579

tigeropen/trade/trade_client.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +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_type=None, stop_loss_price=None, stop_loss_tif='DAY', stop_loss_rth=None,
383-
stop_loss_order_id=None, profit_taker_price=None, profit_taker_tif='DAY', profit_taker_rth=None,
384-
profit_taker_order_id=None):
382+
outside_rth=None, attach_order=None):
385383
"""
386384
创建订单对象.
387385
:param account:
@@ -396,16 +394,7 @@ def create_order(self, account, contract, action, order_type, quantity, limit_pr
396394
:param percent_offset:
397395
:param time_in_force: 订单有效期, 'DAY'(当日有效)和'GTC'(取消前有效)
398396
:param outside_rth: 是否允许盘前盘后交易(美股专属)
399-
400-
:param attach_type: 附加订单类型(仅限价单支持). PROFIT 止盈单类型, LOSS 止损单类型, BRACKETS 括号订单类型(止损和止盈)
401-
:param stop_loss_price: 附加止损单价格
402-
:param stop_loss_tif: 附加止损单有效期. 'DAY'(当日有效)和'GTC'(取消前有效). 同 time_in_force 字段
403-
:param stop_loss_rth: 附加止损单是否允许盘前盘后交易(美股专属). True 允许, False 不允许. 同 outside_rth 字段
404-
:param stop_loss_order_id: 附加止损单号. 可以通过订单号接口获取, 如果传0或为空, 则服务器端会自动生成止损单号
405-
:param profit_taker_price: 附加止盈单价格
406-
:param profit_taker_tif: 附加止盈单有效期. 'DAY'(当日有效)和'GTC'(取消前有效). 同 time_in_force 字段
407-
:param profit_taker_rth: 附加止盈单是否允许盘前盘后交易(美股专属). True 允许, False 不允许. 同 outside_rth 字段
408-
:param profit_taker_order_id: 附加止盈单号. 可以通过订单号接口获取, 如果传0或为空, 则服务器端会自动生成止盈单号
397+
:param attach_order: 附加订单
409398
"""
410399
params = AccountsParams()
411400
params.account = account if account else self._account
@@ -420,10 +409,7 @@ def create_order(self, account, contract, action, order_type, quantity, limit_pr
420409
aux_price=aux_price, trail_stop_price=trail_stop_price,
421410
trailing_percent=trailing_percent, percent_offset=percent_offset,
422411
time_in_force=time_in_force, outside_rth=outside_rth, order_id=order_id,
423-
attach_type=attach_type, stop_loss_price=stop_loss_price, stop_loss_tif=stop_loss_tif,
424-
stop_loss_rth=stop_loss_rth, stop_loss_order_id=stop_loss_order_id,
425-
profit_taker_price=profit_taker_price, profit_taker_tif=profit_taker_tif,
426-
profit_taker_rth=profit_taker_rth, profit_taker_order_id=profit_taker_order_id)
412+
attach_order=attach_order)
427413
return order
428414
else:
429415
raise ApiException(response.code, response.message)
@@ -493,15 +479,7 @@ def place_order(self, order):
493479
params.percent_offset = order.percent_offset
494480
params.time_in_force = order.time_in_force
495481
params.outside_rth = order.outside_rth
496-
params.attach_type = order.attach_type
497-
params.stop_loss_price = order.stop_loss_price
498-
params.stop_loss_tif = order.stop_loss_tif
499-
params.stop_loss_rth = order.stop_loss_rth
500-
params.stop_loss_order_id = order.stop_loss_order_id
501-
params.profit_taker_price = order.profit_taker_price
502-
params.profit_taker_tif = order.profit_taker_tif
503-
params.profit_taker_rth = order.profit_taker_rth
504-
params.profit_taker_order_id = order.profit_taker_order_id
482+
params.attach_order = order.attach_order
505483

506484
request = OpenApiRequest(PLACE_ORDER, biz_model=params)
507485
response_content = self.__fetch_data(request)

0 commit comments

Comments
 (0)