forked from No1CharlesWu/okcoin_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_filter.py
218 lines (199 loc) · 7.88 KB
/
data_filter.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
from collections import namedtuple
import threading
import datetime
class DataFilter(object):
__instance = None
def __init__(self):
self.__ticker_list = list()
self.__depth_list = list()
self.__trades_list = list()
self.__kline_list = list()
self.lock_ticker_list = threading.Lock()
self.lock_depth_list = threading.Lock()
self.lock_trades_list = threading.Lock()
self.lock_kline_list = threading.Lock()
def __new__(cls, *args, **kwargs):
if DataFilter.__instance is None:
DataFilter.__instance = object.__new__(cls, *args, **kwargs)
return DataFilter.__instance
def websocket_add_data(self, data):
channel = data['channel']
if 'ticker' in channel:
t = threading.Thread(target=self.websocket_ticker_filter, name='ticker_filter', args=(data,))
t.start()
elif 'depth' in channel:
d = threading.Thread(target=self.websocket_depth_filter, name='depth_filter', args=(data,))
d.start()
elif 'trades' in channel:
tr = threading.Thread(target=self.websocket_trades_filter, name='trades_filter', args=(data,))
tr.start()
elif 'kline' in channel:
k = threading.Thread(target=self.websocket_kline_filter, name='kline_filter', args=(data,))
k.start()
def websocket_ticker_filter(self, data):
self.lock_ticker_list.acquire()
try:
self.__ticker_list.append(data['data'])
print('ticker: websocket add data')
finally:
self.lock_ticker_list.release()
def rest_add_data_for_ticker(self, data):
self.lock_ticker_list.acquire()
try:
temp = data['ticker']
for k, v in temp.items():
temp[k] = float(v)
temp['timestamp'] = int(data['date']) * 1000
self.__ticker_list.append(temp)
print('ticker: rest add data')
finally:
self.lock_ticker_list.release()
def get_ticker_list(self):
if len(self.__ticker_list) == 0:
return None
self.lock_ticker_list.acquire()
try:
self.__ticker_list.sort(key=lambda d: d['timestamp'])
r_data = self.__ticker_list.pop()
self.__ticker_list.clear()
print('ticker: get data.')
return r_data
finally:
self.lock_ticker_list.release()
def websocket_depth_filter(self, data):
self.lock_depth_list.acquire()
try:
# print('depth: websocket add data')
temp = data['data']
for i, l in enumerate(temp['asks']):
temp['asks'][i][0] = float(l[0])
temp['asks'][i][1] = float(l[1])
for i, l in enumerate(temp['bids']):
temp['bids'][i][0] = float(l[0])
temp['bids'][i][1] = float(l[1])
# print(datetime.datetime.now(), datetime.datetime.fromtimestamp(data['data']['timestamp']/1000))
self.__depth_list.append(temp)
print('depth: websocket add data')
finally:
self.lock_depth_list.release()
def rest_add_data_for_depth(self, data):
self.lock_depth_list.acquire()
try:
data['timestamp'] = int(datetime.datetime.now().timestamp() * 1000)
self.__depth_list.append(data)
print('depth: rest add data')
finally:
self.lock_depth_list.release()
def get_depth_list(self):
if len(self.__depth_list) == 0:
# return {'asks':[],'bids':[],'timestamp':0}
return None
self.lock_depth_list.acquire()
try:
self.__depth_list.sort(key=lambda d: d['timestamp'])
r_data = self.__depth_list.pop()
self.__depth_list.clear()
print('depth: get data.')
return r_data
finally:
self.lock_depth_list.release()
def websocket_trades_filter(self, data):
t_data = data['data']
date = str(datetime.datetime.now().date())
self.lock_trades_list.acquire()
try:
for i, l in enumerate(t_data):
temp = dict()
temp['tid'] = int(l[0])
temp['price'] = float(l[1])
temp['amount'] = float(l[2])
t_date = date + ' ' + l[3]
temp['timestamp'] = int(datetime.datetime.strptime(t_date, '%Y-%m-%d %H:%M:%S').timestamp() * 1000)
temp['type'] = l[4]
# print(i, temp)
if temp not in self.__trades_list:
self.__trades_list.append(temp)
# for j, d in enumerate(self.__trades_list):
# print(j, d)
print('trades: websocket add data')
finally:
self.lock_trades_list.release()
def rest_add_data_for_trades(self, data):
self.lock_trades_list.acquire()
try:
# print('rest_add_data_for_trades:', self.__trades_list)
self.__trades_list.clear()
for i, d in enumerate(data):
temp = dict()
temp['tid'] = d['tid']
temp['price'] = float(d['price'])
temp['amount'] = float(d['amount'])
temp['timestamp'] = d['date_ms']
temp['type'] = 'bid' if d['type'] == 'buy' else 'ask'
self.__trades_list.append(temp)
# print(i, temp)
print('trades: rest add data')
# for j, d in enumerate(self.__trades_list):
# print(j, d)
finally:
self.lock_trades_list.release()
def get_trades_list(self):
if len(self.__trades_list) == 0:
return []
self.lock_trades_list.acquire()
try:
# print(self.__trades_list, type(self.__trades_list))
self.__trades_list.sort(key=lambda t: t['timestamp'], reverse=True)
print('trades: get data.')
self.__trades_list = self.__trades_list[:60]
return self.__trades_list
finally:
self.lock_trades_list.release()
def websocket_kline_filter(self, data):
t_data = data['data']
self.lock_kline_list.acquire()
try:
for i, l in enumerate(t_data):
temp = dict()
temp['timestamp'] = int(l[0])
temp['open'] = float(l[1])
temp['high'] = float(l[2])
temp['low'] = float(l[3])
temp['close'] = float(l[4])
temp['vol'] = float(l[5])
for j, d in enumerate(self.__kline_list):
if temp['timestamp'] == d['timestamp']:
self.__kline_list.pop(j)
break
self.__kline_list.append(temp)
print('kline: websocket add data')
finally:
self.lock_kline_list.release()
def rest_add_data_for_kline(self, data):
self.lock_kline_list.acquire()
try:
self.__kline_list.clear()
for i, l in enumerate(data):
temp = dict()
temp['timestamp'] = l[0]
temp['open'] = l[1]
temp['high'] = l[2]
temp['low'] = l[3]
temp['close'] = l[4]
temp['vol'] = l[5]
self.__kline_list.append(temp)
print('kline: rest add data')
finally:
self.lock_kline_list.release()
def get_kline_list(self):
if len(self.__kline_list) == 0:
return []
self.lock_kline_list.acquire()
try:
self.__kline_list.sort(key=lambda k: k['timestamp'], reverse=True)
print('kline: get data.')
self.__kline_list = self.__kline_list[:40]
return self.__kline_list
finally:
self.lock_kline_list.release()
global_data_filter = DataFilter()