Skip to content

Commit 76478ec

Browse files
committed
Merge branch 'dev' into level2
2 parents 161a8ac + c46d910 commit 76478ec

File tree

8 files changed

+93
-25
lines changed

8 files changed

+93
-25
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ pyasn1==0.4.2
88
rsa==4.0
99
stomp.py==4.1.22
1010
enum34==1.1.6
11+
getmac==0.8.2

setup.py

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

1313
setup(
1414
name='tigeropen',
15-
version='1.3.3',
15+
version='1.4.0',
1616
description='TigerBrokers Open API',
1717
packages=find_packages(exclude=[]),
1818
author='TigerBrokers',

tigeropen/common/consts/params.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
P_TIMESTAMP = "timestamp"
1414
P_VERSION = "version"
1515
P_NOTIFY_URL = "notify_url"
16+
P_DEVICE_ID = "device_id"
1617

17-
COMMON_PARAM_KEYS = set([P_TIGER_ID, P_METHOD, P_CHARSET, P_SIGN_TYPE, P_SIGN, P_TIMESTAMP, P_VERSION, P_NOTIFY_URL])
18+
COMMON_PARAM_KEYS = {P_TIGER_ID, P_METHOD, P_CHARSET, P_SIGN_TYPE, P_SIGN, P_TIMESTAMP, P_VERSION, P_NOTIFY_URL,
19+
P_DEVICE_ID}
1820
P_BIZ_CONTENT = "biz_content"

tigeropen/common/consts/service_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
QUOTE_SHORTABLE_STOCKS = "quote_shortable_stocks"
4444
QUOTE_STOCK_TRADE = "quote_stock_trade"
4545
ORDER_BOOK = "ask_bid" # level2 深度行情
46+
GRAB_QUOTE_PERMISSION = "grab_quote_permission" # 抢占行情
4647

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

tigeropen/examples/quote_client_demo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424

2525
def get_quote():
26+
# 抢占行情权限
27+
is_grab_quote_success = openapi_client.grab_quote_permission()
28+
print(is_grab_quote_success)
29+
2630
market_status_list = openapi_client.get_market_status(Market.US)
2731
print(market_status_list)
2832
briefs = openapi_client.get_briefs(symbols=['AAPL', '00700', '600519'], include_ask_bid=True, right=QuoteRight.BR)

tigeropen/quote/quote_client.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,63 @@
44
55
@author: gaoan
66
"""
7-
import re
87
import enum
8+
import logging
9+
import re
10+
911
import delorean
1012
import six
1113

14+
from tigeropen.common.consts import Market, Language, QuoteRight, BarPeriod
1215
from tigeropen.common.consts import THREAD_LOCAL, SecurityType, CorporateActionType, IndustryLevel
16+
from tigeropen.common.consts.service_types import GRAB_QUOTE_PERMISSION
17+
from tigeropen.common.consts.service_types import MARKET_STATE, ALL_SYMBOLS, ALL_SYMBOL_NAMES, BRIEF, \
18+
TIMELINE, KLINE, TRADE_TICK, OPTION_EXPIRATION, OPTION_CHAIN, FUTURE_EXCHANGE, OPTION_BRIEF, \
19+
OPTION_KLINE, OPTION_TRADE_TICK, FUTURE_KLINE, FUTURE_TICK, FUTURE_CONTRACT_BY_EXCHANGE_CODE, \
20+
FUTURE_TRADING_DATE, QUOTE_SHORTABLE_STOCKS, FUTURE_REAL_TIME_QUOTE, \
21+
FUTURE_CURRENT_CONTRACT, QUOTE_REAL_TIME, QUOTE_STOCK_TRADE, FINANCIAL_DAILY, FINANCIAL_REPORT, CORPORATE_ACTION, \
22+
ORDER_BOOK, INDUSTRY_LIST, INDUSTRY_STOCKS, STOCK_INDUSTRY, STOCK_DETAIL
1323
from tigeropen.common.exceptions import ApiException
24+
from tigeropen.common.util.common_utils import eastern
25+
from tigeropen.common.util.contract_utils import extract_option_info
1426
from tigeropen.fundamental.request.model import FinancialDailyParams, FinancialReportParams, CorporateActionParams, \
1527
IndustryParams
1628
from tigeropen.fundamental.response.corporate_dividend_response import CorporateDividendResponse
1729
from tigeropen.fundamental.response.corporate_earnings_calendar_response import EarningsCalendarResponse
1830
from tigeropen.fundamental.response.corporate_split_response import CorporateSplitResponse
19-
from tigeropen.fundamental.response.financial_report_response import FinancialReportResponse
2031
from tigeropen.fundamental.response.financial_daily_response import FinancialDailyResponse
32+
from tigeropen.fundamental.response.financial_report_response import FinancialReportResponse
2133
from tigeropen.fundamental.response.industry_response import IndustryListResponse, IndustryStocksResponse, \
2234
StockIndustryResponse
35+
from tigeropen.quote.request import OpenApiRequest
36+
from tigeropen.quote.request.model import MarketParams, MultipleQuoteParams, MultipleContractParams, \
37+
FutureQuoteParams, FutureExchangeParams, FutureTypeParams, FutureTradingTimeParams, SingleContractParams, \
38+
SingleOptionQuoteParams, OrderBookParams
2339
from tigeropen.quote.response.future_briefs_response import FutureBriefsResponse
24-
from tigeropen.quote.response.future_exchange_response import FutureExchangeResponse
2540
from tigeropen.quote.response.future_contract_response import FutureContractResponse
41+
from tigeropen.quote.response.future_exchange_response import FutureExchangeResponse
2642
from tigeropen.quote.response.future_quote_bar_response import FutureQuoteBarResponse
2743
from tigeropen.quote.response.future_quote_ticks_response import FutureTradeTickResponse
2844
from tigeropen.quote.response.future_trading_times_response import FutureTradingTimesResponse
45+
from tigeropen.quote.response.market_status_response import MarketStatusResponse
2946
from tigeropen.quote.response.option_briefs_response import OptionBriefsResponse
3047
from tigeropen.quote.response.option_chains_response import OptionChainsResponse
3148
from tigeropen.quote.response.option_expirations_response import OptionExpirationsResponse
3249
from tigeropen.quote.response.option_quote_bar_response import OptionQuoteBarResponse
3350
from tigeropen.quote.response.option_quote_ticks_response import OptionTradeTickResponse
3451
from tigeropen.quote.response.quote_bar_response import QuoteBarResponse
35-
from tigeropen.quote.response.quote_timeline_response import QuoteTimelineResponse
3652
from tigeropen.quote.response.quote_brief_response import QuoteBriefResponse
53+
from tigeropen.quote.response.quote_grab_permission_response import QuoteGrabPermissionResponse
54+
from tigeropen.quote.response.quote_order_book_response import OrderBookResponse
55+
from tigeropen.quote.response.quote_ticks_response import TradeTickResponse
56+
from tigeropen.quote.response.quote_timeline_response import QuoteTimelineResponse
3757
from tigeropen.quote.response.stock_briefs_response import StockBriefsResponse
3858
from tigeropen.quote.response.stock_details_response import StockDetailsResponse
3959
from tigeropen.quote.response.stock_short_interest_response import ShortInterestResponse
4060
from tigeropen.quote.response.stock_trade_meta_response import TradeMetaResponse
4161
from tigeropen.quote.response.symbol_names_response import SymbolNamesResponse
4262
from tigeropen.quote.response.symbols_response import SymbolsResponse
43-
from tigeropen.quote.response.quote_order_book_response import OrderBookResponse
4463
from tigeropen.tiger_open_client import TigerOpenClient
45-
from tigeropen.quote.request.model import MarketParams, MultipleQuoteParams, MultipleContractParams, \
46-
FutureQuoteParams, FutureExchangeParams, FutureTypeParams, FutureTradingTimeParams, SingleContractParams, \
47-
SingleOptionQuoteParams, OrderBookParams
48-
from tigeropen.quote.request import OpenApiRequest
49-
from tigeropen.quote.response.quote_ticks_response import TradeTickResponse
50-
from tigeropen.quote.response.market_status_response import MarketStatusResponse
51-
from tigeropen.common.consts.service_types import MARKET_STATE, ALL_SYMBOLS, ALL_SYMBOL_NAMES, BRIEF, \
52-
TIMELINE, KLINE, TRADE_TICK, OPTION_EXPIRATION, OPTION_CHAIN, FUTURE_EXCHANGE, OPTION_BRIEF, \
53-
OPTION_KLINE, OPTION_TRADE_TICK, FUTURE_KLINE, FUTURE_TICK, FUTURE_CONTRACT_BY_EXCHANGE_CODE, \
54-
FUTURE_TRADING_DATE, QUOTE_SHORTABLE_STOCKS, FUTURE_REAL_TIME_QUOTE, \
55-
FUTURE_CURRENT_CONTRACT, QUOTE_REAL_TIME, QUOTE_STOCK_TRADE, FINANCIAL_DAILY, FINANCIAL_REPORT, CORPORATE_ACTION, \
56-
ORDER_BOOK, INDUSTRY_LIST, INDUSTRY_STOCKS, STOCK_INDUSTRY, STOCK_DETAIL
57-
from tigeropen.common.consts import Market, Language, QuoteRight, BarPeriod
58-
from tigeropen.common.util.contract_utils import extract_option_info
59-
from tigeropen.common.util.common_utils import eastern
60-
import logging
6164

6265

6366
class QuoteClient(TigerOpenClient):
@@ -75,7 +78,7 @@ def __fetch_data(self, request):
7578
response = super(QuoteClient, self).execute(request)
7679
return response
7780
except Exception as e:
78-
if THREAD_LOCAL.logger:
81+
if hasattr(THREAD_LOCAL, 'logger') and THREAD_LOCAL.logger:
7982
THREAD_LOCAL.logger.error(e, exc_info=True)
8083
raise e
8184

@@ -1130,3 +1133,19 @@ def get_stock_industry(self, symbol, market=Market.US):
11301133
return response.stock_industry
11311134
else:
11321135
raise ApiException(response.code, response.message)
1136+
1137+
def grab_quote_permission(self):
1138+
"""
1139+
抢占行情权限
1140+
:return: 是否抢占成功, bool 类型
1141+
"""
1142+
request = OpenApiRequest(GRAB_QUOTE_PERMISSION)
1143+
response_content = self.__fetch_data(request)
1144+
if response_content:
1145+
response = QuoteGrabPermissionResponse()
1146+
response.parse_response_content(response_content)
1147+
if response.is_success():
1148+
return response.is_master
1149+
else:
1150+
raise ApiException(response.code, response.message)
1151+
return False
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# @Date : 2021-04-16
4+
# @Author : sukai
5+
import json
6+
from tigeropen.common.response import TigerResponse
7+
8+
9+
class QuoteGrabPermissionResponse(TigerResponse):
10+
def __init__(self):
11+
super(QuoteGrabPermissionResponse, self).__init__()
12+
self.is_master = False
13+
self._is_success = None
14+
15+
def parse_response_content(self, response_content):
16+
response = super(QuoteGrabPermissionResponse, self).parse_response_content(response_content)
17+
if 'is_success' in response:
18+
self._is_success = response['is_success']
19+
20+
if self.data:
21+
data_json = json.loads(self.data)
22+
self.is_master = data_json.get('is_master')

tigeropen/tiger_open_client.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from __future__ import unicode_literals
88
import datetime
99
import uuid
10-
import sys
1110

1211
from tigeropen.common.consts import *
1312
from tigeropen.common.consts.params import *
@@ -17,9 +16,16 @@
1716
from tigeropen.common.exceptions import *
1817

1918
if not PYTHON_VERSION_3:
19+
import sys
2020
reload(sys)
2121
sys.setdefaultencoding('utf-8')
2222

23+
try:
24+
from getmac import get_mac_address
25+
except ImportError:
26+
def get_mac_address():
27+
return ':'.join(("%012x" % uuid.getnode())[i:i + 2] for i in range(0, 12, 2))
28+
2329

2430
class TigerOpenClient(object):
2531
"""
@@ -36,6 +42,7 @@ def __init__(self, client_config, logger=None):
3642
"Connection": "Keep-Alive",
3743
"User-Agent": 'openapi-python-sdk-' + OPEN_API_SDK_VERSION
3844
}
45+
self.__device_id = self.__get_device_id()
3946

4047
"""
4148
内部方法,从params中抽取公共参数
@@ -49,10 +56,22 @@ def __get_common_params(self, params):
4956
common_params[P_CHARSET] = self.__config.charset
5057
common_params[P_VERSION] = params[P_VERSION]
5158
common_params[P_SIGN_TYPE] = self.__config.sign_type
59+
common_params[P_DEVICE_ID] = self.__device_id
5260
if has_value(params, P_NOTIFY_URL):
5361
common_params[P_NOTIFY_URL] = params[P_NOTIFY_URL]
5462
return common_params
5563

64+
@staticmethod
65+
def __get_device_id():
66+
"""
67+
获取mac地址作为device_id
68+
:return:
69+
"""
70+
try:
71+
return get_mac_address()
72+
except:
73+
return None
74+
5675
"""
5776
内部方法,从params中移除公共参数
5877
"""

0 commit comments

Comments
 (0)