]>
Commit | Line | Data |
---|---|---|
1 | import ccxt | |
2 | # Put your poloniex api key in market.py | |
3 | from market import market | |
4 | ||
5 | def static_var(varname, value): | |
6 | def decorate(func): | |
7 | setattr(func, varname, value) | |
8 | return func | |
9 | return decorate | |
10 | ||
11 | max_digits = 18 | |
12 | ||
13 | repartition_pertenthousand = { | |
14 | "BTC": 2857, | |
15 | "ZEC": 3701, | |
16 | "DOGE": 1805, | |
17 | "DGB": 1015, | |
18 | "SC": 623, | |
19 | } | |
20 | ||
21 | ||
22 | def formatted_price(value): | |
23 | return round(value / 10**max_digits, 8) | |
24 | ||
25 | @static_var("cache", {}) | |
26 | def get_ticker(c1, c2, market): | |
27 | def invert(ticker): | |
28 | return { | |
29 | "bid": float(1/ticker["ask"]), | |
30 | "ask": float(1/ticker["bid"]), | |
31 | } | |
32 | ||
33 | if (c1, c2, market.__class__) in get_ticker.cache: | |
34 | return get_ticker.cache[(c1, c2, market.__class__)] | |
35 | if (c2, c1, market.__class__) in get_ticker.cache: | |
36 | return invert(get_ticker.cache[(c2, c1, market.__class__)]) | |
37 | ||
38 | try: | |
39 | get_ticker.cache[(c1, c2, market.__class__)] = market.fetch_ticker("{}/{}".format(c1, c2)) | |
40 | except ccxt.ExchangeError: | |
41 | try: | |
42 | get_ticker.cache[(c2, c1, market.__class__)] = market.fetch_ticker("{}/{}".format(c2, c1)) | |
43 | except ccxt.ExchangeError: | |
44 | get_ticker.cache[(c1, c2, market.__class__)] = None | |
45 | return get_ticker(c1, c2, market) | |
46 | ||
47 | def fetch_balances(market): | |
48 | balances = {} | |
49 | fetched_balance = market.fetch_balance() | |
50 | for key, value in fetched_balance["total"].items(): | |
51 | if value > 0: | |
52 | balances[key] = int(value * 10**max_digits) | |
53 | return balances | |
54 | ||
55 | def assets_value(assets, market, base_currency="BTC"): | |
56 | repartition_in_base_currency = {} | |
57 | for currency, asset_value in assets.items(): | |
58 | if currency == base_currency: | |
59 | repartition_in_base_currency[currency] = asset_value | |
60 | else: | |
61 | asset_ticker = get_ticker(currency, base_currency, market) | |
62 | if asset_ticker is None: | |
63 | raise Exception("This asset is not available in the chosen market") | |
64 | repartition_in_base_currency[currency] = int(asset_ticker["bid"] * asset_value) | |
65 | return repartition_in_base_currency | |
66 | ||
67 | def dispatch_assets(base_currency_value, repartition_pertenthousand, market, base_currency="BTC"): | |
68 | sum_pertenthousand = sum([v for k, v in repartition_pertenthousand.items()]) | |
69 | repartition_in_base_currency = {} | |
70 | for currency, ptt in repartition_pertenthousand.items(): | |
71 | repartition_in_base_currency[currency] = int(ptt * base_currency_value / sum_pertenthousand) | |
72 | return repartition_in_base_currency | |
73 | ||
74 | def give_orders(current_assets, repartition_pertenthousand, market, base_currency="BTC"): | |
75 | value_in_base = assets_value(current_assets, market, base_currency=base_currency) | |
76 | total_base_value = sum([ v for k, v in value_in_base.items()]) | |
77 | ||
78 | new_repartition = dispatch_assets(total_base_value, repartition_pertenthousand, market, base_currency=base_currency) | |
79 | mouvements = {} | |
80 | ||
81 | for key in set(value_in_base.keys()).union(set(new_repartition.keys())): | |
82 | mouvements[key] = value_in_base.get(key, 0) - new_repartition.get(key, 0) | |
83 | ||
84 | print("assets before repartition:") | |
85 | for currency, value in current_assets.items(): | |
86 | print("holding {} {}".format( | |
87 | formatted_price(value), | |
88 | currency)) | |
89 | print("------------") | |
90 | for currency, value in mouvements.items(): | |
91 | if currency == base_currency: | |
92 | continue | |
93 | asset_ticker = get_ticker(currency, base_currency, market) | |
94 | if value > 0: | |
95 | action = "sell" | |
96 | currency_price = int(value / asset_ticker["bid"]) | |
97 | else: | |
98 | action = "buy" | |
99 | currency_price = int(value / asset_ticker["ask"]) | |
100 | if value != 0: | |
101 | print("need to {} {} {}'s worth of {}, i.e. {} {}".format( | |
102 | action, | |
103 | formatted_price(abs(value)), | |
104 | base_currency, | |
105 | currency, | |
106 | formatted_price(abs(currency_price)), | |
107 | currency)) | |
108 | print("------------\nassets after repartition:") | |
109 | for currency, value in new_repartition.items(): | |
110 | print("holding {} {}".format(formatted_price(value), currency)) | |
111 | ||
112 | current_assets = fetch_balances(market) | |
113 | give_orders(current_assets, repartition_pertenthousand, market) |