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