Created
April 10, 2018 00:41
-
-
Save thecodingrobot/b2fb0b9bab8e9b51f881561e91d01be8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
from math import log | |
from pprint import pprint | |
from bittrex import bittrex as btr | |
from bittrex.bittrex import Bittrex | |
import networkx as nx | |
from numpy import roll | |
def markets(data): | |
result = {} | |
for market in data['result']: | |
result[market['MarketName']] = market | |
return result | |
def graph(data): | |
g = nx.DiGraph() | |
for pair in data['result']: | |
market = pair['MarketName'] | |
u, v = market.split('-') | |
fee = .0025 | |
bid = float(pair['Bid']) | |
ask = float(pair['Ask']) | |
if ask == 0.0 or bid == 0.0: | |
continue | |
ask = ask + ask * fee | |
bid = 1 / (bid - bid * fee) | |
g.add_edge(u, v, weight=float(log(ask))) | |
g.add_edge(v, u, weight=float(log(bid))) | |
return g | |
def cycles(g): | |
weights = nx.get_edge_attributes(g, 'weight') | |
cy = nx.simple_cycles(g) | |
for cycle in cy: | |
cycle.append(cycle[0]) | |
sum_weights = sum([weights[(cycle[i], cycle[i + 1])] for i in range(len(cycle) - 1)]) | |
cycle = cycle[:-1] | |
if sum_weights < 0: | |
yield (cycle, sum_weights) | |
def main(): | |
bt = Bittrex(None, None, api_version=btr.API_V1_1) # or defaulting to v1.1 as Bittrex(None, None) | |
summaries = bt.get_market_summaries() | |
with open('dump.bin', 'wb') as f: | |
pickle.dump(summaries, f) | |
# with open('dump.bin', 'rb') as f: | |
# summaries = pickle.load(f) | |
g = graph(summaries) | |
m = markets(summaries) | |
def calc_profit(cycle, initial=1_000): | |
units = {'USDT': initial} | |
for i in range(len(cycle) - 1): | |
u, v = cycle[i], cycle[i + 1] | |
price = get_price(u, v) | |
units[v] = units[u] / price | |
units[u] = 0 | |
# print(u, v, '{} {}'.format(price, units[v])) | |
return units['USDT'] | |
def get_price(u, v): | |
try: | |
market = m[u + '-' + v] | |
ask = market['Ask'] | |
return ask + ask * .0025 | |
except KeyError as e: | |
market = m[v + '-' + u] | |
bid = market['Bid'] | |
return 1 / (bid - bid * .0025) | |
def print_stats(c): | |
for i in range(len(c) - 1): | |
u, v = c[i], c[i + 1] | |
try: | |
market = m[u + '-' + v] | |
except KeyError: | |
market = m[v + '-' + u] | |
pprint(market) | |
profit_count = 0 | |
best_trade = None | |
max_profit = 0 | |
for c, w in cycles(g): | |
if 'USDT' not in c or len(c) > 5: | |
continue | |
# start with USDT | |
c = roll(c, len(c) - c.index('USDT')).tolist() | |
c.append(c[0]) | |
initial = 3_000 | |
profit = calc_profit(c, initial) | |
if profit > max_profit: | |
max_profit = profit | |
best_trade = c | |
if profit > (initial * .01 + initial): | |
print('{}\t{:.2f}'.format(c, profit)) | |
profit_count += 1 | |
print('----') | |
if max_profit > 0: | |
pct_profit = ((max_profit - 3000) / 3000) * 100 | |
print('Best trade: {}. Profit: {:.2f}; {:.2f}%'.format(best_trade, max_profit, pct_profit)) | |
print_stats(best_trade) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment