2 # Put your poloniex api key in market.py
3 from market
import market
5 def static_var(varname
, value
):
7 setattr(func
, varname
, value
)
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
),
19 repartition_pertenthousand
= {
28 def formatted_price(value
):
29 return round(value
/ 10**max_digits
, 8)
31 @static_var("cache", {})
32 def get_ticker(c1
, c2
, market
):
35 "bid": float(1/ticker
["ask"]),
36 "ask": float(1/ticker
["bid"]),
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
__)])
45 get_ticker
.cache
[(c1
, c2
, market
.__class
__)] = market
.fetch_ticker("{}/{}".format(c1
, c2
))
46 except ccxt
.ExchangeError
:
48 get_ticker
.cache
[(c2
, c1
, market
.__class
__)] = market
.fetch_ticker("{}/{}".format(c2
, c1
))
49 except ccxt
.ExchangeError
:
50 get_ticker
.cache
[(c1
, c2
, market
.__class
__)] = None
51 return get_ticker(c1
, c2
, market
)
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
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
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
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()])
76 new_repartition
= dispatch_assets(total_base_value
, repartition_pertenthousand
, market
, base_currency
=base_currency
)
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)
82 print("assets before repartition:")
83 for currency
, value
in current_assets
.items():
84 print("holding {} {}".format(
85 formatted_price(value
),
88 for currency
, value
in mouvements
.items():
89 if currency
== base_currency
:
91 asset_ticker
= get_ticker(currency
, base_currency
, market
)
94 currency_price
= int(value
/ asset_ticker
["bid"])
97 currency_price
= int(value
/ asset_ticker
["ask"])
99 print("need to {} {} {}'s worth of {}, i.e. {} {}".format(
101 formatted_price(abs(value
)),
104 formatted_price(abs(currency_price
)),
106 print("------------\nassets after repartition:")
107 for currency
, value
in new_repartition
.items():
108 print("holding {} {}".format(formatted_price(value
), currency
))
110 give_orders(current_assets
, repartition_pertenthousand
, market
)