-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbittrexbackup.py
504 lines (370 loc) · 18.1 KB
/
bittrexbackup.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import sys
import pprint
import numpy as np
from copy import deepcopy
import time
import datetime
from bittrex_api import BittrexAPI
# https://cryptocoincharts.info/markets/show/kraken
class PathList():
def __init__(self, all_assets, start_points):
self.path_list = []
self.complete_paths = []
self.initialize(start_points)
self.asset_list = all_assets
def initialize(self,asset_list):
for asset in asset_list:
new_path = Path( [asset] )
self.path_list.append(new_path)
def step(self, final_step = False):
new_path_list = []
for path in self.path_list:
next_moves = path.nodes[-1].avail_trades
# If we're on the final step, return to the initial asset
if final_step:
# Next move returns you to the original asset
next_moves = [path.nodes[0].name]
# See if we can get back to the intial coin
try:
# Get the index of the original asset from the list of available trades
complete_loop_index = path.nodes[-1].avail_trades.index(next_moves[0])
# We're in too deep, cant get back to original coin
except:
# Skip this loop
continue
for i in range(0,len(next_moves)):
move = next_moves[i]
# If its the last step, correct the index for the particular trade you want
if final_step:
i = complete_loop_index
# THESE ARE THE PREVIOUS LISTS> TO BE APPENDED.
link = path.nodes[-1].conversions[i]
direction = path.nodes[-1].order_actions[i]
asset_pair = path.nodes[-1].pair_names[i]
order_vol_denoms = path.nodes[-1].order_vol_denoms[i]
btc_volumes = path.nodes[-1].btc_volumes[i]
order_price = path.nodes[-1].order_prices[i]
#move is a string
for asset in self.asset_list:
if asset.name==move:
move_asset = asset
new_nodes = (path.nodes+[move_asset])
new_links = (path.links+[ link ])
new_directions = (path.directions+ [ direction ])
new_asset_pair = (path.asset_pairs+ [ asset_pair ])
new_order_vol_denoms = (path.order_vol_denoms + [ order_vol_denoms ])
new_btc_volumes = (path.btc_volumes + [ btc_volumes ])
new_order_prices = (path.order_prices + [ order_price ])
new_path = Path(new_nodes, new_links, new_directions, new_asset_pair, new_order_vol_denoms, new_btc_volumes, new_order_prices)
new_path_list.append(new_path)
self.path_list = new_path_list
self.remove_complete_paths()
def remove_complete_paths(self):
for path in self.path_list:
if path.is_complete():
self.complete_paths.append(path)
self.path_list.remove(path)
# if path.value>1:
# print('maybe we will make some money MN: ',path.value)
def show_paths(self):
self.complete_paths.sort(key=lambda r: -r.value)
for i in range(0,5):
# for i in range(0,len(self.complete_paths)):
try:
self.complete_paths[i].print_path()
except:
continue
print('\n')
def return_best(self):
self.complete_paths.sort(key=lambda r: -r.value)
return self.complete_paths[0]
class Path():
def __init__(self,init_node, links = [], directions = [], asset_pairs = [], order_vol_denoms = [], btc_volumes = [] , order_prices=[]):
self.nodes = init_node # list of Asset objects
self.links = links # list of numbers that link each node
self.directions = directions #buy or sell
self.asset_pairs = asset_pairs #
self.order_vol_denoms = order_vol_denoms
self.btc_volumes = btc_volumes
self.order_prices = order_prices
self.value = self.compute_val() # product of links
self.length = len(self.nodes)
def compute_val(self):
product = 1
for l in self.links:
product*=l
return product
def is_complete(self):
if self.nodes[0]==self.nodes[-1]:
return True
else:
return False
def print_path(self):
node_names = []
for node in self.nodes:
node_names.append(node.name)
# est_fee = .9974**len(self.links)
if self.value > .995:
# print("Nodes= %s \nPercent= %.6f \nDirections= %s \nPairs= %s \nLinks= %s \nVolC= %s \n\n\n" %((node_names), self.value*100, self.directions, self.asset_pairs, self.links, self.order_vol_denoms))
print("Nodes: %s Percent: %.6f. BTC vol: %s" %(node_names,self.value*100, self.btc_volumes))
if False:
with open('log_file3.csv','a') as w:
string=''
for i in range(0,len(node_names)-1):
string += node_names[i] + ','
string+='%.6f,%s \n'%(self.value*100.0, self.btc_volumes)
w.write(string)
# def __append__():
# check left, check right
# update value
class Asset():
'''
This is the main asset type that the tree system will use.
It does not matter which exchange this asset came from, as long as the price information is correct.
'''
def __init__(self, kraken_data, name):
self.name = name # String of currency name
self.avail_trades = [] # Currencies this coin can be converted to
self.conversions = [] # Conversion ratios for each of the available trades (how much you will have of the new currency after trading)
self.pair_names = [] # Names of the currency pair you will actually trade on the exchange
self.exchange = 'Bittrex'
# Terms for actually placing the order
self.order_actions = [] # 'BUY' or 'SELL'
self.btc_volumes = [] # Volume in terms of bitcoin. (for a digital to fiat trade, this is # digital coins)
self.order_vol_denoms = [] # Denominations of the order_volumes (for a digital to fiat trade, this is name of digital coins)
self.order_prices = [] # Price in terms of the website (for a digital to fiat trade, this is price in fiat currency)
self.order_price_denoms = [] # Denominations of the order_prices (for a digital to fiat trade, this is name of fiat currency)
# Find the trades involving "name"
for i in range(kraken_data['N']): # Loop over the list of trades
if kraken_data['haves'][i] == name: # If the base is "name"
self.avail_trades.append( kraken_data['wants' ][i])
self.conversions.append( kraken_data['conversions' ][i])
self.pair_names.append( kraken_data['pair_names' ][i])
# self.max_amounts.append( kraken_data['max_amounts' ][i])
self.order_actions.append( kraken_data['order_actions' ][i])
self.btc_volumes.append( kraken_data['btc_volumes' ][i])
self.order_vol_denoms.append( kraken_data['order_vol_denoms' ][i])
self.order_prices.append( kraken_data['order_prices' ][i])
self.order_price_denoms.append(kraken_data['order_price_denoms'][i])
def get_prices(B):
'''
Get prices from Bittrex API
Inputs: Bittrex API Object, B.
asset_pairs_dict = asset pairs from Bittrex.
'''
# Call the Bittrex API to get all the asks and bids
t0 = datetime.datetime.now()
price_dict = B.getmarketsummaries()
t1 = datetime.datetime.now()
print("Market Summary Response Time: %.4f" %((t1-t0).total_seconds()))
# Loop over the asset pairs to get the pair, base, and quote strings
pairs = []
haves = []
wants = []
asks = []
bids = []
btc_volumes = []
N = 0 # Number of pairs
# We need the following conversions before we start:
USDtoBTC = 0
ETHtoBTC = 0
# Go through the asset pair dictionary and find the conversions from USD to BTC and ETH to BTC
# This will be used when getting each markets volume in BTC.
for market in price_dict:
# pprint.pprint(market['MarketName'])
if market['MarketName']=='USDT-BTC':
USDtoBTC = 1.0/market['Ask']
if market['MarketName']=='BTC-ETH':
ETHtoBTC = 1.0/market['Ask']
for market in price_dict:
# protect against shit coin names that cannot be turned into variables
if '1ST' in market['MarketName'] or '2GIVE' in market['MarketName']:
continue
# protect against coins that are currently inactive...
if market['Ask']==0.0 or market['Bid']==0.0:
# print('fuck: ',market['MarketName'])
continue
pairs.append(market['MarketName'])
#base is always first, market second.
assets = market['MarketName'].split('-')
base_currency = assets[0]
market_currency = assets[1]
haves.append(base_currency)
wants.append(market_currency)
asks.append(market['Ask'])
bids.append(market['Bid'])
N+=1
# Volume estimate should always be in BTC.
temp_vol = 'SHIT'
if base_currency=='BTC':
temp_vol = market['BaseVolume']
elif market=='BTC':
temp_vol = market['Volume']
elif base_currency=='USDT':
temp_vol = market['BaseVolume']*USDtoBTC
elif base_currency=='ETH':
temp_vol = market['BaseVolume']*ETHtoBTC
else:
print("Shits Whack")
btc_volumes.append(temp_vol)
# Volume denomination is the want (Market) in Bittrex, and stays the same regardless of trade direction
order_vol_denoms = deepcopy(wants)
order_vol_denoms.extend(deepcopy(wants))
# Price denomination is the have (Base) in Bittrex, and stays the same regardless of trade direction
order_price_denoms = deepcopy(haves)
order_price_denoms.extend(deepcopy(haves))
# BTC Volume estimates: Extend it to be the correct length.
btc_volumes.extend(deepcopy(btc_volumes))
# Now we have lists of one-way exchanges
# Complete the exchange lists by adding the other direction
# Append the "wants" list to the end of the "haves" list, and vice versa
new_wants = deepcopy(haves)
new_haves = deepcopy(wants)
wants.extend(new_wants)
haves.extend(new_haves)
# Create the actions (buy/sell) list
# Going from a have to a want is a BUY in the Bittrex nomenclature
# Example: Asset pair BTC-ETH, if you have BTC and want ETH, you are BUYing ETH with BTC
# if you have ETH and want BTC, you are SELLing ETH for BTC
order_actions = ['BUY' for ask in asks]
order_actions.extend(['SELL' for bid in bids])
# Create a conversions list
# From haves to wants you BUY & convert with 1/ask and the taker fee (0.25% for Bittrex)
# From wants to haves you SELL & convert with the bid and the maker fee (0.25% for Bittrex)
# Example: Asset pair XXBTZUSD, base=have XBT, quote=want USD, conversion: 1 XBT -> 5000 USD, "selling" XBT for USD, so you are the "maker"
# conversions = [bid * (1. - 0.0025) for bid in bids]
# conversions.extend([(1. - 0.0025)/ask for ask in asks])
# Start with the BUYs
conversions = [(1. - 0.0025)/ask for ask in asks]
conversions.extend([bid * (1. - 0.0025) for bid in bids])
# Create ask/bid price and volumes list
# Use the same logic as the conversion list, start with list of asks, then extend by list of bids
# start with the buys.
order_prices = deepcopy(asks)
order_prices.extend(deepcopy(bids))
# order_volumes = deepcopy(bid_vols).extend(deepcopy(ask_vols))
# You will need the pairs to place the order later. Just repeat it
pairs.extend(deepcopy(pairs))
# Make the max_amounts vector, which will be in the denomination of the Asset
# Example: Think of XBT -> USD...
# max_amounts = deepcopy(bid_vols) # First half of vector is just the bid volume (e.g. 0.05 XBT)
# max_amounts.extend( [asks[i]*ask_vols[i] for i in range(N)] ) # Second half of the vector is ask*ask_vol
# # 6000 $/XBT * 0.05 XBT = $300
# Double the length
N = 2*N
# Compile into a dictionary and return
bittrex_data = {
'unique' : list(set(haves)), # Unique currencies you have
'pair_names' : pairs,
'haves' : haves,
'wants' : wants,
'conversions' : conversions,
# 'max_amounts' : max_amounts,
'order_actions' : order_actions,
'btc_volumes' : btc_volumes,
'order_vol_denoms' : order_vol_denoms,
'order_prices' : order_prices,
'order_price_denoms': order_price_denoms,
'N' : N
}
return bittrex_data
def executePath(path, amount_in_BTC , min_value = 1.0015 , overshoot_frac = .25, max_overshoot = .0005 ,debug = False):
'''
overshoot frac: fraction of the gains that we are willing to lose to make trade go through quickly.
ex: path.value=1.1, making 10% on trade, if overshoot=.25, only making 7.5% of the trade. Rest goes to bid/ask padding
max_overshoot: maximum fraction you are willing to overshoot.
'''
#If the value of this path is lower than the min_percentage, return.
if not debug:
if path.value<min_value:
print('DEBUG: Do not execute')
return False
if min_value-max_overshoot<1.00001:
return False
if path.nodes[0].name!='BTC':
return False
#This is a number, not a percentage.
overshoot = min( overshoot_frac*(path.value-1), max_overshoot )
#Get hte index of the trade that we need to overshoot.
overshoots = [overshoot*btc_vol/sum(path.btc_volumes) for btc_vol in path.btc_volumes]
limit_prices = []
amounts_in_market = []
# As we go around the loop, track the amount of currency we need to input.
# start with BTC, or other input
amount_in_current_asset = amount_in_BTC
#Loop over the links between nodes.
for i in range(path.length-1):
# if it is a buy, pay more than the ask by the overshoot.
# buy the amount in the asset
if path.directions[i]=='BUY':
limit_prices.append( path.order_prices[i] * (1+overshoots[i]) )
amounts_in_market.append(amount_in_current_asset*path.links[i])
# if it is a sell, sell for less than the bid by the overshoot.
# sell the amount of your current asset
elif path.directions[i]=='SELL':
limit_prices.append( path.order_prices[i] * (1-overshoots[i]) )
amounts_in_market.append(amount_in_current_asset)
else:
return False
#Step the amount in current asset to the next index
amount_in_current_asset*=path.links[i]
#TODO: check balance of BTC
print(amounts_in_market)
print(limit_prices)
uuids = []
# # Loop through again and execute some trades yo
# for i in range(path.length-1):
# if path.directions[i]=='BUY':
# print("Buying %s. Amount: %s. Price; %s" %( path.asset_pairs[i], amounts_in_market[i], limit_prices[i]) )
# resp = B.buylimit( path.asset_pairs[i], amounts_in_market[i], limit_prices[i])
# pprint.pprint(resp)
# uuid = None
# if 'uuid' in resp:
# uuid = resp['uuid']
# else:
# print('No UUID. ENJOY YOUR SHITCOIN')
# return False
# elif path.directions[i]=='SELL':
# print("Buying %s. Amount: %s. Price; %s" %( path.asset_pairs[i], amounts_in_market[i], limit_prices[i]) )
# resp = B.selllimit( path.asset_pairs[i], amounts_in_market[i], limit_prices[i])
# pprint.pprint(resp)
# uuid = None
# if 'uuid' in resp:
# uuid = resp['uuid']
# else:
# print('No UUID. ENJOY YOUR SHITCOIN')
# return False
# uuids.append(uuid)
# if uuid=='INSUFFICIENT_FUNDS':
if __name__ == '__main__':
B = BittrexAPI('bittrex_key.key') # Start the API
count = 0
while True:
exchange_data = get_prices(B)
# Loop over the assets and make a list
asset_list = []
for asset in exchange_data['unique']:
# Make an instance of the Asset class with that asset's name
exec(asset + '= Asset(exchange_data,\'' + asset + '\')')
# Append the Asset instance to the list
asset_list.append(eval(asset))
trial_list = []
for asset in asset_list:
if asset.name in ['BTC']:
trial_list.append(asset)
# trial_list = asset_list
master_path = PathList(asset_list,trial_list)
# print("god help us")
t0 = datetime.datetime.now()
N = 5
for i in range(0,N):
if i<N-1:
master_path.step()
else:
master_path.step(final_step=True)
t1 = datetime.datetime.now()
print("Path Build Time: %.4f" %((t1-t0).total_seconds()))
master_path.show_paths()
best_path = master_path.return_best()
executePath(best_path,1.0, debug=True)
count+=1