Skip to content

Commit 8eaf5b2

Browse files
committed
Merge branch 'feature_future_contract_by_code' into 'dev'
add api get future contract by code; get derivative contracts See merge request server/openapi/openapi-python-sdk!107
2 parents 5a9b6d5 + dd81f6a commit 8eaf5b2

File tree

7 files changed

+92
-5
lines changed

7 files changed

+92
-5
lines changed

tigeropen/common/consts/service_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"""
3030
CONTRACT = "contract"
3131
CONTRACTS = "contracts"
32+
QUOTE_CONTRACT = "quote_contract"
3233

3334
"""
3435
行情

tigeropen/examples/quote_client_demo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ def get_future_quote():
9999
print(ticks)
100100
contracts = openapi_client.get_future_contracts('CME')
101101
print(contracts)
102+
contract = openapi_client.get_future_contract('VIX2206')
103+
print(contract)
102104
trading_times = openapi_client.get_future_trading_times('CN1901', trading_date=1545049282852)
103105
print(trading_times)
104106
briefs = openapi_client.get_future_brief(['ES1906', 'CN1901'])
105107
print(briefs)
106108

107109

110+
108111
def get_fundamental():
109112
"""获取基础数据"""
110113

tigeropen/examples/trade_client_demo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
client_config = get_client_config()
2929

3030

31+
def get_contract_apis():
32+
openapi_client = TradeClient(client_config, logger=logger)
33+
contract = openapi_client.get_contracts('AAPL')[0]
34+
print(contract)
35+
contract = openapi_client.get_contract('AAPL', SecurityType.STK, currency=Currency.USD)
36+
print(contract)
37+
# get derivative contracts of stock. include OPT, WAR, IOPT
38+
contracts = openapi_client.get_derivative_contracts('00700', SecurityType.WAR, '20220929')
39+
print(contracts)
40+
3141
def get_account_apis():
3242
openapi_client = TradeClient(client_config, logger=logger)
3343
openapi_client.get_managed_accounts()

tigeropen/quote/quote_client.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from tigeropen.common.consts import Market, Language, QuoteRight, BarPeriod, OPEN_API_SERVICE_VERSION_V3
1313
from tigeropen.common.consts import THREAD_LOCAL, SecurityType, CorporateActionType, IndustryLevel
1414
from tigeropen.common.consts.service_types import GRAB_QUOTE_PERMISSION, QUOTE_DELAY, GET_QUOTE_PERMISSION, \
15-
HISTORY_TIMELINE
15+
HISTORY_TIMELINE, FUTURE_CONTRACT_BY_CONTRACT_CODE
1616
from tigeropen.common.consts.service_types import MARKET_STATE, ALL_SYMBOLS, ALL_SYMBOL_NAMES, BRIEF, \
1717
TIMELINE, KLINE, TRADE_TICK, OPTION_EXPIRATION, OPTION_CHAIN, FUTURE_EXCHANGE, OPTION_BRIEF, \
1818
OPTION_KLINE, OPTION_TRADE_TICK, FUTURE_KLINE, FUTURE_TICK, FUTURE_CONTRACT_BY_EXCHANGE_CODE, \
@@ -34,7 +34,7 @@
3434
StockIndustryResponse
3535
from tigeropen.quote.domain.filter import OptionFilter
3636
from tigeropen.quote.request.model import MarketParams, MultipleQuoteParams, MultipleContractParams, \
37-
FutureQuoteParams, FutureExchangeParams, FutureTypeParams, FutureTradingTimeParams, SingleContractParams, \
37+
FutureQuoteParams, FutureExchangeParams, FutureContractParams, FutureTradingTimeParams, SingleContractParams, \
3838
SingleOptionQuoteParams, DepthQuoteParams, OptionChainParams
3939
from tigeropen.quote.response.future_briefs_response import FutureBriefsResponse
4040
from tigeropen.quote.response.future_contract_response import FutureContractResponse
@@ -831,7 +831,7 @@ def get_current_future_contract(self, future_type, lang=None):
831831
trade: 是否可交易
832832
continuous: 是否为连续合约
833833
"""
834-
params = FutureTypeParams()
834+
params = FutureContractParams()
835835
params.type = future_type
836836
params.lang = get_enum_value(lang) if lang else get_enum_value(self._lang)
837837

@@ -846,6 +846,28 @@ def get_current_future_contract(self, future_type, lang=None):
846846
raise ApiException(response.code, response.message)
847847
return None
848848

849+
def get_future_contract(self, contract_code, lang=None):
850+
"""
851+
get future contract by contract_code
852+
:param contract_code: code of future contract, like VIX2206, CL2203
853+
:param lang:
854+
:return: pandas.DataFrame
855+
"""
856+
params = FutureContractParams()
857+
params.contract_code = contract_code
858+
params.lang = get_enum_value(lang) if lang else get_enum_value(self._lang)
859+
860+
request = OpenApiRequest(FUTURE_CONTRACT_BY_CONTRACT_CODE, biz_model=params)
861+
response_content = self.__fetch_data(request)
862+
if response_content:
863+
response = FutureContractResponse()
864+
response.parse_response_content(response_content)
865+
if response.is_success():
866+
return response.contracts
867+
else:
868+
raise ApiException(response.code, response.message)
869+
return None
870+
849871
def get_future_trading_times(self, identifier, trading_date=None):
850872
"""
851873
查询指定期货合约的交易时间

tigeropen/quote/request/model.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,10 @@ def to_openapi_dict(self):
532532
return params
533533

534534

535-
class FutureTypeParams(BaseParams):
535+
class FutureContractParams(BaseParams):
536536
def __init__(self):
537537
self._type = None # 期货品种
538+
self._contract_code = None # 期货代码
538539
self._lang = None # 语言
539540

540541
@property
@@ -545,6 +546,14 @@ def type(self):
545546
def type(self, value):
546547
self._type = value
547548

549+
@property
550+
def contract_code(self):
551+
return self._contract_code
552+
553+
@contract_code.setter
554+
def contract_code(self, value):
555+
self._contract_code = value
556+
548557
@property
549558
def lang(self):
550559
return self._lang
@@ -558,6 +567,9 @@ def to_openapi_dict(self):
558567
if self.type:
559568
params['type'] = self.type
560569

570+
if self.contract_code:
571+
params['contract_code'] = self.contract_code
572+
561573
if self.lang:
562574
params['lang'] = self.lang
563575

tigeropen/trade/request/model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def __init__(self):
213213
self._expiry = None
214214
self._strike = None
215215
self._right = None
216+
self._lang = None
216217

217218
@property
218219
def account(self):
@@ -294,6 +295,14 @@ def right(self):
294295
def right(self, value):
295296
self._right = value
296297

298+
@property
299+
def lang(self):
300+
return self._lang
301+
302+
@lang.setter
303+
def lang(self, value):
304+
self._lang = value
305+
297306
def to_openapi_dict(self):
298307
params = dict()
299308
if self.account:
@@ -326,6 +335,9 @@ def to_openapi_dict(self):
326335
if self.right:
327336
params['right'] = self.right
328337

338+
if self.lang:
339+
params['lang'] = self.lang
340+
329341
return params
330342

331343

tigeropen/trade/trade_client.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from tigeropen.common.consts import THREAD_LOCAL, SecurityType, Market, Currency
1010
from tigeropen.common.consts.service_types import CONTRACTS, ACCOUNTS, POSITIONS, ASSETS, ORDERS, ORDER_NO, \
1111
CANCEL_ORDER, MODIFY_ORDER, PLACE_ORDER, ACTIVE_ORDERS, INACTIVE_ORDERS, FILLED_ORDERS, CONTRACT, PREVIEW_ORDER, \
12-
PRIME_ASSETS, ORDER_TRANSACTIONS
12+
PRIME_ASSETS, ORDER_TRANSACTIONS, QUOTE_CONTRACT
1313
from tigeropen.common.exceptions import ApiException
1414
from tigeropen.common.util.common_utils import get_enum_value
1515
from tigeropen.common.request import OpenApiRequest
@@ -147,6 +147,33 @@ def get_contract(self, symbol, sec_type=SecurityType.STK, currency=None, exchang
147147

148148
return None
149149

150+
def get_derivative_contracts(self, symbol, sec_type, expiry, lang=None):
151+
"""
152+
153+
:param symbol:
154+
:param sec_type: type of contract. tigeropen.common.consts.SecurityType. support: OPTION, WAR, IOPT
155+
:param expiry: expiry date string, like '20220929'
156+
:param lang:
157+
:return: list of Contract
158+
"""
159+
params = ContractParams()
160+
params.symbols = symbol if isinstance(symbol, list) else [symbol]
161+
params.sec_type = get_enum_value(sec_type)
162+
params.expiry = expiry
163+
params.lang = get_enum_value(lang) if lang else get_enum_value(self._lang)
164+
165+
request = OpenApiRequest(QUOTE_CONTRACT, biz_model=params)
166+
response_content = self.__fetch_data(request)
167+
if response_content:
168+
response = ContractsResponse()
169+
response.parse_response_content(response_content)
170+
if response.is_success():
171+
return response.contracts
172+
else:
173+
raise ApiException(response.code, response.message)
174+
175+
return None
176+
150177
def get_positions(self, account=None, sec_type=SecurityType.STK, currency=Currency.ALL, market=Market.ALL,
151178
symbol=None, sub_accounts=None):
152179
"""

0 commit comments

Comments
 (0)