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