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