2 from datetime
import datetime
, timedelta
3 from decimal
import Decimal
as D
, ROUND_DOWN
4 from json
import JSONDecodeError
5 from simplejson
.errors
import JSONDecodeError
as SimpleJSONDecodeError
6 from ccxt
import ExchangeError
, InsufficientFunds
, ExchangeNotAvailable
, InvalidOrder
, OrderNotCached
, OrderNotFound
7 from retry
import retry
10 # FIXME: correctly handle web call timeouts
13 URL
= "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
19 def wait_for_recent(cls
, market
, delta
=4):
20 cls
.repartition(market
, refetch
=True)
21 while cls
.last_date
is None or datetime
.now() - cls
.last_date
> timedelta(delta
):
23 market
.report
.print_log("Attempt to fetch up-to-date cryptoportfolio")
24 cls
.repartition(market
, refetch
=True)
27 def repartition(cls
, market
, liquidity
="medium", refetch
=False):
28 cls
.parse_cryptoportfolio(market
, refetch
=refetch
)
29 liquidities
= cls
.liquidities
[liquidity
]
30 return liquidities
[cls
.last_date
]
33 def get_cryptoportfolio(cls
, market
):
35 r
= requests
.get(cls
.URL
)
36 market
.report
.log_http_request(r
.request
.method
,
37 r
.request
.url
, r
.request
.body
, r
.request
.headers
, r
)
38 except Exception as e
:
39 market
.report
.log_error("get_cryptoportfolio", exception
=e
)
42 cls
.data
= r
.json(parse_int
=D
, parse_float
=D
)
43 except (JSONDecodeError
, SimpleJSONDecodeError
):
47 def parse_cryptoportfolio(cls
, market
, refetch
=False):
48 if refetch
or cls
.data
is None:
49 cls
.get_cryptoportfolio(market
)
51 def filter_weights(weight_hash
):
52 if weight_hash
[1][0] == 0:
54 if weight_hash
[0] == "_row":
59 def clean_weights_(h
):
60 if h
[0].endswith("s"):
61 return [h
[0][0:-1], (h
[1][i
], "short")]
63 return [h
[0], (h
[1][i
], "long")]
66 def parse_weights(portfolio_hash
):
67 weights_hash
= portfolio_hash
["weights"]
69 for i
in range(len(weights_hash
["_row"])):
70 date
= datetime
.strptime(weights_hash
["_row"][i
], "%Y-%m-%d")
71 weights
[date
] = dict(filter(
73 map(clean_weights(i
), weights_hash
.items())))
76 high_liquidity
= parse_weights(cls
.data
["portfolio_1"])
77 medium_liquidity
= parse_weights(cls
.data
["portfolio_2"])
80 "medium": medium_liquidity
,
81 "high": high_liquidity
,
83 cls
.last_date
= max(max(medium_liquidity
.keys()), max(high_liquidity
.keys()))
87 "default": lambda x
, y
: x
[y
],
88 "average": lambda x
, y
: x
["average"],
89 "bid": lambda x
, y
: x
["bid"],
90 "ask": lambda x
, y
: x
["ask"],
94 def compute_value(cls
, ticker
, action
, compute_value
="default"):
99 if isinstance(compute_value
, str):
100 compute_value
= cls
.computations
[compute_value
]
101 return compute_value(ticker
, action
)
104 def __init__(self
, currency
, value
, linked_to
=None, ticker
=None, rate
=None):
105 self
.currency
= currency
106 self
.value
= D(value
)
107 self
.linked_to
= linked_to
111 def in_currency(self
, other_currency
, market
, rate
=None, action
=None, compute_value
="average"):
112 if other_currency
== self
.currency
:
120 asset_ticker
= market
.get_ticker(self
.currency
, other_currency
)
121 if asset_ticker
is not None:
122 rate
= Computation
.compute_value(asset_ticker
, action
, compute_value
=compute_value
)
130 raise Exception("This asset is not available in the chosen market")
134 "currency": self
.currency
,
135 "value": round(self
).value
.normalize(),
138 def __round__(self
, n
=8):
139 return Amount(self
.currency
, self
.value
.quantize(D(1)/D(10**n
), rounding
=ROUND_DOWN
))
142 return Amount(self
.currency
, abs(self
.value
))
144 def __add__(self
, other
):
147 if other
.currency
!= self
.currency
and other
.value
* self
.value
!= 0:
148 raise Exception("Summing amounts must be done with same currencies")
149 return Amount(self
.currency
, self
.value
+ other
.value
)
151 def __radd__(self
, other
):
155 return self
.__add
__(other
)
157 def __sub__(self
, other
):
160 if other
.currency
!= self
.currency
and other
.value
* self
.value
!= 0:
161 raise Exception("Summing amounts must be done with same currencies")
162 return Amount(self
.currency
, self
.value
- other
.value
)
164 def __rsub__(self
, other
):
168 return -self
.__sub
__(other
)
170 def __mul__(self
, value
):
171 if not isinstance(value
, (int, float, D
)):
172 raise TypeError("Amount may only be multiplied by numbers")
173 return Amount(self
.currency
, self
.value
* value
)
175 def __rmul__(self
, value
):
176 return self
.__mul
__(value
)
178 def __floordiv__(self
, value
):
179 if not isinstance(value
, (int, float, D
)):
180 raise TypeError("Amount may only be divided by numbers")
181 return Amount(self
.currency
, self
.value
/ value
)
183 def __truediv__(self
, value
):
184 return self
.__floordiv
__(value
)
186 def __lt__(self
, other
):
188 return self
.value
< 0
189 if self
.currency
!= other
.currency
:
190 raise Exception("Comparing amounts must be done with same currencies")
191 return self
.value
< other
.value
193 def __le__(self
, other
):
194 return self
== other
or self
< other
196 def __gt__(self
, other
):
197 return not self
<= other
199 def __ge__(self
, other
):
200 return not self
< other
202 def __eq__(self
, other
):
204 return self
.value
== 0
205 if self
.currency
!= other
.currency
:
206 raise Exception("Comparing amounts must be done with same currencies")
207 return self
.value
== other
.value
209 def __ne__(self
, other
):
210 return not self
== other
213 return Amount(self
.currency
, - self
.value
)
216 if self
.linked_to
is None:
217 return "{:.8f} {}".format(self
.value
, self
.currency
)
219 return "{:.8f} {} [{}]".format(self
.value
, self
.currency
, self
.linked_to
)
222 if self
.linked_to
is None:
223 return "Amount({:.8f} {})".format(self
.value
, self
.currency
)
225 return "Amount({:.8f} {} -> {})".format(self
.value
, self
.currency
, repr(self
.linked_to
))
228 base_keys
= ["total", "exchange_total", "exchange_used",
229 "exchange_free", "margin_total", "margin_in_position",
230 "margin_available", "margin_borrowed", "margin_pending_gain"]
232 def __init__(self
, currency
, hash_
):
233 self
.currency
= currency
234 for key
in self
.base_keys
:
235 setattr(self
, key
, Amount(currency
, hash_
.get(key
, 0)))
237 self
.margin_position_type
= hash_
.get("margin_position_type")
239 if hash_
.get("margin_borrowed_base_currency") is not None:
240 base_currency
= hash_
["margin_borrowed_base_currency"]
242 "margin_liquidation_price",
243 "margin_lending_fees",
244 "margin_pending_base_gain",
245 "margin_borrowed_base_price"
247 setattr(self
, key
, Amount(base_currency
, hash_
.get(key
, 0)))
250 return dict(map(lambda x
: (x
, getattr(self
, x
).as_json()["value"]), self
.base_keys
))
253 if self
.exchange_total
> 0:
254 if self
.exchange_free
> 0 and self
.exchange_used
> 0:
255 exchange
= " Exch: [✔{} + ❌{} = {}]".format(str(self
.exchange_free
), str(self
.exchange_used
), str(self
.exchange_total
))
256 elif self
.exchange_free
> 0:
257 exchange
= " Exch: [✔{}]".format(str(self
.exchange_free
))
259 exchange
= " Exch: [❌{}]".format(str(self
.exchange_used
))
263 if self
.margin_total
> 0:
264 if self
.margin_available
!= 0 and self
.margin_in_position
!= 0:
265 margin
= " Margin: [✔{} + ❌{} = {}]".format(str(self
.margin_available
), str(self
.margin_in_position
), str(self
.margin_total
))
266 elif self
.margin_available
!= 0:
267 margin
= " Margin: [✔{}]".format(str(self
.margin_available
))
269 margin
= " Margin: [❌{}]".format(str(self
.margin_in_position
))
270 elif self
.margin_total
< 0:
271 margin
= " Margin: [{} @@ {}/{}]".format(str(self
.margin_total
),
272 str(self
.margin_borrowed_base_price
),
273 str(self
.margin_lending_fees
))
277 if self
.margin_total
!= 0 and self
.exchange_total
!= 0:
278 total
= " Total: [{}]".format(str(self
.total
))
282 return "Balance({}".format(self
.currency
) + "".join([exchange
, margin
, total
]) + ")"
285 def __init__(self
, value_from
, value_to
, currency
, market
):
286 # We have value_from of currency, and want to finish with value_to of
287 # that currency. value_* may not be in currency's terms
288 self
.currency
= currency
289 self
.value_from
= value_from
290 self
.value_to
= value_to
295 assert self
.value_from
.value
* self
.value_to
.value
>= 0
296 assert self
.value_from
.currency
== self
.value_to
.currency
297 if self
.value_from
!= 0:
298 assert self
.value_from
.linked_to
is not None and self
.value_from
.linked_to
.currency
== self
.currency
299 elif self
.value_from
.linked_to
is None:
300 self
.value_from
.linked_to
= Amount(self
.currency
, 0)
301 self
.base_currency
= self
.value_from
.currency
305 return self
.value_to
- self
.value_from
309 if self
.value_from
== self
.value_to
:
311 if self
.base_currency
== self
.currency
:
314 if abs(self
.value_from
) < abs(self
.value_to
):
319 def order_action(self
):
320 if (self
.value_from
< self
.value_to
) != self
.inverted
:
326 def trade_type(self
):
327 if self
.value_from
+ self
.value_to
< 0:
334 return not (self
.is_fullfiled
or self
.closed
)
337 for order
in self
.orders
:
342 def is_fullfiled(self
):
343 return abs(self
.filled_amount(in_base_currency
=(not self
.inverted
))) >= abs(self
.delta
)
345 def filled_amount(self
, in_base_currency
=False):
347 for order
in self
.orders
:
348 filled_amount
+= order
.filled_amount(in_base_currency
=in_base_currency
)
351 def update_order(self
, order
, tick
):
353 0: ["waiting", None],
354 1: ["waiting", None],
355 2: ["adjusting", lambda x
, y
: (x
[y
] + x
["average"]) / 2],
356 3: ["waiting", None],
357 4: ["waiting", None],
358 5: ["adjusting", lambda x
, y
: (x
[y
]*2 + x
["average"]) / 3],
359 6: ["waiting", None],
360 7: ["market_fallback", "default"],
364 update
, compute_value
= actions
[tick
]
366 update
= "market_adjust"
367 compute_value
= "default"
372 if compute_value
is not None:
374 new_order
= self
.prepare_order(compute_value
=compute_value
)
378 self
.market
.report
.log_order(order
, tick
, update
=update
,
379 compute_value
=compute_value
, new_order
=new_order
)
381 if new_order
is not None:
383 self
.market
.report
.log_order(order
, tick
, new_order
=new_order
)
385 def prepare_order(self
, close_if_possible
=None, compute_value
="default"):
386 if self
.action
is None:
388 ticker
= self
.market
.get_ticker(self
.currency
, self
.base_currency
)
389 self
.inverted
= ticker
["inverted"]
391 ticker
= ticker
["original"]
392 rate
= Computation
.compute_value(ticker
, self
.order_action(), compute_value
=compute_value
)
394 # FIXME: Dust amount should be removed from there if they werent
395 # honored in other sales
396 delta_in_base
= abs(self
.delta
)
397 # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case)
399 if not self
.inverted
:
400 base_currency
= self
.base_currency
402 if self
.action
== "dispose":
403 filled
= self
.filled_amount(in_base_currency
=False)
404 delta
= delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
405 # I have 10 BTC worth of FOO, and I want to sell 9 BTC
406 # worth of it, computed first with rate 10 FOO = 1 BTC.
407 # -> I "sell" "90" FOO at proposed rate "rate".
409 delta
= delta
- filled
410 # I already sold 60 FOO, 30 left
412 filled
= self
.filled_amount(in_base_currency
=True)
413 delta
= (delta_in_base
- filled
).in_currency(self
.currency
, self
.market
, rate
=1/rate
)
414 # I want to buy 9 BTC worth of FOO, computed with rate
416 # -> I "buy" "9 / rate" FOO at proposed rate "rate"
418 # I already bought 3 / rate FOO, 6 / rate left
420 base_currency
= self
.currency
422 if self
.action
== "dispose":
423 filled
= self
.filled_amount(in_base_currency
=True)
426 delta
= (delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
427 - filled
).in_currency(self
.base_currency
, self
.market
, rate
=1/rate
)
428 # I have 10 BTC worth of FOO, and I want to sell 9 BTC worth of it
429 # computed at rate 1 Foo = 0.01 BTC
430 # Computation says I should sell it at 125 FOO / BTC
431 # -> delta_in_base = 9 BTC
432 # -> delta = (9 * 1/0.01 FOO) * 1/125 = 7.2 BTC
433 # Action: "buy" "7.2 BTC" at rate "125" "FOO" on market
435 # I already bought 300/125 BTC, only 600/125 left
437 filled
= self
.filled_amount(in_base_currency
=False)
440 delta
= delta_in_base
441 # I have 1 BTC worth of FOO, and I want to buy 9 BTC worth of it
442 # At rate 100 Foo / BTC
443 # Computation says I should buy it at 125 FOO / BTC
444 # -> delta_in_base = 9 BTC
445 # Action: "sell" "9 BTC" at rate "125" "FOO" on market
447 delta
= delta
- filled
448 # I already sold 4 BTC, only 5 left
450 if close_if_possible
is None:
451 close_if_possible
= (self
.value_to
== 0)
454 self
.market
.report
.log_error("prepare_order", message
="Less to do than already filled: {}".format(delta
))
457 order
= Order(self
.order_action(),
458 delta
, rate
, base_currency
, self
.trade_type
,
459 self
.market
, self
, close_if_possible
=close_if_possible
)
460 self
.orders
.append(order
)
465 "action": self
.action
,
466 "from": self
.value_from
.as_json()["value"],
467 "to": self
.value_to
.as_json()["value"],
468 "currency": self
.currency
,
469 "base_currency": self
.base_currency
,
473 if self
.closed
and not self
.is_fullfiled
:
475 elif self
.is_fullfiled
:
480 return "Trade({} -> {} in {}, {}{})".format(
487 def print_with_order(self
, ind
=""):
488 self
.market
.report
.print_log("{}{}".format(ind
, self
))
489 for order
in self
.orders
:
490 self
.market
.report
.print_log("{}\t{}".format(ind
, order
))
491 for mouvement
in order
.mouvements
:
492 self
.market
.report
.print_log("{}\t\t{}".format(ind
, mouvement
))
495 def __init__(self
, action
, amount
, rate
, base_currency
, trade_type
, market
,
496 trade
, close_if_possible
=False):
500 self
.base_currency
= base_currency
502 self
.trade_type
= trade_type
505 self
.status
= "pending"
507 self
.close_if_possible
= close_if_possible
513 "action": self
.action
,
514 "trade_type": self
.trade_type
,
515 "amount": self
.amount
.as_json()["value"],
516 "currency": self
.amount
.as_json()["currency"],
517 "base_currency": self
.base_currency
,
519 "status": self
.status
,
520 "close_if_possible": self
.close_if_possible
,
522 "mouvements": list(map(lambda x
: x
.as_json(), self
.mouvements
))
526 return "Order({} {} {} at {} {} [{}]{})".format(
533 " ✂" if self
.close_if_possible
else "",
538 if self
.trade_type
== "long":
545 return self
.status
== "open"
549 return self
.status
== "pending"
553 return self
.status
.startswith("closed") or self
.status
== "canceled" or self
.status
== "error"
555 @retry(InsufficientFunds
)
558 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
559 amount
= round(self
.amount
, self
.market
.ccxt
.order_precision(symbol
)).value
561 if self
.market
.debug
:
562 self
.market
.report
.log_debug_action("market.ccxt.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(
563 symbol
, self
.action
, amount
, self
.rate
, self
.account
))
564 self
.results
.append({"debug": True, "id": -1}
)
566 action
= "market.ccxt.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(symbol
, self
.action
, amount
, self
.rate
, self
.account
)
568 self
.results
.append(self
.market
.ccxt
.create_order(symbol
, 'limit', self
.action
, amount
, price
=self
.rate
, account
=self
.account
))
570 # Impossible to honor the order (dust amount)
571 self
.status
= "closed"
572 self
.mark_finished_order()
574 except InsufficientFunds
as e
:
576 self
.market
.report
.log_error(action
, message
="Retrying with reduced amount", exception
=e
)
577 self
.amount
= self
.amount
* D("0.99")
580 self
.market
.report
.log_error(action
, message
="Giving up {}".format(self
), exception
=e
)
581 self
.status
= "error"
583 except Exception as e
:
584 self
.status
= "error"
585 self
.market
.report
.log_error(action
, exception
=e
)
587 self
.id = self
.results
[0]["id"]
590 def get_status(self
):
591 if self
.market
.debug
:
592 self
.market
.report
.log_debug_action("Getting {} status".format(self
))
594 # other states are "closed" and "canceled"
595 if not self
.finished
:
599 def mark_finished_order(self
):
600 if self
.status
.startswith("closed") and self
.market
.debug
:
601 self
.market
.report
.log_debug_action("Mark {} as finished".format(self
))
603 if self
.status
.startswith("closed"):
604 if self
.trade_type
== "short" and self
.action
== "buy" and self
.close_if_possible
:
605 self
.market
.ccxt
.close_margin_position(self
.amount
.currency
, self
.base_currency
)
608 if self
.market
.debug
:
609 self
.market
.report
.log_debug_action("Fetching {}".format(self
))
612 result
= self
.market
.ccxt
.fetch_order(self
.id)
613 self
.results
.append(result
)
614 self
.status
= result
["status"]
615 # Time at which the order started
616 self
.timestamp
= result
["datetime"]
617 except OrderNotCached
:
618 self
.status
= "closed_unknown"
620 self
.fetch_mouvements()
622 self
.mark_finished_order()
623 # FIXME: consider open order with dust remaining as closed
625 def dust_amount_remaining(self
):
626 return self
.remaining_amount() < Amount(self
.amount
.currency
, D("0.001"))
628 def remaining_amount(self
):
629 return self
.amount
- self
.filled_amount()
631 def filled_amount(self
, in_base_currency
=False):
632 if self
.status
== "open":
635 for mouvement
in self
.mouvements
:
637 filled_amount
+= mouvement
.total_in_base
639 filled_amount
+= mouvement
.total
642 def fetch_mouvements(self
):
644 mouvements
= self
.market
.ccxt
.privatePostReturnOrderTrades({"orderNumber": self.id}
)
645 except ExchangeError
:
649 for mouvement_hash
in mouvements
:
650 self
.mouvements
.append(Mouvement(self
.amount
.currency
,
651 self
.base_currency
, mouvement_hash
))
654 if self
.market
.debug
:
655 self
.market
.report
.log_debug_action("Mark {} as cancelled".format(self
))
656 self
.status
= "canceled"
658 if self
.open and self
.id is not None:
660 self
.market
.ccxt
.cancel_order(self
.id)
661 except OrderNotFound
as e
: # Closed inbetween
662 self
.market
.report
.log_error("cancel_order", message
="Already cancelled order", exception
=e
)
666 def __init__(self
, currency
, base_currency
, hash_
):
667 self
.currency
= currency
668 self
.base_currency
= base_currency
669 self
.id = hash_
.get("tradeID")
670 self
.action
= hash_
.get("type")
671 self
.fee_rate
= D(hash_
.get("fee", -1))
673 self
.date
= datetime
.strptime(hash_
.get("date", ""), '%Y-%m-%d %H:%M:%S')
676 self
.rate
= D(hash_
.get("rate", 0))
677 self
.total
= Amount(currency
, hash_
.get("amount", 0))
678 # rate * total = total_in_base
679 self
.total_in_base
= Amount(base_currency
, hash_
.get("total", 0))
683 "fee_rate": self
.fee_rate
,
685 "action": self
.action
,
686 "total": self
.total
.value
,
687 "currency": self
.currency
,
688 "total_in_base": self
.total_in_base
.value
,
689 "base_currency": self
.base_currency
693 if self
.fee_rate
> 0:
694 fee_rate
= " fee: {}%".format(self
.fee_rate
* 100)
697 if self
.date
is None:
701 return "Mouvement({} ; {} {} ({}){})".format(
702 date
, self
.action
, self
.total
, self
.total_in_base
,