forked from Reidmcc/Binance-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_checks.py
234 lines (204 loc) · 9.71 KB
/
data_checks.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
from api import BinanceAPI
client = BinanceAPI('[API key]','[API secret]')
from excepts import MalformedRequest, StatusUnknown, InternalError
from requests.exceptions import ConnectionError
from http.client import RemoteDisconnected
from urllib3.exceptions import ProtocolError
from http.client import HTTPException
from Trades_simul import Trades
# import sys
# import time
from log import log_c
class Data_checks(object):
def __init__(self):
self.lg = log_c()
self.tr = Trades()
self.logfile = 'Default_log.txt'
def setfile(self, log_file):
self.lg.set_file(log_file)
self.logfile = log_file
def route_check_altfirst(self, pair_1, pair_2, pair_3, pair_1_pip, pair_2_pip, pair_3_pip):
calc_done = False
route_gain = -1
bid_p1 = -1
ask_p2 = -1
bid_p3 = -1
while calc_done == False:
try:
bid_p1 = float(client.depth(pair_1, limit = 5)['bids'][0][0])
ask_p2 = float(client.depth(pair_2, limit = 5)['asks'][0][0])
bid_p3 = float(client.depth(pair_3, limit = 5)['bids'][0][0])
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
route_gain = (1 / (bid_p1 + pair_1_pip)) * (ask_p2 - pair_2_pip) / (bid_p3 + pair_3_pip)
calc_done = True
return (route_gain, bid_p1, ask_p2, bid_p3)
def route_check_altlast(self, pair_1, pair_2, pair_3, pair_1_pip, pair_2_pip, pair_3_pip):
calc_done = False
route_gain = -1
bid_p2 = -1
ask_p3 = -1
ask_p1 = -1
while calc_done == False:
try:
ask_p1 = float(client.depth(pair_1, limit = 5)['asks'][0][0]) - float(pair_1_pip)
bid_p2 = float(client.depth(pair_2, limit = 5)['bids'][0][0]) + float(pair_2_pip)
ask_p3 = float(client.depth(pair_3, limit = 5)['asks'][0][0]) - float(pair_3_pip)
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
# route_gain = ((ask_p1 - pair_1_pip) / ((bid_p2 + pair_2_pip)) * (ask_p3 - pair_3_pip))
route_gain = ask_p1 / bid_p2 * ask_p3
calc_done = True
return (route_gain, ask_p1, bid_p2, ask_p3)
def route_check_altlast_take_t2(self, pair_1, pair_2, pair_3, pair_1_pip, pair_3_pip):
calc_done = False
route_gain = -1
while calc_done == False:
try:
ask_p1 = float(client.depth(pair_1, limit = 5)['asks'][0][0])
ask_p2 = float(client.depth(pair_2, limit = 5)['asks'][0][0])
ask_p3 = float(client.depth(pair_3, limit = 5)['asks'][0][0])
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
else:
route_gain = ((ask_p1 - pair_1_pip) / ((ask_p2)) * (ask_p3 - pair_3_pip))
calc_done = True
return route_gain
def route_check_altlast_take_t3(self, pair_1, pair_2, pair_3, pair_1_pip, pair_2_pip):
calc_done = False
route_gain = -1
ask_p1 = -1
bid_p2 = -1
bid_p3 = -1
p3_bid_quant = -1
while calc_done == False:
try:
ask_p1 = float(client.depth(pair_1, limit = 5)['asks'][0][0])
bid_p2 = float(client.depth(pair_2, limit = 5)['bids'][0][0])
p3_depth = client.depth(pair_3, limit = 5)
bid_p3 = float(p3_depth['bids'][0][0])
p3_bid_quant = float(p3_depth['bids'][0][1])
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
route_gain = ((ask_p1 - pair_1_pip) / ((bid_p2 + pair_2_pip)) * (bid_p3))
calc_done = True
return (route_gain, ask_p1, bid_p2, bid_p3, p3_bid_quant)
def route_check_altlast_take_t2_t3(self, pair_1, pair_2, pair_3, pair_1_pip):
calc_done = False
route_gain = -1
while calc_done == False:
try:
ask_p1 = float(client.depth(pair_1, limit = 5)['asks'][0][0])
ask_p2 = float(client.depth(pair_2, limit = 5)['asks'][0][0])
bid_p3 = float(client.depth(pair_3, limit = 5)['bids'][0][0])
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
route_gain = (ask_p1 - pair_1_pip) / ask_p2 * bid_p3
calc_done = True
return route_gain
def get_low_ask(self, pair):
ask = -1
got_ask = False
while got_ask == False:
try:
ask = client.depth(pair, limit = 5)['asks'][0][0]
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
got_ask = True
return ask
def get_high_bid(self, pair):
bid = -1
got_bid = False
while got_bid == False:
try:
bid = client.depth(pair, limit = 5)['bids'][0][0]
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
got_bid = True
return bid
def route_check_alt_last_lastprice(self, pair_1, pair_2, pair_3, pair_1_pip, pair_2_pip, pair_3_pip):
route_gain = -1
calc_done = False
t_1_price = -1
t_2_price = -1
t_3_price = -1
while calc_done == False:
try:
prices = client.allPrices()
for p in prices:
symbol = p['symbol']
if symbol == pair_1:
t_1_price = float(p['price'])
if symbol == pair_2:
t_2_price = float(p['price'])
if symbol == pair_3:
t_3_price = float(p['price'])
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
route_gain = (t_1_price / (t_2_price) * t_3_price)
calc_done = True
return route_gain
def route_check_t3_ask_oth_lastprice(self, pair_1, pair_2, pair_3, pair_1_pip, pair_2_pip, pair_3_pip):
route_gain = -1
ask_p3 = -1
calc_done = False
t_1_price = -1
t_2_price = -1
while calc_done == False:
try:
prices = client.allPrices()
for p in prices:
symbol = p['symbol']
if symbol == pair_1:
t_1_price = float(p['price']) - pair_1_pip
if symbol == pair_2:
t_2_price = float(p['price']) + pair_2_pip
ask_p3 = float(client.depth(pair_3, limit=5)['asks'][0][0]) - pair_3_pip
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
route_gain = t_1_price / t_2_price * ask_p3
calc_done = True
return (route_gain, t_1_price, t_2_price, ask_p3)
def route_check_altfirst_t1_bid_oth_lastprice(self, pair_1, pair_2, pair_3, pair_1_pip, pair_2_pip, pair_3_pip):
route_gain = -1
bid_p1 = -1
t_2_price = -1
t_3_price = -1
calc_done = False
while calc_done == False:
try:
prices = client.allPrices()
for p in prices:
symbol = p['symbol']
if symbol == pair_2:
t_2_price = float(p['price']) + pair_2_pip
if symbol == pair_3:
t_3_price = float(p['price']) - pair_3_pip
bid_p1 = float(client.depth(pair_1, limit = 5)['bids'][0][0])
# ask_p2 = float(client.depth(pair_2, limit = 5)['asks'][0][0])
# bid_p3 = float(client.depth(pair_3, limit = 5)['bids'][0][0])
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
self.lg.log(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
route_gain = (1 / (bid_p1 + pair_1_pip)) * (t_2_price - pair_2_pip) / (t_3_price + pair_3_pip)
calc_done = True
return (route_gain, bid_p1, t_2_price, t_3_price)
# skit_opt_ETH = dc.route_check_altfirst('XLMBNB', 'XLMETH', 'BNBETH', 1E-5, 1E-8, 1E-6)
# print(skit_opt_ETH)