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