-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsybrain.py
367 lines (342 loc) · 16.9 KB
/
sybrain.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from datetime import datetime as dt, timezone
from datetime import timedelta
from stocks import Main # Machine Learning Module
import pandas as pd
from time import sleep
from utils import bitmex_http_com as bitmex
from utils.bitmex_websocket_com import BitMEXWebsocket
#########################################
API_path_bmex_test = "wss://www.bitmex.com/realtime"
API_key_bmex_test = "key"
API_secret_bmex_test = "secret"
instrument_bmex = "XBTUSD" # XBTUSD or ETHUSD
pos_size = 1 # Number of contracts traded
#########################################
matrix_bmex_ticker = [None] * 3
matrix_bmex_trade = [None] * 5
matrix_bmex_fairPrice = [None] * 10
matrix_bmex_fairPrice_var = [None] * 10
tick_count = 0
tick_ok = False
ws_bmex = BitMEXWebsocket(endpoint=API_path_bmex_test, symbol=instrument_bmex,
api_key=API_key_bmex_test, api_secret=API_secret_bmex_test)
client = bitmex.bitmex(test=False, api_key=API_key_bmex_test, api_secret=API_secret_bmex_test)
pos_taken = 0
def check_order_book(direction):
print("Checking OrderBook...")
obk = ws_bmex.market_depth()
obk_bids = obk[0]['bids'][0:5]
obk_asks = obk[0]['asks'][0:5]
# print("Bids:", obk_bids)
# print("Asks:", obk_asks)
obk_buy_cum = 0
obk_sell_cum = 0
for obk_bid_unit in obk_bids:
obk_buy_cum += obk_bid_unit[1]
for obk_ask_unit in obk_asks:
obk_sell_cum += obk_ask_unit[1]
print("Sell Side: %s - Buy Side: %s" % (str(obk_sell_cum), str(obk_buy_cum)))
if direction == 1 and obk_buy_cum > obk_sell_cum * 10:
print("Go Buy !")
return obk[0]['bids'][1][0]
if direction == 0 and obk_sell_cum > obk_buy_cum * 10:
print("Go Sell !")
return obk[0]['asks'][1][0]
return 0
def covering_fee(init, current, direction):
if direction == 'buy':
target = init + (init / 100 * 0.15)
if current > target:
return True
if direction == 'sell':
target = init - (init / 100 * 0.15)
if current < target:
return True
return False
def fire_buy(departure):
counter = 0
print("Balance before:", ws_bmex.wallet_balance())
'''launch_order(definition='stop_limit', direction='buy',
price=departure, stoplim=matrix_bmex_ticker[1], size=pos_size)'''
'''launch_order(definition='stop_limit', direction='buy',
price=matrix_bmex_ticker[1], stoplim=departure, size=pos_size)'''
launch_order(definition='market', direction='buy', size=pos_size)
# launch_order(definition='limit', direction='buy', price=departure, size=pos_size)
while ws_bmex.open_positions() == 0:
sleep(1)
counter += 1
if counter >= 120:
client.Order.Order_cancelAll().result()
return 0
if ws_bmex.open_stops() == 0:
return 0
continue
print("BUY @", matrix_bmex_ticker[1])
print("Balance - Step 1 of 2:", ws_bmex.wallet_balance())
buyPos = 1
buyPos_init = ws_bmex.get_instrument()['askPrice']
buyPos_working_cached = buyPos_init
ts_cached = [None]
tick_buy_count = 0
buyPos_final = 0
trailing = False
sl_ord_number = 0
while buyPos > 0:
datetime_cached = ws_bmex.get_instrument()['timestamp']
dt2ts = dt.strptime(datetime_cached, '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=timezone.utc).timestamp()
matrix_bmex_ticker[0] = int(dt2ts * 1000)
matrix_bmex_ticker[1] = ws_bmex.get_instrument()['askPrice']
matrix_bmex_ticker[2] = ws_bmex.get_instrument()['bidPrice']
if ts_cached != matrix_bmex_ticker[0]:
tick_buy_count += 1
ts_cached = matrix_bmex_ticker[0]
if tick_buy_count >= 500 and trailing is False:
print("Cutting buy loss !")
if len(ws_bmex.open_stops()) != 0:
client.Order.Order_cancelAll().result()
if ws_bmex.open_positions() != 0:
# launch_order(definition='market', direction='sell', price=None, size=pos_size)
launch_order(definition='stop_loss', direction='sell', price=matrix_bmex_ticker[1], size=pos_size)
while ws_bmex.open_positions() != 0:
sleep(0.1)
continue
buyPos_final = matrix_bmex_ticker[2]
break
if buyPos_working_cached < matrix_bmex_ticker[2] and ws_bmex.open_positions() != 0:
if len(ws_bmex.open_stops()) is not 0:
client.Order.Order_amend(orderID=sl_ord_number, stopPx=matrix_bmex_ticker[2]).result()
print("Trailing buy position to %s..." % str(matrix_bmex_ticker[2]))
if len(ws_bmex.open_stops()) is 0 and covering_fee(buyPos_init, matrix_bmex_ticker[2], 'buy'):
print("Buy stop loss to BE...")
sl_ord_number = launch_order(definition='stop_loss', direction='sell', price=matrix_bmex_ticker[2],
size=pos_size)
trailing = True
if trailing is False:
continue
buyPos_working_cached = matrix_bmex_ticker[2]
print("Buy stop loss modified at %s..." % str(buyPos_working_cached))
if ws_bmex.open_positions() == 0 and trailing is True:
if ws_bmex.open_positions() != 0:
print(sl_ord_number)
if len(ws_bmex.open_stops()) is not 0:
client.Order.Order_amend(orderID=sl_ord_number, orderQty=0).result() # cancel stop order
launch_order(definition='market', direction='sell', price=None, size=pos_size)
print("Finishing buy trail...")
buyPos_final = matrix_bmex_ticker[2]
break
walletBal = ws_bmex.wallet_balance()
print("Closed @", str(buyPos_final), ". Balance:", str(walletBal))
return walletBal
def fire_sell(departure):
counter = 0
print("Balance before:", ws_bmex.wallet_balance())
'''launch_order(definition='stop_limit', direction='sell',
price=departure, stoplim=matrix_bmex_ticker[2], size=pos_size)'''
'''launch_order(definition='stop_limit', direction='sell',
price=matrix_bmex_ticker[2], stoplim=departure, size=pos_size)'''
launch_order(definition='market', direction='sell', size=pos_size)
# launch_order(definition='limit', direction='sell', price=departure, size=pos_size)
while ws_bmex.open_positions() == 0:
sleep(1)
counter += 1
if counter >= 120:
client.Order.Order_cancelAll().result()
return 0
if ws_bmex.open_stops() == 0:
return 0
continue
print("SELL @", matrix_bmex_ticker[2])
print("Balance - Step 1 of 2:", ws_bmex.wallet_balance())
sellPos = 1
sellPos_init = ws_bmex.get_instrument()['bidPrice']
sellPos_working_cached = sellPos_init
ts_cached = [None]
tick_sell_count = 0
sellPos_final = 0
trailing = False
sl_ord_number = 0
while sellPos > 0:
datetime_cached = ws_bmex.get_instrument()['timestamp']
dt2ts = dt.strptime(datetime_cached, '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=timezone.utc).timestamp()
matrix_bmex_ticker[0] = int(dt2ts * 1000)
matrix_bmex_ticker[1] = ws_bmex.get_instrument()['askPrice']
matrix_bmex_ticker[2] = ws_bmex.get_instrument()['bidPrice']
if ts_cached != matrix_bmex_ticker[0]:
tick_sell_count += 1
ts_cached = matrix_bmex_ticker[0]
if tick_sell_count >= 500 and trailing is False:
print("Cutting sell loss !")
if len(ws_bmex.open_stops()) != 0:
client.Order.Order_cancelAll().result()
if ws_bmex.open_positions() != 0:
# launch_order(definition='market', direction='buy', price=None, size=pos_size)
launch_order(definition='stop_loss', direction='buy', price=matrix_bmex_ticker[2], size=pos_size)
while ws_bmex.open_positions() != 0:
sleep(0.1)
continue
sellPos_final = matrix_bmex_ticker[1]
break
if sellPos_working_cached > matrix_bmex_ticker[1] and ws_bmex.open_positions() != 0:
if len(ws_bmex.open_stops()) is not 0:
client.Order.Order_amend(orderID=sl_ord_number, stopPx=matrix_bmex_ticker[1]).result()
print("Trailing sell position to %s..." % str(matrix_bmex_ticker[1]))
if len(ws_bmex.open_stops()) is 0 and covering_fee(sellPos_init, matrix_bmex_ticker[1], 'sell'):
print("Sell stop loss to BE...")
sl_ord_number = launch_order(definition='stop_loss', direction='buy', price=matrix_bmex_ticker[1],
size=pos_size)
trailing = True
if trailing is False:
continue
sellPos_working_cached = matrix_bmex_ticker[1]
print("Sell stop loss modified at %s..." % str(sellPos_working_cached))
if ws_bmex.open_positions() == 0 and trailing is True:
if ws_bmex.open_positions() != 0:
print(sl_ord_number)
if len(ws_bmex.open_stops()) is not 0:
client.Order.Order_amend(orderID=sl_ord_number, orderQty=0).result() # cancel stop order
launch_order(definition='market', direction='buy', price=None, size=pos_size)
print("Finishing sell trail...")
sellPos_final = matrix_bmex_ticker[1]
break
walletBal = ws_bmex.wallet_balance()
print("Closed @", str(sellPos_final), ". Balance:", str(walletBal))
return walletBal
def launch_order(definition, direction, price=None, size=None, stoplim=None):
resulted = 0
if definition == 'market':
if direction == 'sell':
size *= -1
resulted = client.Order.Order_new(symbol=instrument_bmex, orderQty=size, ordType='Market').result()
return resulted[0]['orderID']
if definition == 'limit':
if direction == 'sell':
size *= -1
resulted = client.Order.Order_new(symbol=instrument_bmex, orderQty=size, ordType='Limit', price=price,
execInst='ParticipateDoNotInitiate, LastPrice').result()
return resulted[0]['orderID']
if definition == 'stop_limit':
if direction == 'sell':
size *= -1
resulted = client.Order.Order_new(symbol=instrument_bmex, orderQty=size, ordType='StopLimit',
execInst='LastPrice',
stopPx=stoplim, price=price).result()
return resulted[0]['orderID']
if definition == 'stop_loss':
if direction == 'sell':
size *= -1
resulted = client.Order.Order_new(symbol=instrument_bmex, orderQty=size, ordType='Stop',
execInst='Close, LastPrice',
stopPx=price).result()
return resulted[0]['orderID']
if definition == 'take_profit':
if direction == 'sell':
size *= -1
resulted = client.Order.Order_new(symbol=instrument_bmex, orderQty=size, ordType='Limit',
execInst='Close, LastPrice',
price=price).result()
return resulted[0]['orderID']
def bmex():
global matrix_bmex_ticker
global matrix_bmex_trade
global matrix_bmex_fairPrice
global matrix_bmex_fairPrice_var
global pos_taken
global tick_count
global tick_ok
fP_value_sum = 0
fP_var_value_sum = 0
fP_var_value_av = 0
fairPrice_var_actual = 0
results = 0
p_verdict = 0
datetime_minute_cached = None
ts_cached = [None]
fP_cached = [None] * 2
while ws_bmex.ws.sock.connected:
try:
if datetime_minute_cached != dt.now().minute:
time_starter = dt.utcnow() - timedelta(minutes=750)
# print(time_starter)
data = client.Trade.Trade_getBucketed(symbol=instrument_bmex, binSize="1m", count=750,
startTime=time_starter).result()
df = pd.DataFrame(columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close'])
j = 0
for i in data[0]:
df.loc[j] = pd.Series({'Date': i['timestamp'], 'Open': i['open'], 'High': i['high'],
'Low': i['low'], 'Close': i['close'], 'Volume': i['volume'],
'Adj Close': i['close']})
j += 1
df = df[::-1]
df.to_csv(r'pair_m1.csv', index=False)
print('Launching Machine learning Module...')
start_ts = (dt.utcnow()+timedelta(minutes=0)).strftime("%Y-%m-%d %H:%M:00")
end_ts = (dt.utcnow()+timedelta(minutes=5)).strftime("%Y-%m-%d %H:%M:00")
print('Start:', start_ts, '/ End:', end_ts)
p = Main(['pair_m1.csv', start_ts, end_ts, 'm'])
p_open = p.loc[p.shape[0]-1, 'Open']
p_close = p.loc[p.shape[0]-1, 'Close']
p_verdict = p_close - p_open
if p_verdict > 0:
print('Machine learning : UP !')
if p_verdict < 0:
print('Machine learning : DOWN !')
datetime_minute_cached = dt.now().minute
datetime_cached = ws_bmex.get_instrument()['timestamp']
dt2ts = dt.strptime(datetime_cached, '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=timezone.utc).timestamp()
matrix_bmex_ticker[0] = int(dt2ts * 1000) # (dt2ts - dt(1970, 1, 1)) / timedelta(seconds=1000)
matrix_bmex_ticker[1] = ws_bmex.get_instrument()['askPrice']
matrix_bmex_ticker[2] = ws_bmex.get_instrument()['bidPrice']
if ts_cached != matrix_bmex_ticker[0] and fP_cached[0] != ws_bmex.get_instrument()['fairPrice']:
ts_cached = matrix_bmex_ticker[0]
fP_cached[1] = fP_cached[0]
fP_cached[0] = ws_bmex.get_instrument()['fairPrice']
if fP_cached[0] is not None and fP_cached[1] is not None:
matrix_bmex_fairPrice_var[tick_count] = (fP_cached[0] - fP_cached[1]) / 100
fairPrice_var_actual = matrix_bmex_fairPrice_var[tick_count]
tick_count += 1
if tick_count >= 10:
tick_count = 0
if tick_ok is False:
tick_ok = True
print('Caching Complete !')
if tick_ok is True:
fP_var_value_sum = 0
for fP_var_value in matrix_bmex_fairPrice_var:
fP_var_value_sum += fP_var_value
fP_var_value_av = fP_var_value_sum / 10
print("Average Fair Price Variation: %s - Last Variation: %s - Last Fair Price: %s - Position Taken: %s - Balance: %s" %
(str(fP_var_value_av), str(fairPrice_var_actual), str(ws_bmex.get_instrument()['fairPrice']), str(pos_taken), str(ws_bmex.wallet_balance())))
if fP_var_value_av > 0 and fairPrice_var_actual > fP_var_value_av * 2 and p_verdict > 0:
buy_departure = check_order_book(1)
if buy_departure != 0:
results = fire_buy(buy_departure)
if results == 0:
print("Resetting...")
else:
print("Total Balance:", str(results))
if results != 0:
pos_taken += 1
tick_ok = False
tick_count = 0
if fP_var_value_av < 0 and fairPrice_var_actual < fP_var_value_av * 2 and p_verdict < 0:
sell_departure = check_order_book(0)
if sell_departure != 0:
results = fire_sell(sell_departure)
if results == 0:
print("Resetting...")
else:
print("Total Balance:", str(results))
if results != 0:
pos_taken += 1
tick_ok = False
tick_count = 0
except Exception as e:
print(str(e))
ws_bmex.exit()
print(" This is the end !")
except KeyboardInterrupt:
ws_bmex.exit()
print(" This is the end !")
if __name__ == '__main__':
print("It began in Africa...")
bmex()