-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxxx.py
353 lines (293 loc) · 13.5 KB
/
xxx.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import os
import requests
import pandas as pd
import datetime
import numpy as np
import sys
from utils import *
from bokeh.io import output_file, show
from bokeh.layouts import column, row, gridplot, layout
from bokeh.plotting import figure
from bokeh.models import LinearAxis, Range1d, VBar, DatetimeTickFormatter
from scipy.stats import linregress
# [inst_id, auction]
inst_ids_info = [['sc2402', 1], ['sc2403', 1],
['au2402', 1], ['au2404', 1],
['ag2402', 1], ['ag2404', 1],
['zn2402', 1], ['zn2403', 1],
['al2402', 1], ['al2403', 1],
['cu2402', 1], ['cu2403', 1],
['CF405', 0],
]
sina_usd_symbol_dict = {
'sc': 'OIL',
'au': 'GC',
'ag': 'SI',
'cu': 'CAD',
'zn': 'ZSD',
'al': 'AHD',
}
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
"Accept-Encoding": "gzip, deflate, br",
"Cache-Control": "no-cache",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "gu.sina.cn",
"Proxy-Connection": "keep-alive",
'Sec-Fetch-Site': 'same-site',
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0",
}
def get_usd_price():
se = requests.session()
URL = 'https://gu.sina.cn/ft/api/jsonp.php/var_DATA=/GlobalService.getMink?symbol={}&type=5'
for variety in sina_usd_symbol_dict:
print(variety)
usd_symbol = sina_usd_symbol_dict[variety]
path = os.path.join(data_dir, usd_symbol+'_5min'+'.csv')
url = URL.format(usd_symbol)
r = se.get(url, verify=False, headers=headers)
s = r.text
s=s.replace('},', '')
s=s.replace('"', '')
s=s.replace('}]);', '')
z = s.split('{')[1:]
datas = []
for i in range(len(z)):
data = z[i].split(',')[:7]
data[0] = data[0][2:]
for k in range(1, len(data)):
data[k] = data[k].split(':')[1]
datas.append(data)
df = pd.DataFrame(columns=['time','open','high','low','close','v','p'], data=datas)
df.to_csv(path, encoding='utf-8', index=False)
def get_usd_cny():
se = requests.session()
url = 'https://vip.stock.finance.sina.com.cn/forex/api/jsonp.php/DATA=/NewForexService.getMinKline?symbol=fx_susdcny&scale=5&datalen=1440'
print('USDCNY')
path = os.path.join(data_dir, 'USDCNY_5min'+'.csv')
r = se.get(url, verify=False, headers=headers)
s = r.text
s=s.replace('},', '')
s=s.replace('"', '')
s=s.replace('}]);', '')
z = s.split('{')[1:]
datas = []
for i in range(len(z)):
data = z[i].split(',')[:7]
data[0] = data[0][2:]
for k in range(1, len(data)):
data[k] = data[k].split(':')[1]
datas.append(data)
df = pd.DataFrame(columns=['time','open','high','low','close'], data=datas)
df.to_csv(path, encoding='utf-8', index=False)
def update_au_ag_td_intraday_data():
se = requests.session()
url = 'https://api-ddc-wscn.awtmt.com/market/kline?prod_code=AUTD.SGE&tick_count=360&period_type=60&adjust_price_type=forward&fields=tick_at,open_px,close_px,high_px,low_px,turnover_volume'
r = se.get(url)
data_json = r.json()
df = pd.DataFrame(data_json['data']['candle']['AUTD.SGE']['lines'])
df.columns = ['open', 'close', 'high', 'low', 'volume', 'time']
df = df[['time', 'open', 'high', 'low', 'close', 'volume']]
df['time'] = df['time'].apply(lambda x:datetime.datetime.fromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S'))
print(df)
def xxx_price(display):
now = datetime.datetime.now()
df = pd.DataFrame()
for inst_id_info in inst_ids_info:
time.sleep(0.25)
inst_id = inst_id_info[0]
auction = inst_id_info[1]
if inst_id[1].isdigit():
variety = inst_id[0]
else:
variety = inst_id[0:2]
for exchange in exchange_dict:
if variety in exchange_dict[exchange]:
break
path = os.path.join(future_price_dir, exchange, variety+'.csv')
if not os.path.exists(path):
print("error", path)
exit()
fut_df = pd.read_csv(path, header=[0,1])
fut_t = pd.DatetimeIndex(pd.to_datetime(fut_df['time']['Unnamed: 0_level_1'], format='%Y-%m-%d'))
path = os.path.join(option_price_dir, exchange, inst_id+'.csv')
if not os.path.exists(path):
print("error", path)
exit()
opt_df = pd.read_csv(path, header=[0,1,2])
opt_t = pd.DatetimeIndex(pd.to_datetime(opt_df['time']['time']['time'], format='%Y-%m-%d'))
# usd price
if (auction == 1):
path = os.path.join(data_dir, sina_usd_symbol_dict[variety]+'_5min'+'.csv')
usd_df = pd.read_csv(path)
usd_t = pd.DatetimeIndex(pd.DatetimeIndex(pd.to_datetime(usd_df['time'], format='%Y-%m-%d %H:%M:%S')))
# usdcny
path = os.path.join(data_dir, 'USDCNY_5min'+'.csv')
usdcny_df = pd.read_csv(path)
usdcny_t = pd.DatetimeIndex(pd.to_datetime(usdcny_df['time'], format='%Y-%m-%d %H:%M:%S'))
if (now.strftime('%Y-%m-%d') != opt_t[-1].strftime('%Y-%m-%d')):
print('wrong time 2,', variety)
exit()
if (now.strftime('%Y-%m-%d') != fut_t[-1].strftime('%Y-%m-%d')):
print('wrong time 3,', variety)
exit()
price_chg = 0
if (auction == 1):
if (now.strftime('%Y-%m-%d') != usd_t[-1].strftime('%Y-%m-%d')):
print('wrong time 1,', variety)
exit()
if (now.strftime('%Y-%m-%d') != usdcny_t[-1].strftime('%Y-%m-%d')):
print('wrong time 4,', variety)
exit()
if now.hour >= 15:
usd_start_time = usd_t[-1].strftime('%Y-%m-%d') + ' 15:00:00'
else:
print('wrong time 5,', variety)
exit()
usd_start_time_dt = pd.to_datetime(usd_start_time, format='%Y-%m-%d %H:%M:%S')
w = np.where(usd_t == usd_start_time_dt)[0]
if (len(w) == 0):
usd_start_time = usd_t[-1].strftime('%Y-%m-%d') + ' 14:55:00'
usd_start_time_dt = pd.to_datetime(usd_start_time, format='%Y-%m-%d %H:%M:%S')
w = np.where(usd_t == usd_start_time_dt)[0]
if (len(w) == 0):
print('wrong time 6,', variety)
exit()
w = w[0]
start_price = (usd_df.loc[w, 'open'] + usd_df.loc[w, 'high'] + \
usd_df.loc[w, 'low'] + usd_df.loc[w, 'close']) / 4
end_price = (usd_df.loc[len(usd_df)-1, 'open'] + usd_df.loc[len(usd_df)-1, 'high'] + \
usd_df.loc[len(usd_df)-1, 'low'] + usd_df.loc[len(usd_df)-1, 'close']) / 4
w = np.where(usdcny_t == usd_start_time_dt)[0]
if (len(w) == 0):
print('wrong time 7,', variety)
exit()
w = w[0]
start_fx = (usdcny_df.loc[w, 'open'] + usdcny_df.loc[w, 'high'] + \
usdcny_df.loc[w, 'low'] + usdcny_df.loc[w, 'close']) / 4
end_fx = (usdcny_df.loc[len(usdcny_df)-1, 'open'] + usdcny_df.loc[len(usdcny_df)-1, 'high'] + \
usdcny_df.loc[len(usdcny_df)-1, 'low'] + 3*usdcny_df.loc[len(usdcny_df)-1, 'close']) / 6
price_chg = (end_price*end_fx - start_price*start_fx) / (start_price*start_fx)
cny_price_start = None
temp_fut_df = fut_df.loc[len(fut_df)-1, :]
cs = ['c1','c2','c3','c4','c5','c6','c7','c8','c9']
for c in cs:
if (temp_fut_df[c]['inst_id'] == inst_id):
cny_price_start = temp_fut_df[c]['close']
break
if cny_price_start is None:
print('cny_price_start is None')
exit()
if (auction == 1):
cny_price_end = cny_price_start * (1 + price_chg)
else:
cny_price_end = cny_price_start
print(inst_id, cny_price_end, str(round(price_chg*100,2))+'%')
col = opt_df.columns.tolist()
res = [(col[i][1]) for i in range(len(col)) if col[i][0] == 'P']
strikes_str = []
for i in res:
if i not in strikes_str:
strikes_str.append(i)
strike = []
call_price = []
call_delta = []
put_price = []
put_delta = []
for strike_str in strikes_str:
strike.append(float(strike_str))
call_price.append(opt_df.loc[len(opt_df)-1, pd.IndexSlice['C', strike_str, 'close']])
call_delta.append(opt_df.loc[len(opt_df)-1, pd.IndexSlice['C', strike_str, 'delta_c']])
put_price.append(opt_df.loc[len(opt_df)-1, pd.IndexSlice['P', strike_str, 'close']])
put_delta.append(opt_df.loc[len(opt_df)-1, pd.IndexSlice['P', strike_str, 'delta_c']])
strike = np.array(strike, dtype=float)
call_price = np.array(call_price, dtype=float)
call_delta = np.array(call_delta, dtype=float)
put_price = np.array(put_price, dtype=float)
put_delta = np.array(put_delta, dtype=float)
col = ['inst_id', 'price', 'delta']
data = [[inst_id, cny_price_end, 0]]
for i in range(len(strike)):
if (strike[i] <= cny_price_end*1.15) and (strike[i] >= cny_price_end*0.85):
# call
if exchange == 'shfe' or exchange == 'czce':
opt_inst_id = inst_id + 'C' + strikes_str[i]
else:
opt_inst_id = inst_id + '-C-' + strikes_str[i]
price = call_price[i] + call_delta[i]*(cny_price_end - cny_price_start)
data.append([opt_inst_id, price, call_delta[i]])
# put
if exchange == 'shfe' or exchange == 'czce':
opt_inst_id = inst_id + 'P' + strikes_str[i]
else:
opt_inst_id = inst_id + '-P-' + strikes_str[i]
price = put_price[i] + put_delta[i]*(cny_price_end - cny_price_start)
data.append([opt_inst_id, price, put_delta[i]])
temp_df = pd.DataFrame(columns=col, data=data)
df = pd.concat([df, temp_df], axis=0)
if (display):
# plot
opt_inst_id = np.array(temp_df.loc[1:, 'inst_id'], dtype=str)
price = np.array(temp_df.loc[1:, 'price'], dtype=float)
delta = np.array(temp_df.loc[1:, 'delta'], dtype=float)
w = np.where(np.logical_not(np.isnan(price)))[0]
opt_inst_id = opt_inst_id[w]
price = price[w]
delta = delta[w]
L = len(temp_df.loc[0, 'inst_id'])
c_strike = []
c_price = []
c_delta = []
p_strike = []
p_price = []
p_delta = []
for i in range(len(opt_inst_id)):
if 'C' in opt_inst_id[i][L:]:
if ('-' in opt_inst_id[i][L:]):
c_strike.append(float(opt_inst_id[i][L+3:]))
else:
c_strike.append(float(opt_inst_id[i][L+1:]))
c_price.append(price[i])
c_delta.append(delta[i])
if 'P' in opt_inst_id[i][L:]:
if ('-' in opt_inst_id[i][L:]):
p_strike.append(float(opt_inst_id[i][L+3:]))
else:
p_strike.append(float(opt_inst_id[i][L+1:]))
p_price.append(price[i])
p_delta.append(delta[i])
c_strike = np.array(c_strike, dtype=float)
c_price = np.array(c_price, dtype=float)
c_delta = np.array(c_delta, dtype=float)
sort = np.argsort(c_strike)
c_strike = c_strike[sort]
c_price = c_price[sort]
c_delta = c_delta[sort]
p_strike = np.array(p_strike, dtype=float)
p_price = np.array(p_price, dtype=float)
p_delta = np.array(p_delta, dtype=float)
sort = np.argsort(p_strike)
p_strike = p_strike[sort]
p_price = p_price[sort]
p_delta = p_delta[sort]
fig1 = figure(frame_width=1400, frame_height=300, tools=TOOLS, title=temp_df.loc[0, 'inst_id'])
fig1.line(c_strike, c_price, line_width=2, line_color='red', legend_label='CALL PRICE')
fig1.circle(x=c_strike, y=c_price, color='red', legend_label='CALL PRICE')
fig1.line(p_strike, p_price, line_width=2, line_color='darkgreen', legend_label='PUT PRICE')
fig1.circle(x=p_strike, y=p_price, color='darkgreen', legend_label='PUT PRICE')
fig2 = figure(frame_width=1400, frame_height=300, tools=TOOLS, x_range=fig1.x_range)
fig2.line(c_strike, c_delta, line_width=2, line_color='red', legend_label='CALL DELTA')
fig2.circle(x=c_strike, y=c_delta, color='red', legend_label='CALL DELTA')
fig2.line(p_strike, p_delta, line_width=2, line_color='darkgreen', legend_label='PUT DELTA')
fig2.circle(x=p_strike, y=p_delta, color='darkgreen', legend_label='PUT DELTA')
show(column(fig1, fig2))
path = os.path.join('D:\CTP', 'opt_price'+'.csv')
df.to_csv(path, encoding='utf-8', index=False)
if __name__=="__main__":
get_usd_price()
get_usd_cny()
# TODO: 早盘盘前竞价
xxx_price(len(sys.argv) == 1)
# update_au_ag_td_intraday_data()
pass