3 # Put your poloniex api key in market.py
4 from market
import market
6 # FIXME: Améliorer le bid/ask
7 # FIXME: J'essayais d'utiliser plus de bitcoins que j'en avais à disposition
8 # FIXME: better compute moves to avoid rounding errors
10 def static_var(varname
, value
):
12 setattr(func
, varname
, value
)
17 URL
= "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
22 def repartition_pertenthousand(cls
, liquidity
="medium"):
23 cls
.parse_cryptoportfolio()
24 liquidities
= cls
.liquidities
[liquidity
]
25 last_date
= sorted(liquidities
.keys())[-1]
26 return liquidities
[last_date
]
29 def get_cryptoportfolio(cls
):
32 urllib3
.disable_warnings()
33 http
= urllib3
.PoolManager()
35 r
= http
.request("GET", cls
.URL
)
36 cls
.data
= json
.loads(r
.data
)
39 def parse_cryptoportfolio(cls
):
41 cls
.get_cryptoportfolio()
43 def filter_weights(weight_hash
):
44 if weight_hash
[1] == 0:
46 if weight_hash
[0] == "_row":
51 def clean_weights_(h
):
52 if type(h
[1][i
]) == str:
53 return [h
[0], h
[1][i
]]
55 return [h
[0], int(h
[1][i
] * 10000)]
58 def parse_weights(portfolio_hash
):
59 # FIXME: we'll need shorts at some point
60 assert all(map(lambda x
: x
== "long", portfolio_hash
["holding"]["direction"]))
61 weights_hash
= portfolio_hash
["weights"]
63 for i
in range(len(weights_hash
["_row"])):
64 weights
[weights_hash
["_row"][i
]] = dict(filter(
66 map(clean_weights(i
), weights_hash
.items())))
69 high_liquidity
= parse_weights(cls
.data
["portfolio_1"])
70 medium_liquidity
= parse_weights(cls
.data
["portfolio_2"])
73 "medium": medium_liquidity
,
74 "high": high_liquidity
,
80 def __init__(self
, currency
, value
, int_val
=None, linked_to
=None, ticker
=None):
81 self
.currency
= currency
83 self
._value
= int(value
* 10**self
.MAX_DIGITS
)
86 self
.linked_to
= linked_to
89 self
.ticker_cache
= {}
90 self
.ticker_cache_timestamp
= time
.time()
94 return self
._value
/ 10 ** self
.MAX_DIGITS
96 def in_currency(self
, other_currency
, market
, action
="average"):
97 if other_currency
== self
.currency
:
99 asset_ticker
= self
.get_ticker(other_currency
, market
)
100 if asset_ticker
is not None:
104 int_val
=int(self
._value
* asset_ticker
[action
]),
108 raise Exception("This asset is not available in the chosen market")
110 def get_ticker(self
, c2
, market
, refresh
=False):
116 "average": (float(1/ticker
["bid"]) + float(1/ticker
["ask"]) ) / 2,
117 "notInverted": ticker
,
119 def augment_ticker(ticker
):
122 "average": (ticker
["bid"] + ticker
["ask"] ) / 2,
125 if time
.time() - self
.ticker_cache_timestamp
> 5:
126 self
.ticker_cache
= {}
127 self
.ticker_cache_timestamp
= time
.time()
129 if (c1
, c2
, market
.__class
__) in self
.ticker_cache
:
130 return self
.ticker_cache
[(c1
, c2
, market
.__class
__)]
131 if (c2
, c1
, market
.__class
__) in self
.ticker_cache
:
132 return invert(self
.ticker_cache
[(c2
, c1
, market
.__class
__)])
135 self
.ticker_cache
[(c1
, c2
, market
.__class
__)] = market
.fetch_ticker("{}/{}".format(c1
, c2
))
136 augment_ticker(self
.ticker_cache
[(c1
, c2
, market
.__class
__)])
137 except ccxt
.ExchangeError
:
139 self
.ticker_cache
[(c2
, c1
, market
.__class
__)] = market
.fetch_ticker("{}/{}".format(c2
, c1
))
140 augment_ticker(self
.ticker_cache
[(c2
, c1
, market
.__class
__)])
141 except ccxt
.ExchangeError
:
142 self
.ticker_cache
[(c1
, c2
, market
.__class
__)] = None
143 return self
.get_ticker(c2
, market
)
146 return Amount(self
.currency
, 0, int_val
=abs(self
._value
))
148 def __add__(self
, other
):
149 if other
.currency
!= self
.currency
and other
._value
* self
._value
!= 0:
150 raise Exception("Summing amounts must be done with same currencies")
151 return Amount(self
.currency
, 0, int_val
=self
._value
+ other
._value
)
153 def __radd__(self
, other
):
157 return self
.__add
__(other
)
159 def __sub__(self
, other
):
160 if other
.currency
!= self
.currency
and other
._value
* self
._value
!= 0:
161 raise Exception("Summing amounts must be done with same currencies")
162 return Amount(self
.currency
, 0, int_val
=self
._value
- other
._value
)
167 def __mul__(self
, value
):
168 if type(value
) != int and type(value
) != float:
169 raise TypeError("Amount may only be multiplied by numbers")
170 return Amount(self
.currency
, 0, int_val
=(self
._value
* value
))
172 def __rmul__(self
, value
):
173 return self
.__mul
__(value
)
175 def __floordiv__(self
, value
):
176 if type(value
) != int:
177 raise TypeError("Amount may only be multiplied by integers")
178 return Amount(self
.currency
, 0, int_val
=(self
._value
// value
))
180 def __truediv__(self
, value
):
181 return self
.__floordiv
__(value
)
183 def __lt__(self
, other
):
184 if self
.currency
!= other
.currency
:
185 raise Exception("Comparing amounts must be done with same currencies")
186 return self
._value
< other
._value
188 def __eq__(self
, other
):
190 return self
._value
== 0
191 if self
.currency
!= other
.currency
:
192 raise Exception("Comparing amounts must be done with same currencies")
193 return self
._value
== other
._value
196 if self
.linked_to
is None:
197 return "{:.8f} {}".format(self
.value
, self
.currency
)
199 return "{:.8f} {} [{}]".format(self
.value
, self
.currency
, self
.linked_to
)
202 if self
.linked_to
is None:
203 return "Amount({:.8f} {})".format(self
.value
, self
.currency
)
205 return "Amount({:.8f} {} -> {})".format(self
.value
, self
.currency
, repr(self
.linked_to
))
211 def __init__(self
, currency
, total_value
, free_value
, used_value
):
212 self
.currency
= currency
213 self
.total
= Amount(currency
, total_value
)
214 self
.free
= Amount(currency
, free_value
)
215 self
.used
= Amount(currency
, used_value
)
218 def in_currency(cls
, other_currency
, market
, action
="average", type="total"):
220 for currency
in cls
.known_balances
:
221 balance
= cls
.known_balances
[currency
]
222 other_currency_amount
= getattr(balance
, type)\
223 .in_currency(other_currency
, market
, action
=action
)
224 amounts
[currency
] = other_currency_amount
229 return cls
.known_balances
.keys()
232 def from_hash(cls
, currency
, hash_
):
233 return cls(currency
, hash_
["total"], hash_
["free"], hash_
["used"])
236 def _fill_balances(cls
, hash_
):
238 if key
in ["info", "free", "used", "total"]:
240 if hash_
[key
]["total"] > 0:
241 cls
.known_balances
[key
] = cls
.from_hash(key
, hash_
[key
])
244 def fetch_balances(cls
, market
):
245 cls
._fill
_balances
(market
.fetch_balance())
246 return cls
.known_balances
249 def dispatch_assets(cls
, amount
):
250 repartition_pertenthousand
= Portfolio
.repartition_pertenthousand()
251 sum_pertenthousand
= sum([v
for k
, v
in repartition_pertenthousand
.items()])
253 for currency
, ptt
in repartition_pertenthousand
.items():
254 amounts
[currency
] = ptt
* amount
/ sum_pertenthousand
255 if currency
not in cls
.known_balances
:
256 cls
.known_balances
[currency
] = cls(currency
, 0, 0, 0)
260 def prepare_trades(cls
, market
, base_currency
="BTC"):
261 cls
.fetch_balances(market
)
262 values_in_base
= cls
.in_currency(base_currency
, market
)
263 total_base_value
= sum(values_in_base
.values())
264 new_repartition
= cls
.dispatch_assets(total_base_value
)
265 Trade
.compute_trades(values_in_base
, new_repartition
, market
=market
)
268 return int(self
.total
)
271 return "Balance({} [{}/{}/{}])".format(self
.currency
, str(self
.free
), str(self
.used
), str(self
.total
))
276 def __init__(self
, value_from
, value_to
, currency
, market
=None):
277 # We have value_from of currency, and want to finish with value_to of
278 # that currency. value_* may not be in currency's terms
279 self
.currency
= currency
280 self
.value_from
= value_from
281 self
.value_to
= value_to
283 assert self
.value_from
.currency
== self
.value_to
.currency
284 assert self
.value_from
.linked_to
is not None and self
.value_from
.linked_to
.currency
== self
.currency
285 self
.base_currency
= self
.value_from
.currency
287 if market
is not None:
288 self
.prepare_order(market
)
291 def compute_trades(cls
, values_in_base
, new_repartition
, market
=None):
292 base_currency
= sum(values_in_base
.values()).currency
293 for currency
in Balance
.currencies():
294 if currency
== base_currency
:
296 cls
.trades
[currency
] = cls(
297 values_in_base
.get(currency
, Amount(base_currency
, 0)),
298 new_repartition
.get(currency
, Amount(base_currency
, 0)),
306 if self
.value_from
== self
.value_to
:
308 if self
.base_currency
== self
.currency
:
311 if self
.value_from
< self
.value_to
:
316 def ticker_action(self
, inverted
):
317 if self
.value_from
< self
.value_to
:
318 return "ask" if not inverted
else "bid"
320 return "bid" if not inverted
else "ask"
322 def prepare_order(self
, market
):
323 if self
.action
is None:
325 ticker
= self
.value_from
.ticker
326 inverted
= ticker
["inverted"]
329 value_from
= self
.value_from
.linked_to
330 value_to
= self
.value_to
.in_currency(self
.currency
, market
)
331 delta
= abs(value_to
- value_from
)
332 currency
= self
.base_currency
334 ticker
= ticker
["notInverted"]
335 delta
= abs(self
.value_to
- self
.value_from
)
336 currency
= self
.currency
338 rate
= ticker
[self
.ticker_action(inverted
)]
340 self
.orders
.append(Order(self
.ticker_action(inverted
), delta
, rate
, currency
))
344 return sum(map(lambda v
: v
.orders
, cls
.trades
.values()), [])
347 def follow_orders(cls
, market
):
348 orders
= cls
.all_orders()
350 while len(orders
) != len(finished_orders
):
353 if order
in finished_orders
:
355 if order
.get_status(market
) != "open":
356 finished_orders
.append(order
)
357 print("finished {}".format(order
))
358 print("All orders finished")
361 return "Trade({} -> {} in {}, {})".format(
370 def __init__(self
, action
, amount
, rate
, base_currency
):
374 self
.base_currency
= base_currency
376 self
.status
= "not run"
379 return "Order({} {} at {} {} [{}])".format(
387 def run(self
, market
):
388 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
389 amount
= self
.amount
.value
392 print("market.create_order('{}', 'limit', '{}', {}, price={})".format(
393 symbol
, self
.action
, amount
, self
.rate
))
396 self
.result
= market
.create_order(symbol
, 'limit', self
.action
, amount
, price
=self
.rate
)
401 def get_status(self
, market
):
402 # other states are "closed" and "canceled"
403 if self
.status
== "open":
404 result
= market
.fetch_order(self
.result
['id'])
405 self
.status
= result
["status"]
408 @static_var("cache", {})
409 def fetch_fees(market
):
410 if market
.__class
__ not in fetch_fees
.cache
:
411 fetch_fees
.cache
[market
.__class
__] = market
.fetch_fees()
412 return fetch_fees
.cache
[market
.__class
__]
414 def print_orders(market
, base_currency
="BTC"):
415 Balance
.prepare_trades(market
, base_currency
=base_currency
)
416 for currency
, trade
in Trade
.trades
.items():
418 for order
in trade
.orders
:
419 print("\t", order
, sep
="")
421 def make_orders(market
, base_currency
="BTC"):
422 Balance
.prepare_trades(market
, base_currency
=base_currency
)
423 for currency
, trade
in Trade
.trades
.items():
425 for order
in trade
.orders
:
426 print("\t", order
, sep
="")
429 if __name__
== '__main__':