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