-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_binance_data_with_async_websockets_v7.py
210 lines (168 loc) · 7.35 KB
/
get_binance_data_with_async_websockets_v7.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
import websocket
import pandas as pd
import numpy as np
import json
import pprint
import time
import sys
import threading
from trading_bot_v7 import TraderBot
import traceback
from binance.client import Client as API_Client
from config import api_key,api_secret
class Client(threading.Thread):
def __init__(self,socket,bot):
super().__init__()
self.ws = websocket.WebSocketApp(
url = self.SOCKET,
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close,
on_open = self.on_open
)
self.bot = bot
# convert message to dict, process update
def on_message(self,ws, message):
pass
# catch errors
def on_error(self, ws,E):
# print('error')
# print(E)
traceback.print_exc()
# run when websocket is closed
def on_close(self,ws,*args,**kwargs):
print("### closed ###")
# run when websocket is initialised
def on_open(self,ws):
print(f'Connected to Binance {self.market}\n')
def run(self):
self.ws.run_forever()
class Binance_bookTicker(Client):
instance_list = []
locks = {}
def __init__(self,market,imax,bot):
self.market = market
self.SOCKET = "wss://stream.binance.com:9443/ws/"+market+"@bookTicker"
super().__init__(self.SOCKET,bot)
self.df = pd.DataFrame(columns=['timestamps','bid_qty','bid_price','ask_qty','ask_price'])
self.i = 0
self.imax = imax
Binance_bookTicker.instance_list.append(self)
Binance_bookTicker.locks[self.market] = threading.Lock()
def on_message(self,ws,message):
json_message = json.loads(message)
#pprint.pprint(json_message)
self.bot.market_dict[self.market] = {'bid_qty':json_message['B'],'bid_price':json_message['b'],'ask_qty':json_message['A'],'ask_price':json_message['a']}
#pprint.pprint(market_dict)
# We can also save the data if needed
#df_socket = pd.DataFrame({'timestamps':[time.time()],'id':[json_message['u']],'bid_qty':[json_message['B']],'bid_price':[json_message['b']],'ask_qty':[json_message['A']],'ask_price':[json_message['a']]})
#self.df = pd.concat([self.df,df_socket],ignore_index=True)
start_cash = self.bot.cash_tracker
self.i+=1
print(self.i)
if self.i > self.imax:
# self.df.to_csv(self.market+'_socket_book.csv')
# ws.close()
Binance_bookTicker.close_all()
Binance_depth.close_all()
if not Binance_bookTicker.locks[self.market].locked():
for market in Binance_bookTicker.locks:
Binance_bookTicker.locks[market].acquire()
self.bot.make_trade()#market_dict,full_book_market_dict,cash = make_trade(self.bot.market_dict,full_book_market_dict,cash,client)
for market in Binance_bookTicker.locks:
Binance_bookTicker.locks[market].release()
#market_dict,full_book_market_dict,cash = make_trade(market_dict,full_book_market_dict,cash,client)
#print('need to stop : ',self.bot.need_to_stop)
if self.bot.need_to_stop:#bot safety (in case its loosing money)
Binance_bookTicker.close_all()
Binance_depth.close_all()
def on_open(self,ws):
print('Opening connection!')
@classmethod
def close_all(cls):
#global market_dict,cash
for self_ in Binance_bookTicker.instance_list:
print(f'Closing {self_.market}')
#self_.df.to_csv(self_.market+'_socket_book_async.csv',index=False)
self_.ws.close()
len_df = self_.df.shape[0]
print(f'{self_.market} closed with {len_df} data points')
#pprint.pprint(market_dict)
#print(self.bot.cash_tracker)
class Binance_depth(Client):
instance_list = []
locks = {}
def __init__(self,market,bot):
self.market = market
self.SOCKET = "wss://stream.binance.com:9443/ws/"+market+"@depth20@100ms"
super().__init__(self.SOCKET,bot)
self.df = pd.DataFrame(columns=['timestamps','id'])
Binance_depth.instance_list.append(self)
Binance_depth.locks[market] = threading.Lock()
def on_message(self,ws,message):
json_message = json.loads(message)
#pprint.pprint(json_message)
# Step 1 -> update data
# Step 2 -> make trade
#df_socket = pd.DataFrame({'timestamps':[time.time()],'id':[json_message['lastUpdateId']]})
bid_price = []
bid_qty = []
ask_price = []
ask_qty = []
start_cash = self.bot.cash_tracker
for i in range(len(json_message['bids'])):
bid_price.append(float(json_message['bids'][i][0]))
bid_qty.append(float(json_message['bids'][i][1]))
ask_price.append(float(json_message['asks'][i][0]))
ask_qty.append(float(json_message['asks'][i][1]))
# bid_qty = 'bid_qty'+str(i)
# bid_price = 'bid_price'+str(i)
# ask_qty = 'ask_qty'+str(i)
# ask_price = 'ask_price'+str(i)
# df_socket[bid_price] = json_message['bids'][i][0]
# df_socket[bid_qty] = json_message['bids'][i][1]
# df_socket[ask_price] = json_message['asks'][i][0]
# df_socket[ask_qty] = json_message['asks'][i][1]
# self.df = pd.concat([self.df,df_socket],ignore_index=True)
self.bot.full_book_market_dict[self.market]={'bid_qty':bid_qty,'bid_price':bid_price,'ask_qty':ask_qty,'ask_price':ask_price}
if not Binance_depth.locks[self.market].locked():
for lock in Binance_depth.locks:
lock.acquire()
self.bot.make_trade()#market_dict,full_book_market_dict,cash = make_trade(self.bot.market_dict,full_book_market_dict,cash,client)
for lock in Binance_depth.locks:
lock.release()
#print('need to stop : ',self.bot.need_to_stop)
if self.bot.need_to_stop:#bot safety (in case its loosing money)
Binance_bookTicker.close_all()
Binance_depth.close_all()
def on_open(self,ws):
print('Opening connection!')
def on_close(self,*args,**kwargs):
Binance_bookTicker.close_all()
Binance_depth.close_all()
@classmethod
def close_all(cls):
global full_book_market_dict,cash
for self_ in Binance_depth.instance_list:
print(f'Closing {self_.market}')
#self_.df.to_csv(self_.market+'_socket_full_book_async.csv',index=False)
self_.ws.close()
len_df = self_.df.shape[0]
print(f'{self_.market} full book closed with {len_df} data points')
#pprint.pprint(full_book_market_dict)
#print(self_.bot.cash_tracker)
if __name__ == '__main__':
#sys.tracebacklimit = 0
i_max = 1000000
start_cash = cash = 100
market1,market2,market3 = 'btcusdt','ethbtc','ethusdt'
#variable to keep track of full book data
market_dict = {market1:None,market2:None,market3:None}
#variable to keep track of full book data
full_book_market_dict = {market1:None,market2:None,market3:None}
#trading bot
bot = TraderBot(market1,market2,market3,cash,market_dict,full_book_market_dict)
binance_ethbtc = Binance_bookTicker(market2,i_max,bot)
binance_ethusdt = Binance_bookTicker(market3,i_max,bot)
binance_ethbtc.start()
binance_ethusdt.start()