-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDataConvertionUtilities.py
More file actions
125 lines (103 loc) · 3.68 KB
/
DataConvertionUtilities.py
File metadata and controls
125 lines (103 loc) · 3.68 KB
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
import pandas as pd
import numpy as np
import struct
import random
def SCIDReaderGen(scid):
s_IntradayHeader = '=4sIIHHI36s'
s_IntradayRecord = '=qffffIIII'
header = scid.read(struct.calcsize(s_IntradayHeader))
yield header
while True:
buf = scid.read(struct.calcsize(s_IntradayRecord))
if not buf or len(buf) != struct.calcsize(s_IntradayRecord):
break
yield struct.unpack(s_IntradayRecord, buf)
def ConvertSCIDToDF(scid_file, datetime_is_seconds=True):
reader = SCIDReaderGen(scid_file)
# skip the header
next(reader)
raw = pd.DataFrame(reader, columns=[
'StartDateTime',
'OpenPrice',
'HighPrice',
'LowPrice',
'LastPrice',
'NumTrades',
'Volume',
'BidVolume',
'AskVolume'
])
# convert to unix timestamp
raw.StartDateTime = raw.StartDateTime - (25569 * 86400 * 1000000)
if datetime_is_seconds:
raw.StartDateTime = (raw.StartDateTime / 1000000).astype('int')
return raw
def SCDepthReaderGen(depth):
s_MarketDepthFileHeader = '=4I48s'
s_MarketDepthFileRecord = '=qbbhfII'
header = depth.read(struct.calcsize(s_MarketDepthFileHeader))
yield struct.unpack(s_MarketDepthFileHeader, header)
while True:
record = depth.read(struct.calcsize(s_MarketDepthFileRecord))
if not record or len(record) != struct.calcsize(s_MarketDepthFileRecord):
break
yield struct.unpack(s_MarketDepthFileRecord, record)
def ConvertSCDepthToDF(depth_file):
reader = SCDepthReaderGen(depth_file)
# skip the header
next(reader)
raw = pd.DataFrame(reader, columns=[
'DateTime',
'Command',
'Flags',
'NumOrders',
'Price',
'Quantity',
'Reserved'
])
# convert to unix timestamp
raw.DateTime = raw.DateTime - (25569 * 86400) * 1000_000
raw.drop(columns=['Reserved'], inplace=True)
return raw
def ConvertRaw2Tick(raw_df):
raw_df['AtBidOrAsk'] = (raw_df.Volume == raw_df.AskVolume).astype(np.int32) + 1
check = (raw_df.Volume != raw_df.AskVolume) & (raw_df.Volume != raw_df.BidVolume)
if check.any() == True:
print('Correcting these errors')
print(raw_df[check])
for i in raw_df[check].index:
if raw_df.iloc[i].HighPrice != raw_df.iloc[i].LowPrice:
at_ask = abs(raw_df.iloc[i].LastPrice - raw_df.iloc[i].HighPrice) > abs(raw_df.iloc[i].LastPrice - raw_df.iloc[i].LowPrice)
raw_df.at[i, 'AtBidOrAsk'] = int(at_ask) + 1
else:
raw_df.at[i, 'AtBidOrAsk'] = random.choice([1, 2])
print('After corrections')
print(raw_df[check])
return pd.DataFrame({
'DateTime': raw_df.StartDateTime,
'Price': raw_df.LastPrice,
'Volume': raw_df.Volume,
'AtBidOrAsk': raw_df.AtBidOrAsk
})
def ConvertTick2OHLC(ticks, period=30*60):
ticks = pd.DataFrame(ticks.reset_index(drop=True))
periods = (ticks.DateTime / period).astype(np.int32)
diff = periods.diff()
diff[0] = 1
start_index = np.array(ticks[diff != 0].index)
end_index = np.roll(start_index, -1)
end_index[-1] = len(ticks)
new_index = np.arange(0, len(start_index), 1)
new_index = np.repeat(new_index, end_index - start_index)
ticks['new_index'] = new_index
group = ticks.groupby(['new_index'])
result = pd.DataFrame({
'StartDateTime': group.DateTime.first(),
'EndDateTime': group.DateTime.last(),
'Open': group.Price.first(),
'High': group.Price.max(),
'Low': group.Price.min(),
'Close': group.Price.last(),
'Volume': group.Volume.sum()
})
return result