]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blob - portfolio.py
Add print_balances helper
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / portfolio.py
1 import time
2 from datetime import datetime, timedelta
3 from decimal import Decimal as D, ROUND_DOWN
4 # Put your poloniex api key in market.py
5 from json import JSONDecodeError
6 from ccxt import ExchangeError, ExchangeNotAvailable
7 import requests
8 import helper as h
9 from store import *
10
11 # FIXME: correctly handle web call timeouts
12
13 class Portfolio:
14 URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
15 liquidities = {}
16 data = None
17 last_date = None
18
19 @classmethod
20 def wait_for_recent(cls, delta=4):
21 cls.repartition(refetch=True)
22 while cls.last_date is None or datetime.now() - cls.last_date > timedelta(delta):
23 time.sleep(30)
24 cls.repartition(refetch=True)
25
26 @classmethod
27 def repartition(cls, liquidity="medium", refetch=False):
28 cls.parse_cryptoportfolio(refetch=refetch)
29 liquidities = cls.liquidities[liquidity]
30 return liquidities[cls.last_date]
31
32 @classmethod
33 def get_cryptoportfolio(cls):
34 try:
35 r = requests.get(cls.URL)
36 except Exception:
37 return
38 try:
39 cls.data = r.json(parse_int=D, parse_float=D)
40 except JSONDecodeError:
41 cls.data = None
42
43 @classmethod
44 def parse_cryptoportfolio(cls, refetch=False):
45 if refetch or cls.data is None:
46 cls.get_cryptoportfolio()
47
48 def filter_weights(weight_hash):
49 if weight_hash[1][0] == 0:
50 return False
51 if weight_hash[0] == "_row":
52 return False
53 return True
54
55 def clean_weights(i):
56 def clean_weights_(h):
57 if h[0].endswith("s"):
58 return [h[0][0:-1], (h[1][i], "short")]
59 else:
60 return [h[0], (h[1][i], "long")]
61 return clean_weights_
62
63 def parse_weights(portfolio_hash):
64 weights_hash = portfolio_hash["weights"]
65 weights = {}
66 for i in range(len(weights_hash["_row"])):
67 date = datetime.strptime(weights_hash["_row"][i], "%Y-%m-%d")
68 weights[date] = dict(filter(
69 filter_weights,
70 map(clean_weights(i), weights_hash.items())))
71 return weights
72
73 high_liquidity = parse_weights(cls.data["portfolio_1"])
74 medium_liquidity = parse_weights(cls.data["portfolio_2"])
75
76 cls.liquidities = {
77 "medium": medium_liquidity,
78 "high": high_liquidity,
79 }
80 cls.last_date = max(max(medium_liquidity.keys()), max(high_liquidity.keys()))
81
82 class Computation:
83 computations = {
84 "default": lambda x, y: x[y],
85 "average": lambda x, y: x["average"],
86 "bid": lambda x, y: x["bid"],
87 "ask": lambda x, y: x["ask"],
88 }
89
90 @classmethod
91 def compute_value(cls, ticker, action, compute_value="default"):
92 if action == "buy":
93 action = "ask"
94 if action == "sell":
95 action = "bid"
96 if isinstance(compute_value, str):
97 compute_value = cls.computations[compute_value]
98 return compute_value(ticker, action)
99
100 class Amount:
101 def __init__(self, currency, value, linked_to=None, ticker=None, rate=None):
102 self.currency = currency
103 self.value = D(value)
104 self.linked_to = linked_to
105 self.ticker = ticker
106 self.rate = rate
107
108 def in_currency(self, other_currency, market, rate=None, action=None, compute_value="average"):
109 if other_currency == self.currency:
110 return self
111 if rate is not None:
112 return Amount(
113 other_currency,
114 self.value * rate,
115 linked_to=self,
116 rate=rate)
117 asset_ticker = h.get_ticker(self.currency, other_currency, market)
118 if asset_ticker is not None:
119 rate = Computation.compute_value(asset_ticker, action, compute_value=compute_value)
120 return Amount(
121 other_currency,
122 self.value * rate,
123 linked_to=self,
124 ticker=asset_ticker,
125 rate=rate)
126 else:
127 raise Exception("This asset is not available in the chosen market")
128
129 def __round__(self, n=8):
130 return Amount(self.currency, self.value.quantize(D(1)/D(10**n), rounding=ROUND_DOWN))
131
132 def __abs__(self):
133 return Amount(self.currency, abs(self.value))
134
135 def __add__(self, other):
136 if other == 0:
137 return self
138 if other.currency != self.currency and other.value * self.value != 0:
139 raise Exception("Summing amounts must be done with same currencies")
140 return Amount(self.currency, self.value + other.value)
141
142 def __radd__(self, other):
143 if other == 0:
144 return self
145 else:
146 return self.__add__(other)
147
148 def __sub__(self, other):
149 if other == 0:
150 return self
151 if other.currency != self.currency and other.value * self.value != 0:
152 raise Exception("Summing amounts must be done with same currencies")
153 return Amount(self.currency, self.value - other.value)
154
155 def __rsub__(self, other):
156 if other == 0:
157 return -self
158 else:
159 return -self.__sub__(other)
160
161 def __mul__(self, value):
162 if not isinstance(value, (int, float, D)):
163 raise TypeError("Amount may only be multiplied by numbers")
164 return Amount(self.currency, self.value * value)
165
166 def __rmul__(self, value):
167 return self.__mul__(value)
168
169 def __floordiv__(self, value):
170 if not isinstance(value, (int, float, D)):
171 raise TypeError("Amount may only be divided by numbers")
172 return Amount(self.currency, self.value / value)
173
174 def __truediv__(self, value):
175 return self.__floordiv__(value)
176
177 def __lt__(self, other):
178 if other == 0:
179 return self.value < 0
180 if self.currency != other.currency:
181 raise Exception("Comparing amounts must be done with same currencies")
182 return self.value < other.value
183
184 def __le__(self, other):
185 return self == other or self < other
186
187 def __gt__(self, other):
188 return not self <= other
189
190 def __ge__(self, other):
191 return not self < other
192
193 def __eq__(self, other):
194 if other == 0:
195 return self.value == 0
196 if self.currency != other.currency:
197 raise Exception("Comparing amounts must be done with same currencies")
198 return self.value == other.value
199
200 def __ne__(self, other):
201 return not self == other
202
203 def __neg__(self):
204 return Amount(self.currency, - self.value)
205
206 def __str__(self):
207 if self.linked_to is None:
208 return "{:.8f} {}".format(self.value, self.currency)
209 else:
210 return "{:.8f} {} [{}]".format(self.value, self.currency, self.linked_to)
211
212 def __repr__(self):
213 if self.linked_to is None:
214 return "Amount({:.8f} {})".format(self.value, self.currency)
215 else:
216 return "Amount({:.8f} {} -> {})".format(self.value, self.currency, repr(self.linked_to))
217
218 class Balance:
219
220 def __init__(self, currency, hash_):
221 self.currency = currency
222 for key in ["total",
223 "exchange_total", "exchange_used", "exchange_free",
224 "margin_total", "margin_borrowed", "margin_free"]:
225 setattr(self, key, Amount(currency, hash_.get(key, 0)))
226
227 self.margin_position_type = hash_.get("margin_position_type")
228
229 if hash_.get("margin_borrowed_base_currency") is not None:
230 base_currency = hash_["margin_borrowed_base_currency"]
231 for key in [
232 "margin_liquidation_price",
233 "margin_pending_gain",
234 "margin_lending_fees",
235 "margin_borrowed_base_price"
236 ]:
237 setattr(self, key, Amount(base_currency, hash_.get(key, 0)))
238
239 def __repr__(self):
240 if self.exchange_total > 0:
241 if self.exchange_free > 0 and self.exchange_used > 0:
242 exchange = " Exch: [✔{} + ❌{} = {}]".format(str(self.exchange_free), str(self.exchange_used), str(self.exchange_total))
243 elif self.exchange_free > 0:
244 exchange = " Exch: [✔{}]".format(str(self.exchange_free))
245 else:
246 exchange = " Exch: [❌{}]".format(str(self.exchange_used))
247 else:
248 exchange = ""
249
250 if self.margin_total > 0:
251 if self.margin_free != 0 and self.margin_borrowed != 0:
252 margin = " Margin: [✔{} + borrowed {} = {}]".format(str(self.margin_free), str(self.margin_borrowed), str(self.margin_total))
253 elif self.margin_free != 0:
254 margin = " Margin: [✔{}]".format(str(self.margin_free))
255 else:
256 margin = " Margin: [borrowed {}]".format(str(self.margin_borrowed))
257 elif self.margin_total < 0:
258 margin = " Margin: [{} @@ {}/{}]".format(str(self.margin_total),
259 str(self.margin_borrowed_base_price),
260 str(self.margin_lending_fees))
261 else:
262 margin = ""
263
264 if self.margin_total != 0 and self.exchange_total != 0:
265 total = " Total: [{}]".format(str(self.total))
266 else:
267 total = ""
268
269 return "Balance({}".format(self.currency) + "".join([exchange, margin, total]) + ")"
270
271 class Trade:
272 def __init__(self, value_from, value_to, currency, market=None):
273 # We have value_from of currency, and want to finish with value_to of
274 # that currency. value_* may not be in currency's terms
275 self.currency = currency
276 self.value_from = value_from
277 self.value_to = value_to
278 self.orders = []
279 self.market = market
280 assert self.value_from.currency == self.value_to.currency
281 if self.value_from != 0:
282 assert self.value_from.linked_to is not None and self.value_from.linked_to.currency == self.currency
283 elif self.value_from.linked_to is None:
284 self.value_from.linked_to = Amount(self.currency, 0)
285 self.base_currency = self.value_from.currency
286
287 @property
288 def action(self):
289 if self.value_from == self.value_to:
290 return None
291 if self.base_currency == self.currency:
292 return None
293
294 if abs(self.value_from) < abs(self.value_to):
295 return "acquire"
296 else:
297 return "dispose"
298
299 def order_action(self, inverted):
300 if (self.value_from < self.value_to) != inverted:
301 return "buy"
302 else:
303 return "sell"
304
305 @property
306 def trade_type(self):
307 if self.value_from + self.value_to < 0:
308 return "short"
309 else:
310 return "long"
311
312 def filled_amount(self, in_base_currency=False):
313 filled_amount = 0
314 for order in self.orders:
315 filled_amount += order.filled_amount(in_base_currency=in_base_currency)
316 return filled_amount
317
318 def update_order(self, order, tick):
319 new_order = None
320 if tick in [0, 1, 3, 4, 6]:
321 print("{}, tick {}, waiting".format(order, tick))
322 elif tick == 2:
323 new_order = self.prepare_order(compute_value=lambda x, y: (x[y] + x["average"]) / 2)
324 print("{}, tick {}, cancelling and adjusting to {}".format(order, tick, new_order))
325 elif tick ==5:
326 new_order = self.prepare_order(compute_value=lambda x, y: (x[y]*2 + x["average"]) / 3)
327 print("{}, tick {}, cancelling and adjusting to {}".format(order, tick, new_order))
328 elif tick >= 7:
329 if tick == 7:
330 print("{}, tick {}, fallbacking to market value".format(order, tick))
331 if (tick - 7) % 3 == 0:
332 new_order = self.prepare_order(compute_value="default")
333 print("{}, tick {}, market value, cancelling and adjusting to {}".format(order, tick, new_order))
334
335 if new_order is not None:
336 order.cancel()
337 new_order.run()
338
339 def prepare_order(self, compute_value="default"):
340 if self.action is None:
341 return None
342 ticker = h.get_ticker(self.currency, self.base_currency, self.market)
343 inverted = ticker["inverted"]
344 if inverted:
345 ticker = ticker["original"]
346 rate = Computation.compute_value(ticker, self.order_action(inverted), compute_value=compute_value)
347
348 # FIXME: Dust amount should be removed from there if they werent
349 # honored in other sales
350 delta_in_base = abs(self.value_from - self.value_to)
351 # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case)
352
353 if not inverted:
354 base_currency = self.base_currency
355 # BTC
356 if self.action == "dispose":
357 filled = self.filled_amount(in_base_currency=False)
358 delta = delta_in_base.in_currency(self.currency, self.market, rate=1/self.value_from.rate)
359 # I have 10 BTC worth of FOO, and I want to sell 9 BTC
360 # worth of it, computed first with rate 10 FOO = 1 BTC.
361 # -> I "sell" "90" FOO at proposed rate "rate".
362
363 delta = delta - filled
364 # I already sold 60 FOO, 30 left
365 else:
366 filled = self.filled_amount(in_base_currency=True)
367 delta = (delta_in_base - filled).in_currency(self.currency, self.market, rate=1/rate)
368 # I want to buy 9 BTC worth of FOO, computed with rate
369 # 10 FOO = 1 BTC
370 # -> I "buy" "9 / rate" FOO at proposed rate "rate"
371
372 # I already bought 3 / rate FOO, 6 / rate left
373 else:
374 base_currency = self.currency
375 # FOO
376 if self.action == "dispose":
377 filled = self.filled_amount(in_base_currency=True)
378 # Base is FOO
379
380 delta = (delta_in_base.in_currency(self.currency, self.market, rate=1/self.value_from.rate)
381 - filled).in_currency(self.base_currency, self.market, rate=1/rate)
382 # I have 10 BTC worth of FOO, and I want to sell 9 BTC worth of it
383 # computed at rate 1 Foo = 0.01 BTC
384 # Computation says I should sell it at 125 FOO / BTC
385 # -> delta_in_base = 9 BTC
386 # -> delta = (9 * 1/0.01 FOO) * 1/125 = 7.2 BTC
387 # Action: "buy" "7.2 BTC" at rate "125" "FOO" on market
388
389 # I already bought 300/125 BTC, only 600/125 left
390 else:
391 filled = self.filled_amount(in_base_currency=False)
392 # Base is FOO
393
394 delta = delta_in_base
395 # I have 1 BTC worth of FOO, and I want to buy 9 BTC worth of it
396 # At rate 100 Foo / BTC
397 # Computation says I should buy it at 125 FOO / BTC
398 # -> delta_in_base = 9 BTC
399 # Action: "sell" "9 BTC" at rate "125" "FOO" on market
400
401 delta = delta - filled
402 # I already sold 4 BTC, only 5 left
403
404 close_if_possible = (self.value_to == 0)
405
406 if delta <= 0:
407 print("Less to do than already filled: {}".format(delta))
408 return None
409
410 order = Order(self.order_action(inverted),
411 delta, rate, base_currency, self.trade_type,
412 self.market, self, close_if_possible=close_if_possible)
413 self.orders.append(order)
414 return order
415
416 def __repr__(self):
417 return "Trade({} -> {} in {}, {})".format(
418 self.value_from,
419 self.value_to,
420 self.currency,
421 self.action)
422
423 def print_with_order(self):
424 print(self)
425 for order in self.orders:
426 print("\t", order, sep="")
427
428 class Order:
429 def __init__(self, action, amount, rate, base_currency, trade_type, market,
430 trade, close_if_possible=False):
431 self.action = action
432 self.amount = amount
433 self.rate = rate
434 self.base_currency = base_currency
435 self.market = market
436 self.trade_type = trade_type
437 self.results = []
438 self.mouvements = []
439 self.status = "pending"
440 self.trade = trade
441 self.close_if_possible = close_if_possible
442 self.id = None
443 self.fetch_cache_timestamp = None
444
445 def __repr__(self):
446 return "Order({} {} {} at {} {} [{}]{})".format(
447 self.action,
448 self.trade_type,
449 self.amount,
450 self.rate,
451 self.base_currency,
452 self.status,
453 " ✂" if self.close_if_possible else "",
454 )
455
456 @property
457 def account(self):
458 if self.trade_type == "long":
459 return "exchange"
460 else:
461 return "margin"
462
463 @property
464 def open(self):
465 return self.status == "open"
466
467 @property
468 def pending(self):
469 return self.status == "pending"
470
471 @property
472 def finished(self):
473 return self.status == "closed" or self.status == "canceled" or self.status == "error"
474
475 def run(self):
476 symbol = "{}/{}".format(self.amount.currency, self.base_currency)
477 amount = round(self.amount, self.market.order_precision(symbol)).value
478
479 if TradeStore.debug:
480 print("market.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(
481 symbol, self.action, amount, self.rate, self.account))
482 self.results.append({"debug": True, "id": -1})
483 else:
484 try:
485 self.results.append(self.market.create_order(symbol, 'limit', self.action, amount, price=self.rate, account=self.account))
486 except ExchangeNotAvailable:
487 # Impossible to honor the order (dust amount)
488 self.status = "closed"
489 self.mark_finished_order()
490 return
491 except Exception as e:
492 self.status = "error"
493 print("error when running market.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(
494 symbol, self.action, amount, self.rate, self.account))
495 self.error_message = str("{}: {}".format(e.__class__.__name__, e))
496 print(self.error_message)
497 return
498 self.id = self.results[0]["id"]
499 self.status = "open"
500
501 def get_status(self):
502 if TradeStore.debug:
503 return self.status
504 # other states are "closed" and "canceled"
505 if not self.finished:
506 self.fetch()
507 if self.finished:
508 self.mark_finished_order()
509 return self.status
510
511 def mark_finished_order(self):
512 if TradeStore.debug:
513 return
514 if self.status == "closed":
515 if self.trade_type == "short" and self.action == "buy" and self.close_if_possible:
516 self.market.close_margin_position(self.amount.currency, self.base_currency)
517
518 def fetch(self, force=False):
519 if TradeStore.debug or (not force and self.fetch_cache_timestamp is not None
520 and time.time() - self.fetch_cache_timestamp < 10):
521 return
522 self.fetch_cache_timestamp = time.time()
523
524 result = self.market.fetch_order(self.id)
525 self.results.append(result)
526
527 self.status = result["status"]
528 # Time at which the order started
529 self.timestamp = result["datetime"]
530 self.fetch_mouvements()
531
532 # FIXME: consider open order with dust remaining as closed
533
534 def dust_amount_remaining(self):
535 return self.remaining_amount() < Amount(self.amount.currency, D("0.001"))
536
537 def remaining_amount(self):
538 if self.status == "open":
539 self.fetch()
540 return self.amount - self.filled_amount()
541
542 def filled_amount(self, in_base_currency=False):
543 if self.status == "open":
544 self.fetch()
545 filled_amount = 0
546 for mouvement in self.mouvements:
547 if in_base_currency:
548 filled_amount += mouvement.total_in_base
549 else:
550 filled_amount += mouvement.total
551 return filled_amount
552
553 def fetch_mouvements(self):
554 try:
555 mouvements = self.market.privatePostReturnOrderTrades({"orderNumber": self.id})
556 except ExchangeError:
557 mouvements = []
558 self.mouvements = []
559
560 for mouvement_hash in mouvements:
561 self.mouvements.append(Mouvement(self.amount.currency,
562 self.base_currency, mouvement_hash))
563
564 def cancel(self):
565 if TradeStore.debug:
566 self.status = "canceled"
567 return
568 self.market.cancel_order(self.id)
569 self.fetch()
570
571 class Mouvement:
572 def __init__(self, currency, base_currency, hash_):
573 self.currency = currency
574 self.base_currency = base_currency
575 self.id = hash_.get("tradeID")
576 self.action = hash_.get("type")
577 self.fee_rate = D(hash_.get("fee", -1))
578 try:
579 self.date = datetime.strptime(hash_.get("date", ""), '%Y-%m-%d %H:%M:%S')
580 except ValueError:
581 self.date = None
582 self.rate = D(hash_.get("rate", 0))
583 self.total = Amount(currency, hash_.get("amount", 0))
584 # rate * total = total_in_base
585 self.total_in_base = Amount(base_currency, hash_.get("total", 0))
586
587 if __name__ == '__main__': # pragma: no cover
588 from market import market
589 h.print_orders(market)