Skip to content

Commit c46d910

Browse files
committed
Merge branch 'feature_grab_quote' into 'dev'
Feature grab quote See merge request server/openapi/openapi-python-sdk!65
2 parents ed4f000 + 6b4136a commit c46d910

File tree

8 files changed

+91
-24
lines changed

8 files changed

+91
-24
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
@@ -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+
GRAB_QUOTE_PERMISSION = "grab_quote_permission" # 抢占行情
4546

4647
# 期权行情
4748
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: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,61 @@
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 MARKET_STATE, ALL_SYMBOLS, ALL_SYMBOL_NAMES, BRIEF, \
17+
TIMELINE, KLINE, TRADE_TICK, OPTION_EXPIRATION, OPTION_CHAIN, FUTURE_EXCHANGE, OPTION_BRIEF, \
18+
OPTION_KLINE, OPTION_TRADE_TICK, FUTURE_KLINE, FUTURE_TICK, FUTURE_CONTRACT_BY_EXCHANGE_CODE, \
19+
FUTURE_TRADING_DATE, QUOTE_SHORTABLE_STOCKS, FUTURE_REAL_TIME_QUOTE, \
20+
FUTURE_CURRENT_CONTRACT, QUOTE_REAL_TIME, QUOTE_STOCK_TRADE, FINANCIAL_DAILY, FINANCIAL_REPORT, CORPORATE_ACTION, \
21+
INDUSTRY_LIST, INDUSTRY_STOCKS, STOCK_INDUSTRY, STOCK_DETAIL, GRAB_QUOTE_PERMISSION
1322
from tigeropen.common.exceptions import ApiException
23+
from tigeropen.common.util.common_utils import eastern
24+
from tigeropen.common.util.contract_utils import extract_option_info
1425
from tigeropen.fundamental.request.model import FinancialDailyParams, FinancialReportParams, CorporateActionParams, \
1526
IndustryParams
1627
from tigeropen.fundamental.response.corporate_dividend_response import CorporateDividendResponse
1728
from tigeropen.fundamental.response.corporate_earnings_calendar_response import EarningsCalendarResponse
1829
from tigeropen.fundamental.response.corporate_split_response import CorporateSplitResponse
19-
from tigeropen.fundamental.response.financial_report_response import FinancialReportResponse
2030
from tigeropen.fundamental.response.financial_daily_response import FinancialDailyResponse
31+
from tigeropen.fundamental.response.financial_report_response import FinancialReportResponse
2132
from tigeropen.fundamental.response.industry_response import IndustryListResponse, IndustryStocksResponse, \
2233
StockIndustryResponse
34+
from tigeropen.quote.request import OpenApiRequest
35+
from tigeropen.quote.request.model import MarketParams, MultipleQuoteParams, MultipleContractParams, \
36+
FutureQuoteParams, FutureExchangeParams, FutureTypeParams, FutureTradingTimeParams, SingleContractParams, \
37+
SingleOptionQuoteParams
2338
from tigeropen.quote.response.future_briefs_response import FutureBriefsResponse
24-
from tigeropen.quote.response.future_exchange_response import FutureExchangeResponse
2539
from tigeropen.quote.response.future_contract_response import FutureContractResponse
40+
from tigeropen.quote.response.future_exchange_response import FutureExchangeResponse
2641
from tigeropen.quote.response.future_quote_bar_response import FutureQuoteBarResponse
2742
from tigeropen.quote.response.future_quote_ticks_response import FutureTradeTickResponse
2843
from tigeropen.quote.response.future_trading_times_response import FutureTradingTimesResponse
44+
from tigeropen.quote.response.market_status_response import MarketStatusResponse
2945
from tigeropen.quote.response.option_briefs_response import OptionBriefsResponse
3046
from tigeropen.quote.response.option_chains_response import OptionChainsResponse
3147
from tigeropen.quote.response.option_expirations_response import OptionExpirationsResponse
3248
from tigeropen.quote.response.option_quote_bar_response import OptionQuoteBarResponse
3349
from tigeropen.quote.response.option_quote_ticks_response import OptionTradeTickResponse
3450
from tigeropen.quote.response.quote_bar_response import QuoteBarResponse
35-
from tigeropen.quote.response.quote_timeline_response import QuoteTimelineResponse
3651
from tigeropen.quote.response.quote_brief_response import QuoteBriefResponse
52+
from tigeropen.quote.response.quote_grab_permission_response import QuoteGrabPermissionResponse
53+
from tigeropen.quote.response.quote_ticks_response import TradeTickResponse
54+
from tigeropen.quote.response.quote_timeline_response import QuoteTimelineResponse
3755
from tigeropen.quote.response.stock_briefs_response import StockBriefsResponse
3856
from tigeropen.quote.response.stock_details_response import StockDetailsResponse
3957
from tigeropen.quote.response.stock_short_interest_response import ShortInterestResponse
4058
from tigeropen.quote.response.stock_trade_meta_response import TradeMetaResponse
4159
from tigeropen.quote.response.symbol_names_response import SymbolNamesResponse
4260
from tigeropen.quote.response.symbols_response import SymbolsResponse
4361
from tigeropen.tiger_open_client import TigerOpenClient
44-
from tigeropen.quote.request.model import MarketParams, MultipleQuoteParams, MultipleContractParams, \
45-
FutureQuoteParams, FutureExchangeParams, FutureTypeParams, FutureTradingTimeParams, SingleContractParams, \
46-
SingleOptionQuoteParams
47-
from tigeropen.quote.request import OpenApiRequest
48-
from tigeropen.quote.response.quote_ticks_response import TradeTickResponse
49-
from tigeropen.quote.response.market_status_response import MarketStatusResponse
50-
from tigeropen.common.consts.service_types import MARKET_STATE, ALL_SYMBOLS, ALL_SYMBOL_NAMES, BRIEF, \
51-
TIMELINE, KLINE, TRADE_TICK, OPTION_EXPIRATION, OPTION_CHAIN, FUTURE_EXCHANGE, OPTION_BRIEF, \
52-
OPTION_KLINE, OPTION_TRADE_TICK, FUTURE_KLINE, FUTURE_TICK, FUTURE_CONTRACT_BY_EXCHANGE_CODE, \
53-
FUTURE_TRADING_DATE, QUOTE_SHORTABLE_STOCKS, FUTURE_REAL_TIME_QUOTE, \
54-
FUTURE_CURRENT_CONTRACT, QUOTE_REAL_TIME, QUOTE_STOCK_TRADE, FINANCIAL_DAILY, FINANCIAL_REPORT, CORPORATE_ACTION, \
55-
INDUSTRY_LIST, INDUSTRY_STOCKS, STOCK_INDUSTRY, STOCK_DETAIL
56-
from tigeropen.common.consts import Market, Language, QuoteRight, BarPeriod
57-
from tigeropen.common.util.contract_utils import extract_option_info
58-
from tigeropen.common.util.common_utils import eastern
59-
import logging
6062

6163

6264
class QuoteClient(TigerOpenClient):
@@ -74,7 +76,7 @@ def __fetch_data(self, request):
7476
response = super(QuoteClient, self).execute(request)
7577
return response
7678
except Exception as e:
77-
if THREAD_LOCAL.logger:
79+
if hasattr(THREAD_LOCAL, 'logger') and THREAD_LOCAL.logger:
7880
THREAD_LOCAL.logger.error(e, exc_info=True)
7981
raise e
8082

@@ -1079,3 +1081,19 @@ def get_stock_industry(self, symbol, market=Market.US):
10791081
return response.stock_industry
10801082
else:
10811083
raise ApiException(response.code, response.message)
1084+
1085+
def grab_quote_permission(self):
1086+
"""
1087+
抢占行情权限
1088+
:return: 是否抢占成功, bool 类型
1089+
"""
1090+
request = OpenApiRequest(GRAB_QUOTE_PERMISSION)
1091+
response_content = self.__fetch_data(request)
1092+
if response_content:
1093+
response = QuoteGrabPermissionResponse()
1094+
response.parse_response_content(response_content)
1095+
if response.is_success():
1096+
return response.is_master
1097+
else:
1098+
raise ApiException(response.code, response.message)
1099+
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)