3 def static_var(varname
, value
):
5 setattr(func
, varname
, value
)
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
),
17 repartition_pertenthousand
= {
25 market
= ccxt
.poloniex()
27 def formatted_price(value
):
28 return round(value
/ 10**max_digits
, 8)
30 @static_var("cache", {})
31 def get_ticker(c1
, c2
, market
):
34 "bid": float(1/ticker
["ask"]),
35 "ask": float(1/ticker
["bid"]),
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
__)])
44 get_ticker
.cache
[(c1
, c2
, market
.__class
__)] = market
.fetch_ticker("{}/{}".format(c1
, c2
))
45 except ccxt
.ExchangeError
:
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
)
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
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
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
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()])
75 new_repartition
= dispatch_assets(total_base_value
, repartition_pertenthousand
, market
, base_currency
=base_currency
)
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)
81 print("assets before repartition:")
82 for currency
, value
in current_assets
.items():
83 print("holding {} {}".format(
84 formatted_price(value
),
87 for currency
, value
in mouvements
.items():
88 if currency
== base_currency
:
90 asset_ticker
= get_ticker(currency
, base_currency
, market
)
93 currency_price
= int(value
/ asset_ticker
["bid"])
96 currency_price
= int(value
/ asset_ticker
["ask"])
98 print("need to {} {} {}'s worth of {}, i.e. {} {}".format(
100 formatted_price(abs(value
)),
103 formatted_price(abs(currency_price
)),
105 print("------------\nassets after repartition:")
106 for currency
, value
in new_repartition
.items():
107 print("holding {} {}".format(formatted_price(value
), currency
))
109 give_orders(current_assets
, repartition_pertenthousand
, market
)