5 "ETH": int(2.00000000 * 10**max_digits
),
6 "BTC": int(1.23000000 * 10**max_digits
),
7 "ZEC": int(2.00000000 * 10**max_digits
),
8 "SC" : int(2.000000000 * 10**max_digits
),
11 repartition_pertenthousand
= {
19 market
= ccxt
.poloniex()
21 def formatted_price(value
):
22 return round(value
/ 10**max_digits
, 8)
24 def get_ticker(c1
, c2
, market
):
26 return market
.fetch_ticker("{}/{}".format(c1
, c2
))
27 except ccxt
.ExchangeError
:
29 ticker
= market
.fetch_ticker("{}/{}".format(c2
, c1
))
31 "bid": float(1/ticker
["ask"]),
32 "ask": float(1/ticker
["bid"]),
34 except ccxt
.ExchangeError
:
37 def assets_value(assets
, market
, base_currency
="BTC"):
38 repartition_in_base_currency
= {}
39 for currency
, asset_value
in assets
.items():
40 if currency
== base_currency
:
41 repartition_in_base_currency
[currency
] = asset_value
43 asset_ticker
= get_ticker(currency
, base_currency
, market
)
44 if asset_ticker
is None:
45 raise Exception("This asset is not available in the chosen market")
46 repartition_in_base_currency
[currency
] = int(asset_ticker
["bid"] * asset_value
)
47 return repartition_in_base_currency
49 def dispatch_assets(base_currency_value
, repartition_pertenthousand
, market
, base_currency
="BTC"):
50 sum_pertenthousand
= sum([v
for k
, v
in repartition_pertenthousand
.items()])
51 repartition_in_base_currency
= {}
52 for currency
, ptt
in repartition_pertenthousand
.items():
53 repartition_in_base_currency
[currency
] = int(ptt
* base_currency_value
/ sum_pertenthousand
)
54 return repartition_in_base_currency
56 def give_orders(current_assets
, repartition_pertenthousand
, market
, base_currency
="BTC"):
57 value_in_base
= assets_value(current_assets
, market
, base_currency
=base_currency
)
58 total_base_value
= sum([ v
for k
, v
in value_in_base
.items()])
60 new_repartition
= dispatch_assets(total_base_value
, repartition_pertenthousand
, market
, base_currency
=base_currency
)
63 for key
in set(value_in_base
.keys()).union(set(new_repartition
.keys())):
64 mouvements
[key
] = value_in_base
.get(key
, 0) - new_repartition
.get(key
, 0)
66 print("assets before repartition:")
67 for currency
, value
in current_assets
.items():
68 print("holding {} {}".format(
69 formatted_price(value
),
72 for currency
, value
in mouvements
.items():
73 if currency
== base_currency
:
75 asset_ticker
= get_ticker(currency
, base_currency
, market
)
78 currency_price
= int(value
/ asset_ticker
["bid"])
81 currency_price
= int(value
/ asset_ticker
["ask"])
83 print("need to {} {} {}'s worth of {}, i.e. {} {}".format(
85 formatted_price(abs(value
)),
88 formatted_price(abs(currency_price
)),
90 print("------------\nassets after repartition:")
91 for currency
, value
in new_repartition
.items():
92 print("holding {} {}".format(formatted_price(value
), currency
))
94 give_orders(current_assets
, repartition_pertenthousand
, market
)