Skip to content

Commit d1d205a

Browse files
committed
Merge branch 'level2' into 'dev'
Level2 -> dev See merge request !34
2 parents cb8f55c + b555341 commit d1d205a

File tree

8 files changed

+113
-6
lines changed

8 files changed

+113
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ __pycache__/
44
build/
55
dist/
66
eggs/
7+
*.egg-info
78

89
.idea
910
.DS_Store

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
logbook
21
six
32
simplejson
43
delorean
5-
pandas
4+
pandas==0.24.2
65
python-dateutil
76
pytz
87
pyasn1==0.4.2

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
55
@author: gaoan
66
"""
7+
from os import path
78
from setuptools import find_packages, setup
89

9-
install_requires = ['six', 'simplejson', 'delorean', 'pandas', 'python-dateutil', 'pytz', 'pyasn1==0.4.4', 'rsa==4.0', 'stomp.py']
10+
with open(path.join(path.abspath(path.dirname(__file__)), 'requirements.txt'), encoding='utf-8') as f:
11+
install_requires = f.read()
1012

1113
setup(
1214
name='tigeropen',
13-
version='1.1.7',
15+
version='1.1.8',
1416
description='TigerBrokers Open API',
1517
packages=find_packages(exclude=[]),
1618
author='TigerBrokers',

tigeropen/common/consts/service_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
QUOTE_REAL_TIME = "quote_real_time"
4343
QUOTE_SHORTABLE_STOCKS = "quote_shortable_stocks"
4444
QUOTE_STOCK_TRADE = "quote_stock_trade"
45+
ORDER_BOOK = "ask_bid" # level2 深度行情
4546

4647
# 期权行情
4748
OPTION_EXPIRATION = "option_expiration"

tigeropen/examples/quote_client_demo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def get_quote():
3939
print(ticks)
4040
short_interest = openapi_client.get_short_interest(['GOOG', 'AAPL', '00700'])
4141
print(short_interest)
42+
# 获取深度行情
43+
order_book = openapi_client.get_order_book(['02828'])
44+
print(order_book)
4245

4346

4447
def get_option_quote():

tigeropen/quote/quote_client.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@
3535
from tigeropen.quote.response.stock_trade_meta_response import TradeMetaResponse
3636
from tigeropen.quote.response.symbol_names_response import SymbolNamesResponse
3737
from tigeropen.quote.response.symbols_response import SymbolsResponse
38+
from tigeropen.quote.response.quote_order_book_response import OrderBookResponse
3839
from tigeropen.tiger_open_client import TigerOpenClient
3940
from tigeropen.quote.request.model import MarketParams, MultipleQuoteParams, MultipleContractParams, \
4041
FutureQuoteParams, FutureExchangeParams, FutureTypeParams, FutureTradingTimeParams, SingleContractParams, \
41-
SingleOptionQuoteParams
42+
SingleOptionQuoteParams, OrderBookParams
4243
from tigeropen.quote.request import OpenApiRequest
4344
from tigeropen.quote.response.quote_ticks_response import TradeTickResponse
4445
from tigeropen.quote.response.market_status_response import MarketStatusResponse
4546
from tigeropen.common.consts.service_types import MARKET_STATE, ALL_SYMBOLS, ALL_SYMBOL_NAMES, BRIEF, \
4647
TIMELINE, KLINE, TRADE_TICK, OPTION_EXPIRATION, OPTION_CHAIN, FUTURE_EXCHANGE, OPTION_BRIEF, \
4748
OPTION_KLINE, OPTION_TRADE_TICK, FUTURE_KLINE, FUTURE_TICK, FUTURE_CONTRACT_BY_EXCHANGE_CODE, \
4849
FUTURE_TRADING_DATE, QUOTE_SHORTABLE_STOCKS, FUTURE_REAL_TIME_QUOTE, \
49-
FUTURE_CURRENT_CONTRACT, QUOTE_REAL_TIME, QUOTE_STOCK_TRADE, FINANCIAL_DAILY, FINANCIAL_REPORT, CORPORATE_ACTION
50+
FUTURE_CURRENT_CONTRACT, QUOTE_REAL_TIME, QUOTE_STOCK_TRADE, FINANCIAL_DAILY, FINANCIAL_REPORT, CORPORATE_ACTION, \
51+
ORDER_BOOK
5052
from tigeropen.common.consts import Market, Language, QuoteRight, BarPeriod
5153
from tigeropen.common.util.contract_utils import extract_option_info
5254
from tigeropen.common.util.common_utils import eastern
@@ -373,6 +375,54 @@ def get_short_interest(self, symbols, lang=None):
373375

374376
return None
375377

378+
def get_order_book(self, symbols):
379+
"""
380+
获取深度行情
381+
:param symbols:
382+
:return:
383+
数据结构:
384+
若返回单个 symbol:
385+
{'symbol': '02833',
386+
'asks': [(27.4, 300, 2), (27.45, 500, 1), (27.5, 4400, 1), (27.55, 0, 0), (27.6, 5700, 3), (27.65, 0, 0),
387+
(27.7, 500, 1), (27.75, 0, 0), (27.8, 0, 0), (27.85, 0, 0)],
388+
'bids': [(27, 4000, 3), (26.95, 200, 1), (26.9, 0, 0), (26.85, 400, 1), (26.8, 0, 0), (26.75, 0, 0),
389+
(26.7, 0, 0), (26.65, 0, 0), (26.6, 0, 0), (26.55, 0, 0)]
390+
}
391+
392+
若返回多个 symbol:
393+
{'02833':
394+
{'symbol': '02833',
395+
'asks': [(27.35, 200, 1), (27.4, 2100, 2), (27.45, 500, 1), (27.5, 4400, 1), (27.55, 0, 0),
396+
(27.6, 5700, 3), (27.65, 0, 0), (27.7, 500, 1), (27.75, 0, 0), (27.8, 0, 0)],
397+
'bids': [(27.05, 100, 1), (27, 5000, 4), (26.95, 200, 1), (26.9, 0, 0), (26.85, 400, 1), (26.8, 0, 0),
398+
(26.75, 0, 0), (26.7, 0, 0), (26.65, 0, 0), (26.6, 0, 0)]
399+
},
400+
'02828':
401+
{'symbol': '02828',
402+
'asks': [(106.6, 6800, 7), (106.7, 110200, 10), (106.8, 64400, 8), (106.9, 80600, 8), (107, 9440, 16),
403+
(107.1, 31800, 5), (107.2, 11800, 4), (107.3, 9800, 2), (107.4, 9400, 1), (107.5, 21000, 9)],
404+
'bids': [(106.5, 62800, 17), (106.4, 68200, 9), (106.3, 78400, 6), (106.2, 52400, 4), (106.1, 3060, 4),
405+
(106, 33400, 4), (105.9, 29600, 3), (105.8, 9600, 2), (105.7, 15200, 2), (105.6, 0, 0)]}
406+
}
407+
408+
asks 和 bids 列表项数据含义为:
409+
[(ask_price1, ask_volume1, order_count), (ask_price2, ask_volume2, order_count), ...]
410+
[(bid_price1, bid_volume2, order_count), (bid_price2, bid_volume2, order_count), ...]
411+
412+
"""
413+
params = OrderBookParams()
414+
params.symbols = symbols if isinstance(symbols, list) else [symbols]
415+
416+
request = OpenApiRequest(ORDER_BOOK, biz_model=params)
417+
response_content = self.__fetch_data(request)
418+
if response_content:
419+
response = OrderBookResponse()
420+
response.parse_response_content(response_content)
421+
if response.is_success():
422+
return response.order_book
423+
else:
424+
raise ApiException(response.code, response.message)
425+
376426
def get_option_expirations(self, symbols):
377427
"""
378428
返回美股期权的过期日

tigeropen/quote/request/model.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,22 @@ def to_openapi_dict(self):
648648
params['limit'] = self.limit
649649

650650
return params
651+
652+
653+
class OrderBookParams(object):
654+
def __init__(self):
655+
self._symbols = None
656+
657+
@property
658+
def symbols(self):
659+
return self._symbols
660+
661+
@symbols.setter
662+
def symbols(self, value):
663+
self._symbols = value
664+
665+
def to_openapi_dict(self):
666+
params = dict()
667+
if self.symbols:
668+
params['symbols'] = self.symbols
669+
return params
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
from tigeropen.common.response import TigerResponse
3+
from tigeropen.common.util.string_utils import get_string
4+
5+
6+
class OrderBookResponse(TigerResponse):
7+
def __init__(self):
8+
super(OrderBookResponse, self).__init__()
9+
self.order_book = dict()
10+
self._is_success = None
11+
12+
def parse_response_content(self, response_content):
13+
response = super(OrderBookResponse, self).parse_response_content(response_content)
14+
if 'is_success' in response:
15+
self._is_success = response['is_success']
16+
17+
if self.data and isinstance(self.data, list):
18+
if len(self.data) == 1:
19+
item = self.data[0]
20+
symbol = get_string(item.get('symbol'))
21+
asks = [(v['price'], v['volume'], v['count']) for v in item.get('asks', [])]
22+
bids = [(v['price'], v['volume'], v['count']) for v in item.get('bids', [])]
23+
self.order_book = {'symbol': symbol, 'asks': asks, 'bids': bids}
24+
else:
25+
for item in self.data:
26+
symbol = get_string(item.get('symbol'))
27+
asks = [(v['price'], v['volume'], v['count']) for v in item.get('asks', [])]
28+
bids = [(v['price'], v['volume'], v['count']) for v in item.get('bids', [])]
29+
self.order_book[symbol] = {'symbol': symbol, 'asks': asks, 'bids': bids}
30+
return self.order_book
31+
32+

0 commit comments

Comments
 (0)