-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_tiger.py
161 lines (120 loc) · 4.05 KB
/
utils_tiger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from typing import Type
import time
import tigeropen.common.consts as tiger_consts
from tigeropen.tiger_open_config import TigerOpenClientConfig
from tigeropen.quote.quote_client import QuoteClient
from tigeropen.push.push_client import PushClient
from tigeropen.common.util.signature_utils import read_private_key
import utils_common
g_config = None
g_quote_client = None
def call_api(func, time_limit, retry=0):
itry = 0
while True:
try:
result = func()
time.sleep(time_limit)
break
except Exception as e:
if itry < retry:
itry += 1
else:
raise e
pass
pass
return result
def get_client_config(config: dict) -> Type[TigerOpenClientConfig]:
client_config = TigerOpenClientConfig()
client_config.private_key = read_private_key(config['tiger_private_key'])
client_config.tiger_id = config['tiger_id']
client_config.account = config['tiger_account']
client_config.standard_account = config.get('tiger_standard_account')
client_config.paper_account = config['tiger_paper_account']
client_config.language = tiger_consts.Language.zh_CN
return client_config
def get_quote_client(config: dict = None) -> Type[QuoteClient]:
global g_config
if config is None:
config = g_config
pass
return QuoteClient(get_client_config(config))
def set_config(config: dict):
global g_config, g_quote_client
g_config = config
g_quote_client = get_quote_client(config)
def get_push_client(config: dict = None) -> Type[PushClient]:
global g_config
if config is None:
config = g_config
pass
client_config = get_client_config(config)
protocol, host, port = client_config.socket_host_port
push_client = PushClient(host, port, use_ssl=(protocol == 'ssl'))
push_client.connect(client_config.tiger_id, client_config.private_key)
return push_client
def get_future_bars_minute(
identifiers: list,
begin_time: int = -1,
end_time: int = -1,
limit: int = 1000):
def func(): return g_quote_client.get_future_bars(
identifiers,
period=tiger_consts.BarPeriod.ONE_MINUTE,
begin_time=begin_time,
end_time=end_time,
limit=limit)
return call_api(func, 3, 0)
def get_bars_minute_dt(
quote_client: QuoteClient,
symbols: list,
dt: str):
ms = utils_common.dt2ms(dt)
start = ms + 12 * 60 * 60 * 1000
end = ms + 36 * 60 * 60 * 1000
return quote_client.get_bars(
symbols=symbols,
period=tiger_consts.BarPeriod.ONE_MINUTE,
begin_time=start,
end_time=end,
right=tiger_consts.QuoteRight.BR,
limit=600)
def get_timeline(
symbols: list,
begin_time: int = -1):
def func(): return g_quote_client.get_timeline(
symbols, include_hour_trading=True,
begin_time=begin_time)
return call_api(func, 0.5, 0)
def get_bars_minute_month(
quote_client: QuoteClient,
symbols: list,
dt: str):
ms = utils_common.dt2ms(dt)
start = ms + (12-31*24) * 60 * 60 * 1000
end = ms + 36 * 60 * 60 * 1000
print(start)
print(end)
return quote_client.get_bars(
symbols=symbols,
period=tiger_consts.BarPeriod.ONE_MINUTE,
begin_time=start,
end_time=end,
right=tiger_consts.QuoteRight.BR,
limit=60000)
def get_trade_ticks(
symbols: list,
limit: int,
begin_index: int = None,
end_index: int = None):
def func(): return g_quote_client.get_trade_ticks(
symbols, begin_index=begin_index, end_index=end_index, limit=limit)
return call_api(func, 0.5, 0)
def get_future_trade_ticks(
symbol: str,
begin_index: int,
end_index: int,
limit: int):
def func(): return g_quote_client.get_future_trade_ticks(
symbol, begin_index=begin_index, end_index=end_index,
limit=limit)
return call_api(func, 0.5, 0)