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
12 # FIXME: correctly handle web call timeouts
15 URL
= "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
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
):
25 cls
.repartition(refetch
=True)
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
]
34 def get_cryptoportfolio(cls
):
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
)
43 cls
.data
= r
.json(parse_int
=D
, parse_float
=D
)
44 except (JSONDecodeError
, SimpleJSONDecodeError
):
48 def parse_cryptoportfolio(cls
, refetch
=False):
49 if refetch
or cls
.data
is None:
50 cls
.get_cryptoportfolio()
52 def filter_weights(weight_hash
):
53 if weight_hash
[1][0] == 0:
55 if weight_hash
[0] == "_row":
60 def clean_weights_(h
):
61 if h
[0].endswith("s"):
62 return [h
[0][0:-1], (h
[1][i
], "short")]
64 return [h
[0], (h
[1][i
], "long")]
67 def parse_weights(portfolio_hash
):
68 weights_hash
= portfolio_hash
["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(
74 map(clean_weights(i
), weights_hash
.items())))
77 high_liquidity
= parse_weights(cls
.data
["portfolio_1"])
78 medium_liquidity
= parse_weights(cls
.data
["portfolio_2"])
81 "medium": medium_liquidity
,
82 "high": high_liquidity
,
84 cls
.last_date
= max(max(medium_liquidity
.keys()), max(high_liquidity
.keys()))
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"],
95 def compute_value(cls
, ticker
, action
, compute_value
="default"):
100 if isinstance(compute_value
, str):
101 compute_value
= cls
.computations
[compute_value
]
102 return compute_value(ticker
, action
)
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
112 def in_currency(self
, other_currency
, market
, rate
=None, action
=None, compute_value
="average"):
113 if other_currency
== self
.currency
:
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
)
131 raise Exception("This asset is not available in the chosen market")
135 "currency": self
.currency
,
136 "value": round(self
).value
.normalize(),
139 def __round__(self
, n
=8):
140 return Amount(self
.currency
, self
.value
.quantize(D(1)/D(10**n
), rounding
=ROUND_DOWN
))
143 return Amount(self
.currency
, abs(self
.value
))
145 def __add__(self
, other
):
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
)
152 def __radd__(self
, other
):
156 return self
.__add
__(other
)
158 def __sub__(self
, other
):
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
)
165 def __rsub__(self
, other
):
169 return -self
.__sub
__(other
)
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
)
176 def __rmul__(self
, value
):
177 return self
.__mul
__(value
)
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
)
184 def __truediv__(self
, value
):
185 return self
.__floordiv
__(value
)
187 def __lt__(self
, other
):
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
194 def __le__(self
, other
):
195 return self
== other
or self
< other
197 def __gt__(self
, other
):
198 return not self
<= other
200 def __ge__(self
, other
):
201 return not self
< other
203 def __eq__(self
, other
):
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
210 def __ne__(self
, other
):
211 return not self
== other
214 return Amount(self
.currency
, - self
.value
)
217 if self
.linked_to
is None:
218 return "{:.8f} {}".format(self
.value
, self
.currency
)
220 return "{:.8f} {} [{}]".format(self
.value
, self
.currency
, self
.linked_to
)
223 if self
.linked_to
is None:
224 return "Amount({:.8f} {})".format(self
.value
, self
.currency
)
226 return "Amount({:.8f} {} -> {})".format(self
.value
, self
.currency
, repr(self
.linked_to
))
229 base_keys
= ["total", "exchange_total", "exchange_used",
230 "exchange_free", "margin_total", "margin_borrowed",
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)))
238 self
.margin_position_type
= hash_
.get("margin_position_type")
240 if hash_
.get("margin_borrowed_base_currency") is not None:
241 base_currency
= hash_
["margin_borrowed_base_currency"]
243 "margin_liquidation_price",
244 "margin_pending_gain",
245 "margin_lending_fees",
246 "margin_borrowed_base_price"
248 setattr(self
, key
, Amount(base_currency
, hash_
.get(key
, 0)))
251 return dict(map(lambda x
: (x
, getattr(self
, x
).as_json()["value"]), self
.base_keys
))
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
))
260 exchange
= " Exch: [❌{}]".format(str(self
.exchange_used
))
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
))
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
))
278 if self
.margin_total
!= 0 and self
.exchange_total
!= 0:
279 total
= " Total: [{}]".format(str(self
.total
))
283 return "Balance({}".format(self
.currency
) + "".join([exchange
, margin
, total
]) + ")"
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
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
303 if self
.value_from
== self
.value_to
:
305 if self
.base_currency
== self
.currency
:
308 if abs(self
.value_from
) < abs(self
.value_to
):
313 def order_action(self
, inverted
):
314 if (self
.value_from
< self
.value_to
) != inverted
:
320 def trade_type(self
):
321 if self
.value_from
+ self
.value_to
< 0:
326 def filled_amount(self
, in_base_currency
=False):
328 for order
in self
.orders
:
329 filled_amount
+= order
.filled_amount(in_base_currency
=in_base_currency
)
332 def update_order(self
, order
, tick
):
334 if tick
in [0, 1, 3, 4, 6]:
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)
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)
346 if (tick
- 7) % 3 == 0:
347 new_order
= self
.prepare_order(compute_value
="default")
348 update
= "market_adjust"
349 compute_value
= "default"
354 update
= "market_fallback"
356 ReportStore
.log_order(order
, tick
, update
=update
,
357 compute_value
=compute_value
, new_order
=new_order
)
359 if new_order
is not None:
362 ReportStore
.log_order(order
, tick
, new_order
=new_order
)
364 def prepare_order(self
, compute_value
="default"):
365 if self
.action
is None:
367 ticker
= h
.get_ticker(self
.currency
, self
.base_currency
, self
.market
)
368 inverted
= ticker
["inverted"]
370 ticker
= ticker
["original"]
371 rate
= Computation
.compute_value(ticker
, self
.order_action(inverted
), compute_value
=compute_value
)
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)
380 base_currency
= self
.base_currency
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".
389 delta
= delta
- filled
390 # I already sold 60 FOO, 30 left
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
396 # -> I "buy" "9 / rate" FOO at proposed rate "rate"
398 # I already bought 3 / rate FOO, 6 / rate left
400 base_currency
= self
.currency
402 if self
.action
== "dispose":
403 filled
= self
.filled_amount(in_base_currency
=True)
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
415 # I already bought 300/125 BTC, only 600/125 left
417 filled
= self
.filled_amount(in_base_currency
=False)
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
427 delta
= delta
- filled
428 # I already sold 4 BTC, only 5 left
430 close_if_possible
= (self
.value_to
== 0)
433 ReportStore
.log_error("prepare_order", message
="Less to do than already filled: {}".format(delta
))
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
)
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
,
452 return "Trade({} -> {} in {}, {})".format(
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
))
466 def __init__(self
, action
, amount
, rate
, base_currency
, trade_type
, market
,
467 trade
, close_if_possible
=False):
471 self
.base_currency
= base_currency
473 self
.trade_type
= trade_type
476 self
.status
= "pending"
478 self
.close_if_possible
= close_if_possible
480 self
.fetch_cache_timestamp
= None
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
,
490 "status": self
.status
,
491 "close_if_possible": self
.close_if_possible
,
493 "mouvements": list(map(lambda x
: x
.as_json(), self
.mouvements
))
497 return "Order({} {} {} at {} {} [{}]{})".format(
504 " ✂" if self
.close_if_possible
else "",
509 if self
.trade_type
== "long":
516 return self
.status
== "open"
520 return self
.status
== "pending"
524 return self
.status
== "closed" or self
.status
== "canceled" or self
.status
== "error"
527 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
528 amount
= round(self
.amount
, self
.market
.order_precision(symbol
)).value
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}
)
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()
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
)
547 self
.id = self
.results
[0]["id"]
550 def get_status(self
):
552 ReportStore
.log_debug_action("Getting {} status".format(self
))
554 # other states are "closed" and "canceled"
555 if not self
.finished
:
558 self
.mark_finished_order()
561 def mark_finished_order(self
):
563 ReportStore
.log_debug_action("Mark {} as finished".format(self
))
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
)
569 def fetch(self
, force
=False):
571 ReportStore
.log_debug_action("Fetching {}".format(self
))
573 if (not force
and self
.fetch_cache_timestamp
is not None
574 and time
.time() - self
.fetch_cache_timestamp
< 10):
576 self
.fetch_cache_timestamp
= time
.time()
578 result
= self
.market
.fetch_order(self
.id)
579 self
.results
.append(result
)
581 self
.status
= result
["status"]
582 # Time at which the order started
583 self
.timestamp
= result
["datetime"]
584 self
.fetch_mouvements()
586 # FIXME: consider open order with dust remaining as closed
588 def dust_amount_remaining(self
):
589 return self
.remaining_amount() < Amount(self
.amount
.currency
, D("0.001"))
591 def remaining_amount(self
):
592 if self
.status
== "open":
594 return self
.amount
- self
.filled_amount()
596 def filled_amount(self
, in_base_currency
=False):
597 if self
.status
== "open":
600 for mouvement
in self
.mouvements
:
602 filled_amount
+= mouvement
.total_in_base
604 filled_amount
+= mouvement
.total
607 def fetch_mouvements(self
):
609 mouvements
= self
.market
.privatePostReturnOrderTrades({"orderNumber": self.id}
)
610 except ExchangeError
:
614 for mouvement_hash
in mouvements
:
615 self
.mouvements
.append(Mouvement(self
.amount
.currency
,
616 self
.base_currency
, mouvement_hash
))
620 ReportStore
.log_debug_action("Mark {} as cancelled".format(self
))
621 self
.status
= "canceled"
623 self
.market
.cancel_order(self
.id)
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))
634 self
.date
= datetime
.strptime(hash_
.get("date", ""), '%Y-%m-%d %H:%M:%S')
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))
644 "fee_rate": self
.fee_rate
,
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
654 if self
.fee_rate
> 0:
655 fee_rate
= " fee: {}%".format(self
.fee_rate
* 100)
658 if self
.date
is None:
662 return "Mouvement({} ; {} {} ({}){})".format(
663 date
, self
.action
, self
.total
, self
.total_in_base
,
666 if __name__
== '__main__': # pragma: no cover
667 from market
import market
668 h
.print_orders(market
)