]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blob - portfolio.py
Add method to wait for portfolio update
[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 delta_in_base = abs(self.value_from - self.value_to)
349 # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case)
350
351 if not inverted:
352 base_currency = self.base_currency
353 # BTC
354 if self.action == "dispose":
355 filled = self.filled_amount(in_base_currency=False)
356 delta = delta_in_base.in_currency(self.currency, self.market, rate=1/self.value_from.rate)
357 # I have 10 BTC worth of FOO, and I want to sell 9 BTC
358 # worth of it, computed first with rate 10 FOO = 1 BTC.
359 # -> I "sell" "90" FOO at proposed rate "rate".
360
361 delta = delta - filled
362 # I already sold 60 FOO, 30 left
363 else:
364 filled = self.filled_amount(in_base_currency=True)
365 delta = (delta_in_base - filled).in_currency(self.currency, self.market, rate=1/rate)
366 # I want to buy 9 BTC worth of FOO, computed with rate
367 # 10 FOO = 1 BTC
368 # -> I "buy" "9 / rate" FOO at proposed rate "rate"
369
370 # I already bought 3 / rate FOO, 6 / rate left
371 else:
372 base_currency = self.currency
373 # FOO
374 if self.action == "dispose":
375 filled = self.filled_amount(in_base_currency=True)
376 # Base is FOO
377
378 delta = (delta_in_base.in_currency(self.currency, self.market, rate=1/self.value_from.rate)
379 - filled).in_currency(self.base_currency, self.market, rate=1/rate)
380 # I have 10 BTC worth of FOO, and I want to sell 9 BTC worth of it
381 # computed at rate 1 Foo = 0.01 BTC
382 # Computation says I should sell it at 125 FOO / BTC
383 # -> delta_in_base = 9 BTC
384 # -> delta = (9 * 1/0.01 FOO) * 1/125 = 7.2 BTC
385 # Action: "buy" "7.2 BTC" at rate "125" "FOO" on market
386
387 # I already bought 300/125 BTC, only 600/125 left
388 else:
389 filled = self.filled_amount(in_base_currency=False)
390 # Base is FOO
391
392 delta = delta_in_base
393 # I have 1 BTC worth of FOO, and I want to buy 9 BTC worth of it
394 # At rate 100 Foo / BTC
395 # Computation says I should buy it at 125 FOO / BTC
396 # -> delta_in_base = 9 BTC
397 # Action: "sell" "9 BTC" at rate "125" "FOO" on market
398
399 delta = delta - filled
400 # I already sold 4 BTC, only 5 left
401
402 close_if_possible = (self.value_to == 0)
403
404 if delta <= 0:
405 print("Less to do than already filled: {}".format(delta))
406 return None
407
408 order = Order(self.order_action(inverted),
409 delta, rate, base_currency, self.trade_type,
410 self.market, self, close_if_possible=close_if_possible)
411 self.orders.append(order)
412 return order
413
414 def __repr__(self):
415 return "Trade({} -> {} in {}, {})".format(
416 self.value_from,
417 self.value_to,
418 self.currency,
419 self.action)
420
421 def print_with_order(self):
422 print(self)
423 for order in self.orders:
424 print("\t", order, sep="")
425
426 class Order:
427 def __init__(self, action, amount, rate, base_currency, trade_type, market,
428 trade, close_if_possible=False):
429 self.action = action
430 self.amount = amount
431 self.rate = rate
432 self.base_currency = base_currency
433 self.market = market
434 self.trade_type = trade_type
435 self.results = []
436 self.mouvements = []
437 self.status = "pending"
438 self.trade = trade
439 self.close_if_possible = close_if_possible
440 self.id = None
441 self.fetch_cache_timestamp = None
442
443 def __repr__(self):
444 return "Order({} {} {} at {} {} [{}]{})".format(
445 self.action,
446 self.trade_type,
447 self.amount,
448 self.rate,
449 self.base_currency,
450 self.status,
451 " ✂" if self.close_if_possible else "",
452 )
453
454 @property
455 def account(self):
456 if self.trade_type == "long":
457 return "exchange"
458 else:
459 return "margin"
460
461 @property
462 def open(self):
463 return self.status == "open"
464
465 @property
466 def pending(self):
467 return self.status == "pending"
468
469 @property
470 def finished(self):
471 return self.status == "closed" or self.status == "canceled" or self.status == "error"
472
473 def run(self):
474 symbol = "{}/{}".format(self.amount.currency, self.base_currency)
475 amount = round(self.amount, self.market.order_precision(symbol)).value
476
477 if TradeStore.debug:
478 print("market.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(
479 symbol, self.action, amount, self.rate, self.account))
480 self.results.append({"debug": True, "id": -1})
481 else:
482 try:
483 self.results.append(self.market.create_order(symbol, 'limit', self.action, amount, price=self.rate, account=self.account))
484 except ExchangeNotAvailable:
485 # Impossible to honor the order (dust amount)
486 self.status = "closed"
487 self.mark_finished_order()
488 return
489 except Exception as e:
490 self.status = "error"
491 print("error when running market.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(
492 symbol, self.action, amount, self.rate, self.account))
493 self.error_message = str("{}: {}".format(e.__class__.__name__, e))
494 print(self.error_message)
495 return
496 self.id = self.results[0]["id"]
497 self.status = "open"
498
499 def get_status(self):
500 if TradeStore.debug:
501 return self.status
502 # other states are "closed" and "canceled"
503 if not self.finished:
504 self.fetch()
505 if self.finished:
506 self.mark_finished_order()
507 return self.status
508
509 def mark_finished_order(self):
510 if TradeStore.debug:
511 return
512 if self.status == "closed":
513 if self.trade_type == "short" and self.action == "buy" and self.close_if_possible:
514 self.market.close_margin_position(self.amount.currency, self.base_currency)
515
516 def fetch(self, force=False):
517 if TradeStore.debug or (not force and self.fetch_cache_timestamp is not None
518 and time.time() - self.fetch_cache_timestamp < 10):
519 return
520 self.fetch_cache_timestamp = time.time()
521
522 result = self.market.fetch_order(self.id)
523 self.results.append(result)
524
525 self.status = result["status"]
526 # Time at which the order started
527 self.timestamp = result["datetime"]
528 self.fetch_mouvements()
529
530 # FIXME: consider open order with dust remaining as closed
531
532 def dust_amount_remaining(self):
533 return self.remaining_amount() < Amount(self.amount.currency, D("0.001"))
534
535 def remaining_amount(self):
536 if self.status == "open":
537 self.fetch()
538 return self.amount - self.filled_amount()
539
540 def filled_amount(self, in_base_currency=False):
541 if self.status == "open":
542 self.fetch()
543 filled_amount = 0
544 for mouvement in self.mouvements:
545 if in_base_currency:
546 filled_amount += mouvement.total_in_base
547 else:
548 filled_amount += mouvement.total
549 return filled_amount
550
551 def fetch_mouvements(self):
552 try:
553 mouvements = self.market.privatePostReturnOrderTrades({"orderNumber": self.id})
554 except ExchangeError:
555 mouvements = []
556 self.mouvements = []
557
558 for mouvement_hash in mouvements:
559 self.mouvements.append(Mouvement(self.amount.currency,
560 self.base_currency, mouvement_hash))
561
562 def cancel(self):
563 if TradeStore.debug:
564 self.status = "canceled"
565 return
566 self.market.cancel_order(self.id)
567 self.fetch()
568
569 class Mouvement:
570 def __init__(self, currency, base_currency, hash_):
571 self.currency = currency
572 self.base_currency = base_currency
573 self.id = hash_.get("tradeID")
574 self.action = hash_.get("type")
575 self.fee_rate = D(hash_.get("fee", -1))
576 try:
577 self.date = datetime.strptime(hash_.get("date", ""), '%Y-%m-%d %H:%M:%S')
578 except ValueError:
579 self.date = None
580 self.rate = D(hash_.get("rate", 0))
581 self.total = Amount(currency, hash_.get("amount", 0))
582 # rate * total = total_in_base
583 self.total_in_base = Amount(base_currency, hash_.get("total", 0))
584
585 if __name__ == '__main__': # pragma: no cover
586 from market import market
587 h.print_orders(market)