Skip to content

Commit e37e883

Browse files
committed
future ticks upgrade to v3; get all future contracts by type;
stop loss trailing order
1 parent 8acab3d commit e37e883

File tree

11 files changed

+95
-31
lines changed

11 files changed

+95
-31
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 2.1.7 (2022-08-19)
2+
### New
3+
- 新增获取期货某类型所有合约接口 `QuoteClient.get_all_future_contracts`
4+
- 附加订单支持追踪止损单
5+
6+
### Breaking
7+
- 期货tick接口 `QuoteClient.get_future_trade_ticks`, 合约参数由接受列表改为只接受单个合约
8+
19
## 2.1.6 (2022-08-11)
210
### Modify
311
- 支持全局时区配置, 可通过 ClientConfig.timezone 设置时区

tigeropen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
55
@author: gaoan
66
"""
7-
__VERSION__ = '2.1.6'
7+
__VERSION__ = '2.1.7'

tigeropen/common/consts/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ class OrderSortBy(Enum):
160160
LATEST_CREATED = 'LATEST_CREATED'
161161
LATEST_STATUS_UPDATED = 'LATEST_STATUS_UPDATED'
162162

163+
164+
@unique
165+
class OrderType(Enum):
166+
MKT = 'MKT' # 市价单
167+
LMT = 'LMT' # 限价单
168+
STP = 'STP' # 止损单
169+
STP_LMT = 'STP_LMT' # 止损限价单
170+
TRAIL = 'TRAIL' # 跟踪止损单

tigeropen/common/consts/service_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
FUTURE_EXCHANGE = "future_exchange"
6565
FUTURE_CONTRACT_BY_CONTRACT_CODE = "future_contract_by_contract_code"
6666
FUTURE_CONTRACT_BY_EXCHANGE_CODE = "future_contract_by_exchange_code"
67+
FUTURE_CONTRACTS = "future_contracts"
6768
FUTURE_CONTINUOUS_CONTRACTS = "future_continuous_contracts"
6869
FUTURE_CURRENT_CONTRACT = "future_current_contract"
6970
FUTURE_KLINE = "future_kline"

tigeropen/common/util/order_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,20 @@ 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(leg_type, price, time_in_force='DAY', outside_rth=None):
77+
def order_leg(leg_type, price, time_in_force='DAY', outside_rth=None, limit_price=None, trailing_percent=None,
78+
trailing_amount=None):
7879
"""
7980
附加订单
8081
:param leg_type: 附加订单类型. PROFIT 止盈单类型, LOSS 止损单类型
8182
:param price: 附加订单价格.
8283
:param time_in_force: 附加订单有效期. 'DAY'(当日有效)和'GTC'(取消前有效 Good-Til-Canceled).
8384
:param outside_rth: 附加订单是否允许盘前盘后交易(美股专属). True 允许, False 不允许.
85+
:param limit_price: attached stop loss order's limit price
86+
:param trailing_percent: attached trailing stop loss order's trailing percent
87+
:param trailing_amount: attached trailing stop loss order's trailing amount
8488
"""
85-
return OrderLeg(leg_type=leg_type, price=price, time_in_force=time_in_force, outside_rth=outside_rth)
89+
return OrderLeg(leg_type=leg_type, price=price, time_in_force=time_in_force, outside_rth=outside_rth,
90+
limit_price=limit_price, trailing_percent=trailing_percent, trailing_amount=trailing_amount)
8691

8792

8893
def limit_order_with_legs(account, contract, action, quantity, limit_price, order_legs=None):

tigeropen/examples/quote_client_demo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@ def get_future_quote():
112112
print(exchanges)
113113
bars = openapi_client.get_future_bars(['CN1901'], begin_time=-1, end_time=1545105097358)
114114
print(bars)
115-
ticks = openapi_client.get_future_trade_ticks(['CN1901'])
115+
ticks = openapi_client.get_future_trade_ticks('CN2209')
116116
print(ticks)
117117
contracts = openapi_client.get_future_contracts('CME')
118118
print(contracts)
119+
contracts = openapi_client.get_all_future_contracts('CL')
120+
print(contracts)
119121
contract = openapi_client.get_future_contract('VIX2206')
120122
print(contract)
121123
trading_times = openapi_client.get_future_trading_times('CN1901', trading_date=1545049282852)

tigeropen/quote/quote_client.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from tigeropen.common.consts import Market, QuoteRight, BarPeriod, OPEN_API_SERVICE_VERSION_V3
1414
from tigeropen.common.consts import THREAD_LOCAL, SecurityType, CorporateActionType, IndustryLevel
1515
from tigeropen.common.consts.service_types import GRAB_QUOTE_PERMISSION, QUOTE_DELAY, GET_QUOTE_PERMISSION, \
16-
HISTORY_TIMELINE, FUTURE_CONTRACT_BY_CONTRACT_CODE, TRADING_CALENDAR
16+
HISTORY_TIMELINE, FUTURE_CONTRACT_BY_CONTRACT_CODE, TRADING_CALENDAR, FUTURE_CONTRACTS
1717
from tigeropen.common.consts.service_types import MARKET_STATE, ALL_SYMBOLS, ALL_SYMBOL_NAMES, BRIEF, \
1818
TIMELINE, KLINE, TRADE_TICK, OPTION_EXPIRATION, OPTION_CHAIN, FUTURE_EXCHANGE, OPTION_BRIEF, \
1919
OPTION_KLINE, OPTION_TRADE_TICK, FUTURE_KLINE, FUTURE_TICK, FUTURE_CONTRACT_BY_EXCHANGE_CODE, \
@@ -72,8 +72,8 @@ class QuoteClient(TigerOpenClient):
7272

7373
def __init__(self, client_config, logger=None, is_grab_permission=True):
7474
if not logger:
75-
logger = logging.getLogger('tiger_openapi')
76-
super(QuoteClient, self).__init__(client_config, logger=logger)
75+
self.logger = logging.getLogger('tiger_openapi')
76+
super(QuoteClient, self).__init__(client_config, logger=self.logger)
7777
self._lang = LANGUAGE
7878
self._timezone = eastern
7979
if client_config:
@@ -83,7 +83,7 @@ def __init__(self, client_config, logger=None, is_grab_permission=True):
8383
self.permissions = None
8484
if is_grab_permission and self.permissions is None:
8585
self.permissions = self.grab_quote_permission()
86-
logger.info('Grab quote permission. Permissions:' + str(self.permissions))
86+
self.logger.info('Grab quote permission. Permissions:' + str(self.permissions))
8787

8888
def __fetch_data(self, request):
8989
try:
@@ -890,6 +890,28 @@ def get_current_future_contract(self, future_type, lang=None):
890890
raise ApiException(response.code, response.message)
891891
return None
892892

893+
def get_all_future_contracts(self, future_type, lang=None):
894+
"""
895+
Query all contracts of a given type
896+
:param future_type: like CL, VIX
897+
:param lang: language
898+
:return: same as "get_current_future_contract"
899+
"""
900+
params = FutureContractParams()
901+
params.type = future_type
902+
params.lang = get_enum_value(lang) if lang else get_enum_value(self._lang)
903+
904+
request = OpenApiRequest(FUTURE_CONTRACTS, biz_model=params)
905+
response_content = self.__fetch_data(request)
906+
if response_content:
907+
response = FutureContractResponse()
908+
response.parse_response_content(response_content)
909+
if response.is_success():
910+
return response.contracts
911+
else:
912+
raise ApiException(response.code, response.message)
913+
return None
914+
893915
def get_future_contract(self, contract_code, lang=None):
894916
"""
895917
get future contract by contract_code
@@ -1014,10 +1036,10 @@ def get_future_bars_by_page(self, identifier, period=BarPeriod.DAY, begin_time=-
10141036
time.sleep(time_interval)
10151037
return pd.concat(result).sort_values('time').reset_index(drop=True)
10161038

1017-
def get_future_trade_ticks(self, identifiers, begin_index=0, end_index=30, limit=1000):
1039+
def get_future_trade_ticks(self, identifier, begin_index=0, end_index=30, limit=1000):
10181040
"""
10191041
获取期货逐笔成交
1020-
:param identifiers: 期货代码列表
1042+
:param identifier: future identifier. Only supports one identifier
10211043
:param begin_index: 开始索引
10221044
:param end_index: 结束索引
10231045
:param limit: 数量限制
@@ -1028,11 +1050,16 @@ def get_future_trade_ticks(self, identifiers, begin_index=0, end_index=30, limit
10281050
volume: 成交量
10291051
"""
10301052
params = FutureQuoteParams()
1031-
params.contract_codes = identifiers
1053+
# Compatible with previous version (previous version 'identifiers' argument is a list)
1054+
params.contract_code = identifier
1055+
if isinstance(identifier, list):
1056+
self.logger.warning("the 'identifier' argument should be a string")
1057+
params.contract_code = identifier[0]
10321058
params.begin_index = begin_index
10331059
params.end_index = end_index
10341060
params.limit = limit
10351061
params.lang = get_enum_value(self._lang)
1062+
params.version = OPEN_API_SERVICE_VERSION_V3
10361063
request = OpenApiRequest(FUTURE_TICK, biz_model=params)
10371064
response_content = self.__fetch_data(request)
10381065
if response_content:

tigeropen/quote/request/model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ def to_openapi_dict(self):
601601
class FutureQuoteParams(MarketParams):
602602
def __init__(self):
603603
super(FutureQuoteParams, self).__init__()
604+
self._contract_code = None
604605
self._contract_codes = None
605606
self._period = None
606607
self._begin_time = None
@@ -610,6 +611,14 @@ def __init__(self):
610611
self._end_index = None
611612
self._page_token = None
612613

614+
@property
615+
def contract_code(self):
616+
return self._contract_code
617+
618+
@contract_code.setter
619+
def contract_code(self, value):
620+
self._contract_code = value
621+
613622
@property
614623
def contract_codes(self):
615624
return self._contract_codes
@@ -677,6 +686,9 @@ def page_token(self, value):
677686
def to_openapi_dict(self):
678687
params = super(FutureQuoteParams, self).to_openapi_dict()
679688

689+
if self.contract_code:
690+
params['contract_code'] = self.contract_code
691+
680692
if self.contract_codes:
681693
params['contract_codes'] = self.contract_codes
682694

tigeropen/quote/response/future_quote_ticks_response.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
from tigeropen.common.response import TigerResponse
1010

11-
COLUMNS = ['identifier', 'index', 'time', 'price', 'volume']
12-
1311

1412
class FutureTradeTickResponse(TigerResponse):
1513
def __init__(self):
@@ -22,17 +20,6 @@ def parse_response_content(self, response_content):
2220
if 'is_success' in response:
2321
self._is_success = response['is_success']
2422

25-
if self.data and isinstance(self.data, list):
26-
tick_items = []
27-
for symbol_item in self.data:
28-
identifier = symbol_item.get('contractCode')
29-
if 'items' in symbol_item:
30-
for item in symbol_item['items'][::-1]:
31-
item_values = {'identifier': identifier}
32-
for key, value in item.items():
33-
if value is None:
34-
continue
35-
item_values[key] = value
36-
tick_items.append([item_values.get(tag) for tag in COLUMNS])
37-
38-
self.trade_ticks = pd.DataFrame(tick_items, columns=COLUMNS)
23+
if self.data:
24+
self.trade_ticks = pd.DataFrame(self.data.get('items', []))
25+
self.trade_ticks.insert(0, column='identifier', value=self.data.get('contractCode'))

tigeropen/trade/domain/order.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,24 @@ class OrderLeg:
143143
附加订单
144144
"""
145145

146-
def __init__(self, leg_type, price, time_in_force='DAY', outside_rth=None):
146+
def __init__(self, leg_type, price, time_in_force='DAY', outside_rth=None, limit_price=None, trailing_percent=None,
147+
trailing_amount=None):
147148
"""
148-
:param leg_type: 附加订单类型(仅限价单支持). PROFIT 止盈单类型, LOSS 止损单类型
149-
:param price: 附加订单价格
150-
:param time_in_force: 附加订单有效期. 'DAY'(当日有效)和'GTC'(取消前有效).
149+
:param leg_type: 附加订单类型(仅限价单支持). PROFIT 止盈单; LOSS 止损单
150+
:param price: 附加订单触发价格
151+
:param time_in_force: 附加订单有效期. 'DAY'(当日有效)和 'GTC'(取消前有效).
151152
:param outside_rth: 附加订单是否允许盘前盘后交易(美股专属). True 允许, False 不允许.
153+
:param limit_price: attached stop loss order's limit price
154+
:param trailing_percent: attached trailing stop loss order's trailing percent
155+
:param trailing_amount: attached trailing stop loss order's trailing amount
152156
"""
153157
self.leg_type = leg_type
154158
self.price = price
155159
self.time_in_force = time_in_force
156160
self.outside_rth = outside_rth
161+
self.limit_price = limit_price
162+
self.trailing_percent = trailing_percent
163+
self.trailing_amount = trailing_amount
157164

158165
def to_dict(self):
159166
return self.__dict__

0 commit comments

Comments
 (0)