-
Notifications
You must be signed in to change notification settings - Fork 0
/
delta_math.py
executable file
·278 lines (195 loc) · 7.4 KB
/
delta_math.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
import argparse
import functools
import pandas as pd
import numpy as np
import datetime
import json
import sys
import os
import math
last_mid_price = 0
def init():
parser = argparse.ArgumentParser(
prog = __file__,
description = 'delta math')
parser.add_argument('-f','--filename')
parser.add_argument('-o','--output',nargs='?', const=1, type=str, default=None)
args = parser.parse_args()
global dfile
dfile = args.filename
global dout
dout = args.output
global net_delta_hist
net_delta_hist = []
print('file: ',dfile)
print('output: ',dout)
def parseFile():
header = ['tid','price','qty','quoteQty','time','side','bmatch']
global df
df = pd.read_csv (dfile,names=header)
print(df)
def cleanData(minutes):
seconds = minutes * 60
df['time'] = pd.to_datetime(df['time'], unit='ms')
df['side'] = np.where(df['side']==True,1, -1)
print(df)
current_ind = 0
total_count=0
count = 0
l = []
if os.path.exists('output.csv'):
os.remove('output.csv')
with open('output.csv', 'a') as f:
if dout != None:
header = "start_time,end_time,open_price,closing_price,total_volume,bar_direction,cum_delta,bar_duration,volume_sec,high_wick_bid_ask,low_wick_bid_ask,bid_imb,ask_imb,price_sd,price_mean,net_delta_t3,net_delta_t2,net_delta_t1,log_return_p1\n"
f.write(header)
t1 = df['time'][current_ind]
l.append(json.loads(df.loc[current_ind].to_json()))
for j in range(1,len(df)):
t2 = df['time'][j]
diff = (t2 - t1).total_seconds()
count+=1
#print(t1,t2,diff,current_ind,count)
l.append(json.loads(df.loc[j].to_json()))
if diff > seconds:
current_ind = j
count=0
t1 = df['time'][current_ind]
o = getMetrics(l)
if dout != None and o != "":
f.write(o)
l = []
f.close()
def unix2read(ts):
#.strftime('%Y-%m-%d %H:%M:%S.%f')
return datetime.datetime.fromtimestamp(int(ts) / 1000,tz=datetime.timezone.utc)
def addBuySell(i,p,v,s):
if p in i:
if s == -1:
i[p]['sell'] += float(v)
elif s == 1:
i[p]['buy'] += float(v)
else:
if s == -1:
i[p] = {'sell':float(v),'buy':0}
elif s == 1:
i[p] = {'sell':0,'buy':float(v)}
def getMetrics(l):
total_volume = 0
net_buy_volume = 0
net_sell_volume = 0
imbalance = {}
market_depth = []
first_value = l[0]
last_value = l[len(l)-1]
start_time = unix2read(first_value['time'])
end_time = unix2read(last_value['time'])
time_diff = (end_time - start_time).total_seconds()
open_price = first_value['price']
closing_price = last_value['price']
mean_price = getMean(l)
sd = getStd(l)
max_price = max(l, key=lambda feature: feature['price'])['price']
min_price = min(l, key=lambda feature: feature['price'])['price']
log_return = 0
global last_mid_price
if last_mid_price == 0:
last_mid_price = (max_price + min_price)/2
else:
mid_price = (max_price + min_price)/2
log_return = abs(np.log(mid_price) - np.log(last_mid_price)) * 100
log_return = "{:.5f}".format(log_return)
last_mid_price = mid_price
bar_direction = 1 if (open_price < closing_price) else -1
for value in l:
price = str(value['price'])
total_volume += float(value['qty'])
if value['side'] == 1:
net_buy_volume += float(value['qty'])
addBuySell(imbalance,price,value['qty'],1)
elif value['side'] == -1:
net_sell_volume += float(value['qty'])
addBuySell(imbalance,price,value['qty'],-1)
volume_sec = "{:.2f}".format(total_volume / time_diff)
net_delta = "{:.2f}".format(net_sell_volume - net_buy_volume)
net_delta_hist.append(net_delta)
total_volume = "{:.2f}".format(total_volume)
mean_price = "{:.2f}".format(mean_price)
sd = "{:.2f}".format(sd)
for k in sorted(imbalance.keys(),reverse=False):
b = imbalance[k]['buy']
s = imbalance[k]['sell']
market_depth.append({
'p':k
,'s':s
,'b':b
});
low_wick_bid_ask=market_depth[0]
high_wick_bid_ask=market_depth[len(market_depth)-1]
buy_imb = 0
sell_imb = 0
threshold = 100 # size
i = 0
p1 = market_depth[i]['s']
for j in range(1,len(market_depth)):
p2 = market_depth[j]['b']
if p1 > p2:
if p2 != 0:
d=(p1 / p2)
if d > threshold:
sell_imb+=1
#print(market_depth[i]['p'],'sell',"{:.2f}".format(d),"{:.2f}".format(p1),"{:.2f}".format(p2))
else:
if p1 != 0:
d=(p2 / p1)
if d > threshold:
buy_imb+=1
#print(market_depth[j]['p'],'buy',"{:.2f}".format(d),"{:.2f}".format(p1),"{:.2f}".format(p2))
i+=1
p1 = market_depth[i]['s']
print(total_volume,net_delta,bar_direction,volume_sec,min_price,max_price,time_diff)
print('HighWick',high_wick_bid_ask)
print('LowWick',low_wick_bid_ask)
high_wick = str(high_wick_bid_ask['s']) + 'x'+ str(high_wick_bid_ask['b'])
low_wick = str(low_wick_bid_ask['s']) + 'x'+ str(low_wick_bid_ask['b'])
print('BuyImb',buy_imb,'SellImb',sell_imb)
print('************************')
output =""
if len(net_delta_hist) == 3:
output += start_time.strftime('%Y-%m-%d %H:%M:%S.%f') + ","
output += end_time.strftime('%Y-%m-%d %H:%M:%S.%f') + ","
output += str(open_price) + ","
output += str(closing_price) + ","
output += str(total_volume) + ","
output += str(bar_direction) + ","
output += str(net_delta) + ","
output += str(time_diff) + ","
output += str(volume_sec) + ","
output += str(high_wick) + ","
output += str(low_wick) + ","
output += str(sell_imb) + ","#bid_imb
output += str(buy_imb) + ","#ask_imb
output += str(sd) + ","
output += str(mean_price) + ","
output += net_delta_hist[0] + ","
output += net_delta_hist[1] + ","
output += net_delta_hist[2] + ","
output += log_return + "\n"
del net_delta_hist[0]
return output
def getMean(l):
mean = 0
for v in l:
mean += v['price']
return mean/len(l)
def getStd(l):
sq_diff = 0
mean = getMean(l)
deviations = [(x['price'] - mean) ** 2 for x in l]
variance = sum(deviations) / len(l)
std_dev = math.sqrt(variance)
return std_dev
if __name__ == "__main__":
init()
parseFile()
cleanData(5)