Skip to content

Commit e980ecc

Browse files
author
Kai Su
committed
modified: tigeropen/quote/quote_client.py
modified: tigeropen/quote/response/option_briefs_response.py modified: tigeropen/trade/domain/contract.py modified: tigeropen/trade/response/__init__.py modified: tigeropen/trade/response/contracts_response.py modified: tigeropen/trade/response/orders_response.py modified: tigeropen/trade/response/positions_response.py modified: tigeropen/trade/trade_client.py modified: tigeropen/common/consts/quote_keys.py
1 parent 3ca2edd commit e980ecc

File tree

10 files changed

+116
-21
lines changed

10 files changed

+116
-21
lines changed

tigeropen/common/consts/quote_keys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class QuoteChangeKey(Enum):
2121

2222
@unique
2323
class QuoteKeyType(Enum):
24-
ALL = None # 所有行情数据
24+
ALL = 'askPrice,askSize,bidPrice,bidSize,open,high,low,close,preClose,volume,latestPrice,mi' # 所有行情数据
2525
QUOTE = 'askPrice,askSize,bidPrice,bidSize' # 盘口数据
2626
TRADE = 'open,high,low,close,preClose,volume,latestPrice' # 成交数据
2727
TIMELINE = 'mi' # 分时数据

tigeropen/examples/push_client_demo.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def on_query_subscribed_quote(symbols, focus_keys, limit, used):
1818
:param limit: 当前 tigerid 可以订阅的合约数量
1919
:param used: 目前已订阅的合约数量
2020
:return:
21+
返回示例:
22+
symbols: ['00700', 'SPY'],
23+
focus_keys: {'00700': ['ask_size', 'latest_price', 'ask_price', 'prev_close', 'open', 'minute', 'low', 'volume',
24+
'bid_price', 'bid_size', 'high', 'close'], 'SPY': ['ask_size', 'latest_price', 'ask_price', 'prev_close',
25+
'open', 'minute', 'low', 'volume', 'bid_price', 'bid_size', 'high', 'close']},
26+
limit: 100,
27+
used: 2
28+
2129
"""
2230
print(symbols, focus_keys, limit, used)
2331

@@ -29,19 +37,56 @@ def on_quote_changed(symbol, items, hour_trading):
2937
:param items: list,每个元素是一个tuple,对应订阅的字段名称和值
3038
:param hour_trading: 是否为盘前盘后的交易
3139
:return:
40+
items 数据示例
41+
[('latest_price', 339.8), ('ask_size', 42500), ('ask_price', 340.0), ('bid_size', 1400), ('bid_price', 339.8),
42+
('high', 345.0), ('prev_close', 342.4), ('low', 339.2), ('open', 344.0), ('volume', 7361440),
43+
('minute', {'p': 339.8, 'a': 341.084, 't': 1568098440000, 'v': 7000, 'h': 340.0, 'l': 339.8}),
44+
('timestamp', '1568098469463')]
3245
"""
3346
print(symbol, items, hour_trading)
3447

3548

3649
def on_order_changed(account, items):
50+
"""
51+
52+
:param account:
53+
:param items:
54+
:return:
55+
items 数据示例:
56+
[('order_type', 'LMT'), ('symbol', 'ABCD'), ('order_id', 1000101463), ('sec_type', 'STK'), ('filled', 100),
57+
('quantity', 100), ('segment', 'summary'), ('action', 'BUY'), ('currency', 'USD'), ('id', 173612806463631360),
58+
('order_time', 1568095814556), ('time_in_force', 'DAY'), ('identifier', 'ABCD'), ('limit_price', 113.7),
59+
('outside_rth', True), ('avg_fill_price', 113.7), ('trade_time', 1568095815418),
60+
('status', <OrderStatus.FILLED: 'Filled'>)]
61+
"""
3762
print(account, items)
3863

3964

4065
def on_asset_changed(account, items):
66+
"""
67+
68+
:param account:
69+
:param items:
70+
:return:
71+
items 数据示例:
72+
[('equity_with_loan', 721583.83), ('gross_position_value', 1339641.94),
73+
('excess_liquidity', 378624.18), ('available_funds', 320059.1), ('initial_margin_requirement', 497419.25),
74+
('buying_power', 2293551.51), ('cash', 950059.0), ('segment', 'summary'), ('net_liquidation', 817685.72),
75+
('maintenance_margin_requirement', 439061.54)]
76+
"""
4177
print(account, items)
4278

4379

4480
def on_position_changed(account, items):
81+
"""
82+
83+
:param account:
84+
:param items:
85+
:return:
86+
items 数据示例:
87+
[('symbol', 'ABCD'), ('market_price', 3.68525), ('market_value', 0.0), ('sec_type', 'STK'),
88+
('segment', 'summary'), ('currency', 'USD'), ('quantity', 0.0), ('average_cost', 3.884548)]
89+
"""
4590
print(account, items)
4691

4792

tigeropen/quote/quote_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ def get_option_briefs(self, identifiers):
461461
open: 开盘价
462462
high: 最高价
463463
low: 最低价
464+
rates_bonds: 无风险利率
465+
volatility: 历史波动率
464466
"""
465467
params = MultipleContractParams()
466468
contracts = []

tigeropen/quote/response/option_briefs_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
COLUMNS = ['identifier', 'symbol', 'expiry', 'strike', 'put_call', 'multiplier', 'ask_price', 'ask_size', 'bid_price',
1515
'bid_size', 'pre_close', 'latest_price', 'latest_time', 'volume', 'open_interest', 'open', 'high', 'low',
16-
'rates_bonds']
16+
'rates_bonds', 'volatility']
1717
BRIEF_FIELD_MAPPINGS = {'askPrice': 'ask_price', 'askSize': 'ask_size', 'bidPrice': 'bid_price', 'bidSize': 'bid_size',
1818
'latestPrice': 'latest_price', 'openInterest': 'open_interest', 'preClose': 'pre_close',
1919
'right': 'put_call', 'latestTime': 'latest_time', 'openInt': 'open_interest',

tigeropen/trade/domain/contract.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
55
@author: gaoan
66
"""
7+
from tigeropen.common.consts import SecurityType
78

89

910
class Contract(object):
1011
def __init__(self, symbol, currency, contract_id=None, sec_type=None, exchange=None, origin_symbol=None,
1112
local_symbol=None, expiry=None, strike=None, put_call=None, multiplier=None, name=None,
1213
short_margin=None, short_fee_rate=None, shortable=None, long_initial_margin=None,
13-
long_maintenance_margin=None, contract_month=None):
14+
long_maintenance_margin=None, contract_month=None, identifier=None):
1415
self.contract_id = contract_id
1516
self.symbol = symbol
1617
self.currency = currency
@@ -19,27 +20,35 @@ def __init__(self, symbol, currency, contract_id=None, sec_type=None, exchange=N
1920
self.origin_symbol = origin_symbol
2021
self.local_symbol = local_symbol
2122
self.expiry = expiry
23+
# 行权价
2224
self.strike = strike
25+
# 看跌/看涨
2326
self.put_call = put_call
27+
# 合约乘数
2428
self.multiplier = multiplier
29+
# 合约名称
2530
self.name = name
31+
# 做空保证金比例
2632
self.short_margin = short_margin
33+
# 做空费率
2734
self.short_fee_rate = short_fee_rate
35+
# 做空池剩余
2836
self.shortable = shortable
37+
# 做多初始保证金
2938
self.long_initial_margin = long_initial_margin
39+
# 做多维持保证金
3040
self.long_maintenance_margin = long_maintenance_margin
41+
# 合约月份
3142
self.contract_month = contract_month
43+
# 合约标识符
44+
self.identifier = identifier
3245

3346
def __repr__(self):
34-
if self.symbol:
35-
if self.origin_symbol is not None:
36-
return self.origin_symbol
37-
elif self.contract_id:
38-
return '%s/%s/%s/%d' % (self.symbol, self.sec_type, self.currency, self.contract_id)
39-
else:
40-
return '%s/%s/%s' % (self.symbol, self.sec_type, self.currency)
47+
identifier = self.identifier if self.identifier else self.symbol
48+
if self.sec_type == SecurityType.FUT.value:
49+
return '%s/%s/%s/%s' % (identifier, self.sec_type, self.currency, self.exchange)
4150
else:
42-
return '%d' % (self.contract_id,)
51+
return '%s/%s/%s' % (identifier, self.sec_type, self.currency)
4352

4453
def is_cn_stock(self):
4554
"""

tigeropen/trade/response/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
CONTRACT_FIELDS = set(['symbol', 'market', 'multiplier', 'sec_type', 'currency', 'local_symbol', 'origin_symbol',
99
'expiry', 'strike', 'right', 'contract_id', 'exchange', 'name', 'short_margin', 'short_fee_rate',
10-
'shortable', 'long_initial_margin', 'long_maintenance_margin', 'contract_month'])
10+
'shortable', 'long_initial_margin', 'long_maintenance_margin', 'contract_month', 'identifier'])
1111

tigeropen/trade/response/contracts_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ def parse_response_content(self, response_content):
6161
long_initial_margin = contract_fields.get('long_initial_margin')
6262
long_maintenance_margin = contract_fields.get('long_maintenance_margin')
6363
contract_month = contract_fields.get('contract_month')
64+
identifier = contract_fields.get('identifier')
6465
contract = Contract(symbol, currency, contract_id=contract_id, sec_type=sec_type, exchange=exchange,
6566
origin_symbol=origin_symbol, local_symbol=local_symbol, expiry=expiry,
6667
strike=strike, put_call=put_call, multiplier=multiplier, name=name,
6768
short_margin=short_margin, short_fee_rate=short_fee_rate, shortable=shortable,
6869
long_initial_margin=long_initial_margin, contract_month=contract_month,
69-
long_maintenance_margin=long_maintenance_margin)
70+
long_maintenance_margin=long_maintenance_margin, identifier=identifier)
7071
self.contracts.append(contract)

tigeropen/trade/response/orders_response.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'timeInForce': 'time_in_force', 'openTime': 'order_time', 'latestTime': 'trade_time',
2323
'contractId': 'contract_id',
2424
'trailStopPrice': 'trail_stop_price', 'trailingPercent': 'trailing_percent',
25-
'percentOffset': 'percent_offset'}
25+
'percentOffset': 'percent_offset', 'identifier': 'identifier'}
2626

2727

2828
class OrdersResponse(TigerResponse):
@@ -74,9 +74,10 @@ def parse_order(item):
7474
strike = contract_fields.get('strike')
7575
put_call = contract_fields.get('right')
7676
multiplier = contract_fields.get('multiplier')
77+
identifier = contract_fields.get('identifier')
7778
contract = Contract(symbol, currency, contract_id=contract_id, sec_type=sec_type, exchange=exchange,
7879
origin_symbol=origin_symbol, local_symbol=local_symbol, expiry=expiry,
79-
strike=strike, put_call=put_call, multiplier=multiplier)
80+
strike=strike, put_call=put_call, multiplier=multiplier, identifier=identifier)
8081
account = order_fields.get('account')
8182
action = order_fields.get('action')
8283
order_type = order_fields.get('order_type')

tigeropen/trade/response/positions_response.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
from tigeropen.trade.response import CONTRACT_FIELDS
1414

1515
POSITION_FIELD_MAPPINGS = {'averageCost': 'average_cost', 'position': 'quantity', 'latestPrice': 'market_price',
16-
'marketValue': 'market_value', 'orderType': 'order_type', 'realizedPnl': 'realized_pnl',
17-
'unrealizedPnl': 'unrealized_pnl', 'secType': 'sec_type', 'localSymbol': 'local_symbol',
18-
'originSymbol': 'origin_symbol', 'contractId':'contract_id'}
16+
'marketValue': 'market_value', 'orderType': 'order_type', 'realizedPnl': 'realized_pnl',
17+
'unrealizedPnl': 'unrealized_pnl', 'secType': 'sec_type', 'localSymbol': 'local_symbol',
18+
'originSymbol': 'origin_symbol', 'contractId': 'contract_id', 'identifier': 'identifier'}
1919

2020

2121
class PositionsResponse(TigerResponse):
@@ -57,9 +57,11 @@ def parse_response_content(self, response_content):
5757
strike = contract_fields.get('strike')
5858
put_call = contract_fields.get('right')
5959
multiplier = contract_fields.get('multiplier')
60+
identifier = contract_fields.get('identifier')
6061
contract = Contract(symbol, currency, contract_id=contract_id, sec_type=sec_type,
6162
exchange=exchange, origin_symbol=origin_symbol, local_symbol=local_symbol,
62-
expiry=expiry, strike=strike, put_call=put_call, multiplier=multiplier)
63+
expiry=expiry, strike=strike, put_call=put_call, multiplier=multiplier,
64+
identifier=identifier)
6365
account = position_fields.get('account')
6466
quantity = position_fields.get('quantity')
6567
average_cost = position_fields.get('average_cost')

tigeropen/trade/trade_client.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ def get_managed_accounts(self, account=None):
6262
return None
6363

6464
def get_contracts(self, symbol, sec_type=SecurityType.STK, currency=None, exchange=None):
65+
"""
66+
批量获取合约
67+
:param symbol:
68+
:param sec_type:
69+
:param currency:
70+
:param exchange:
71+
:return: 合约对象列表, 每个列表项的对象信息同 get_contract 返回
72+
"""
6573
params = ContractParams()
6674
params.account = self._account
6775
params.symbols = symbol if isinstance(symbol, list) else [symbol]
@@ -85,6 +93,33 @@ def get_contracts(self, symbol, sec_type=SecurityType.STK, currency=None, exchan
8593

8694
def get_contract(self, symbol, sec_type=SecurityType.STK, currency=None, exchange=None, expiry=None, strike=None,
8795
right=None):
96+
"""
97+
获取合约
98+
:param symbol:
99+
:param sec_type:
100+
:param currency:
101+
:param exchange:
102+
:param expiry:
103+
:param strike:
104+
:param right:
105+
:return: Contract 对象. 有如下属性:
106+
symbol: 合约 symbol
107+
identifier: 合约唯一标识
108+
currency: 币种
109+
exchange: 交易所
110+
name: 合约名称
111+
sec_type: 合约类型
112+
long_initial_margin: 做多初始保证金比例
113+
long_maintenance_margin: 做多维持保证金比例
114+
short_fee_rate: 做空费率
115+
short_margin: 做空保证金
116+
shortable: 做空池剩余
117+
multiplier: 合约乘数
118+
expiry: 合约到期日(期货/期权)
119+
contract_month: 合约月份(期货)
120+
strike: 行权价(期权)
121+
put_call: 看跌/看涨(期权)
122+
"""
88123
params = ContractParams()
89124
params.account = self._account
90125
params.symbol = symbol
@@ -218,7 +253,7 @@ def get_orders(self, account=None, sec_type=None, market=Market.ALL, symbol=None
218253
:param limit: 每次获取订单的数量
219254
:param is_brief: 是否返回精简的订单数据
220255
:param states: 订单状态枚举对象列表, 可选, 若传递则按状态筛选
221-
:return: Order 对象构成的列表
256+
:return: Order 对象构成的列表. Order 对象信息参见 tigeropen.trade.domain.order
222257
"""
223258
params = OrdersParams()
224259
params.account = account if account else self._account
@@ -321,7 +356,7 @@ def get_order(self, account=None, id=None, order_id=None, is_brief=False):
321356
:param id:
322357
:param order_id:
323358
:param is_brief: 是否返回精简的订单数据
324-
:return:
359+
:return: Order 对象. 对象信息参见 tigeropen.trade.domain.order
325360
"""
326361
params = OrderParams()
327362
params.account = account if account else self._account

0 commit comments

Comments
 (0)