2 from retry
import retry
3 from decimal
import Decimal
as D
, ROUND_DOWN
4 from ccxt
import ExchangeError
, InsufficientFunds
, ExchangeNotAvailable
, InvalidOrder
, OrderNotCached
, OrderNotFound
, RequestTimeout
, InvalidNonce
8 "default": lambda x
, y
: x
[y
],
9 "average": lambda x
, y
: x
["average"],
10 "bid": lambda x
, y
: x
["bid"],
11 "ask": lambda x
, y
: x
["ask"],
15 def compute_value(cls
, ticker
, action
, compute_value
="default"):
20 if isinstance(compute_value
, str):
21 compute_value
= cls
.computations
[compute_value
]
22 return compute_value(ticker
, action
)
25 def __init__(self
, currency
, value
, linked_to
=None, ticker
=None, rate
=None):
26 self
.currency
= currency
28 self
.linked_to
= linked_to
32 def in_currency(self
, other_currency
, market
, rate
=None, action
=None, compute_value
="average"):
33 if other_currency
== self
.currency
:
41 asset_ticker
= market
.get_ticker(self
.currency
, other_currency
)
42 if asset_ticker
is not None:
43 rate
= Computation
.compute_value(asset_ticker
, action
, compute_value
=compute_value
)
51 raise Exception("This asset is not available in the chosen market")
55 "currency": self
.currency
,
56 "value": round(self
).value
.normalize(),
59 def __round__(self
, n
=8):
60 return Amount(self
.currency
, self
.value
.quantize(D(1)/D(10**n
), rounding
=ROUND_DOWN
))
63 return Amount(self
.currency
, abs(self
.value
))
65 def __add__(self
, other
):
68 if other
.currency
!= self
.currency
and other
.value
* self
.value
!= 0:
69 raise Exception("Summing amounts must be done with same currencies")
70 return Amount(self
.currency
, self
.value
+ other
.value
)
72 def __radd__(self
, other
):
76 return self
.__add
__(other
)
78 def __sub__(self
, other
):
81 if other
.currency
!= self
.currency
and other
.value
* self
.value
!= 0:
82 raise Exception("Summing amounts must be done with same currencies")
83 return Amount(self
.currency
, self
.value
- other
.value
)
85 def __rsub__(self
, other
):
89 return -self
.__sub
__(other
)
91 def __mul__(self
, value
):
92 if not isinstance(value
, (int, float, D
)):
93 raise TypeError("Amount may only be multiplied by numbers")
94 return Amount(self
.currency
, self
.value
* value
)
96 def __rmul__(self
, value
):
97 return self
.__mul
__(value
)
99 def __floordiv__(self
, value
):
100 if not isinstance(value
, (int, float, D
)):
101 raise TypeError("Amount may only be divided by numbers")
102 return Amount(self
.currency
, self
.value
/ value
)
104 def __truediv__(self
, value
):
105 return self
.__floordiv
__(value
)
107 def __lt__(self
, other
):
109 return self
.value
< 0
110 if self
.currency
!= other
.currency
:
111 raise Exception("Comparing amounts must be done with same currencies")
112 return self
.value
< other
.value
114 def __le__(self
, other
):
115 return self
== other
or self
< other
117 def __gt__(self
, other
):
118 return not self
<= other
120 def __ge__(self
, other
):
121 return not self
< other
123 def __eq__(self
, other
):
125 return self
.value
== 0
126 if self
.currency
!= other
.currency
:
127 raise Exception("Comparing amounts must be done with same currencies")
128 return self
.value
== other
.value
130 def __ne__(self
, other
):
131 return not self
== other
134 return Amount(self
.currency
, - self
.value
)
137 if self
.linked_to
is None:
138 return "{:.8f} {}".format(self
.value
, self
.currency
)
140 return "{:.8f} {} [{}]".format(self
.value
, self
.currency
, self
.linked_to
)
143 if self
.linked_to
is None:
144 return "Amount({:.8f} {})".format(self
.value
, self
.currency
)
146 return "Amount({:.8f} {} -> {})".format(self
.value
, self
.currency
, repr(self
.linked_to
))
149 base_keys
= ["total", "exchange_total", "exchange_used",
150 "exchange_free", "margin_total", "margin_in_position",
151 "margin_available", "margin_borrowed", "margin_pending_gain"]
153 def __init__(self
, currency
, hash_
):
154 self
.currency
= currency
155 for key
in self
.base_keys
:
156 setattr(self
, key
, Amount(currency
, hash_
.get(key
, 0)))
158 self
.margin_position_type
= hash_
.get("margin_position_type")
160 if hash_
.get("margin_borrowed_base_currency") is not None:
161 base_currency
= hash_
["margin_borrowed_base_currency"]
163 "margin_liquidation_price",
164 "margin_lending_fees",
165 "margin_pending_base_gain",
166 "margin_borrowed_base_price"
168 setattr(self
, key
, Amount(base_currency
, hash_
.get(key
, 0)))
171 return dict(map(lambda x
: (x
, getattr(self
, x
).as_json()["value"]), self
.base_keys
))
174 if self
.exchange_total
> 0:
175 if self
.exchange_free
> 0 and self
.exchange_used
> 0:
176 exchange
= " Exch: [✔{} + ❌{} = {}]".format(str(self
.exchange_free
), str(self
.exchange_used
), str(self
.exchange_total
))
177 elif self
.exchange_free
> 0:
178 exchange
= " Exch: [✔{}]".format(str(self
.exchange_free
))
180 exchange
= " Exch: [❌{}]".format(str(self
.exchange_used
))
184 if self
.margin_total
> 0:
185 if self
.margin_available
!= 0 and self
.margin_in_position
!= 0:
186 margin
= " Margin: [✔{} + ❌{} = {}]".format(str(self
.margin_available
), str(self
.margin_in_position
), str(self
.margin_total
))
187 elif self
.margin_available
!= 0:
188 margin
= " Margin: [✔{}]".format(str(self
.margin_available
))
190 margin
= " Margin: [❌{}]".format(str(self
.margin_in_position
))
191 elif self
.margin_total
< 0:
192 margin
= " Margin: [{} @@ {}/{}]".format(str(self
.margin_total
),
193 str(self
.margin_borrowed_base_price
),
194 str(self
.margin_lending_fees
))
198 if self
.margin_total
!= 0 and self
.exchange_total
!= 0:
199 total
= " Total: [{}]".format(str(self
.total
))
203 return "Balance({}".format(self
.currency
) + "".join([exchange
, margin
, total
]) + ")"
206 def __init__(self
, value_from
, value_to
, currency
, market
):
207 # We have value_from of currency, and want to finish with value_to of
208 # that currency. value_* may not be in currency's terms
209 self
.currency
= currency
210 self
.value_from
= value_from
211 self
.value_to
= value_to
216 assert self
.value_from
.value
* self
.value_to
.value
>= 0
217 assert self
.value_from
.currency
== self
.value_to
.currency
218 if self
.value_from
!= 0:
219 assert self
.value_from
.linked_to
is not None and self
.value_from
.linked_to
.currency
== self
.currency
220 elif self
.value_from
.linked_to
is None:
221 self
.value_from
.linked_to
= Amount(self
.currency
, 0)
222 self
.base_currency
= self
.value_from
.currency
226 return self
.value_to
- self
.value_from
230 if self
.value_from
== self
.value_to
:
232 if self
.base_currency
== self
.currency
:
235 if abs(self
.value_from
) < abs(self
.value_to
):
240 def order_action(self
):
241 if (self
.value_from
< self
.value_to
) != self
.inverted
:
247 def trade_type(self
):
248 if self
.value_from
+ self
.value_to
< 0:
255 return not (self
.is_fullfiled
or self
.closed
)
258 for order
in self
.orders
:
263 def is_fullfiled(self
):
264 return abs(self
.filled_amount(in_base_currency
=(not self
.inverted
))) >= abs(self
.delta
)
266 def filled_amount(self
, in_base_currency
=False):
268 for order
in self
.orders
:
269 filled_amount
+= order
.filled_amount(in_base_currency
=in_base_currency
)
273 0: ["waiting", None],
274 1: ["waiting", None],
275 2: ["adjusting", lambda x
, y
: (x
[y
] + x
["average"]) / 2],
276 3: ["waiting", None],
277 4: ["waiting", None],
278 5: ["adjusting", lambda x
, y
: (x
[y
]*2 + x
["average"]) / 3],
279 6: ["waiting", None],
280 7: ["market_fallback", "default"],
283 def tick_actions_recreate(self
, tick
, default
="average"):
284 return ([default
] + \
285 [ y
[1] for x
, y
in self
.tick_actions
.items() if x
<= tick
and y
[1] is not None ])[-1]
287 def update_order(self
, order
, tick
):
288 if tick
in self
.tick_actions
:
289 update
, compute_value
= self
.tick_actions
[tick
]
291 update
= "market_adjust"
292 compute_value
= "default"
297 if compute_value
is not None:
299 new_order
= self
.prepare_order(compute_value
=compute_value
)
303 self
.market
.report
.log_order(order
, tick
, update
=update
,
304 compute_value
=compute_value
, new_order
=new_order
)
306 if new_order
is not None:
308 self
.market
.report
.log_order(order
, tick
, new_order
=new_order
)
310 def prepare_order(self
, close_if_possible
=None, compute_value
="default"):
311 if self
.action
is None:
313 ticker
= self
.market
.get_ticker(self
.currency
, self
.base_currency
)
315 self
.market
.report
.log_error("prepare_order",
316 message
="Unknown ticker {}/{}".format(self
.currency
, self
.base_currency
))
318 self
.inverted
= ticker
["inverted"]
320 ticker
= ticker
["original"]
321 rate
= Computation
.compute_value(ticker
, self
.order_action(), compute_value
=compute_value
)
323 # FIXME: Dust amount should be removed from there if they werent
324 # honored in other sales
325 delta_in_base
= abs(self
.delta
)
326 # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case)
328 if not self
.inverted
:
329 base_currency
= self
.base_currency
331 if self
.action
== "dispose":
332 filled
= self
.filled_amount(in_base_currency
=False)
333 delta
= delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
334 # I have 10 BTC worth of FOO, and I want to sell 9 BTC
335 # worth of it, computed first with rate 10 FOO = 1 BTC.
336 # -> I "sell" "90" FOO at proposed rate "rate".
338 delta
= delta
- filled
339 # I already sold 60 FOO, 30 left
341 filled
= self
.filled_amount(in_base_currency
=True)
342 delta
= (delta_in_base
- filled
).in_currency(self
.currency
, self
.market
, rate
=1/rate
)
343 # I want to buy 9 BTC worth of FOO, computed with rate
345 # -> I "buy" "9 / rate" FOO at proposed rate "rate"
347 # I already bought 3 / rate FOO, 6 / rate left
349 base_currency
= self
.currency
351 if self
.action
== "dispose":
352 filled
= self
.filled_amount(in_base_currency
=True)
355 delta
= (delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
356 - filled
).in_currency(self
.base_currency
, self
.market
, rate
=1/rate
)
357 # I have 10 BTC worth of FOO, and I want to sell 9 BTC worth of it
358 # computed at rate 1 Foo = 0.01 BTC
359 # Computation says I should sell it at 125 FOO / BTC
360 # -> delta_in_base = 9 BTC
361 # -> delta = (9 * 1/0.01 FOO) * 1/125 = 7.2 BTC
362 # Action: "buy" "7.2 BTC" at rate "125" "FOO" on market
364 # I already bought 300/125 BTC, only 600/125 left
366 filled
= self
.filled_amount(in_base_currency
=False)
369 delta
= delta_in_base
370 # I have 1 BTC worth of FOO, and I want to buy 9 BTC worth of it
371 # At rate 100 Foo / BTC
372 # Computation says I should buy it at 125 FOO / BTC
373 # -> delta_in_base = 9 BTC
374 # Action: "sell" "9 BTC" at rate "125" "FOO" on market
376 delta
= delta
- filled
377 # I already sold 4 BTC, only 5 left
379 if close_if_possible
is None:
380 close_if_possible
= (self
.value_to
== 0)
383 self
.market
.report
.log_error("prepare_order", message
="Less to do than already filled: {}".format(delta
))
386 order
= Order(self
.order_action(),
387 delta
, rate
, base_currency
, self
.trade_type
,
388 self
.market
, self
, close_if_possible
=close_if_possible
)
389 self
.orders
.append(order
)
394 "action": self
.action
,
395 "from": self
.value_from
.as_json()["value"],
396 "to": self
.value_to
.as_json()["value"],
397 "currency": self
.currency
,
398 "base_currency": self
.base_currency
,
402 if self
.closed
and not self
.is_fullfiled
:
404 elif self
.is_fullfiled
:
409 return "Trade({} -> {} in {}, {}{})".format(
416 def print_with_order(self
, ind
=""):
417 self
.market
.report
.print_log("{}{}".format(ind
, self
))
418 for order
in self
.orders
:
419 self
.market
.report
.print_log("{}\t{}".format(ind
, order
))
420 for mouvement
in order
.mouvements
:
421 self
.market
.report
.print_log("{}\t\t{}".format(ind
, mouvement
))
423 class RetryException(Exception):
427 def __init__(self
, action
, amount
, rate
, base_currency
, trade_type
, market
,
428 trade
, close_if_possible
=False):
432 self
.base_currency
= base_currency
434 self
.trade_type
= trade_type
437 self
.status
= "pending"
439 self
.close_if_possible
= close_if_possible
442 self
.start_date
= None
446 "action": self
.action
,
447 "trade_type": self
.trade_type
,
448 "amount": self
.amount
.as_json()["value"],
449 "currency": self
.amount
.as_json()["currency"],
450 "base_currency": self
.base_currency
,
452 "status": self
.status
,
453 "close_if_possible": self
.close_if_possible
,
455 "mouvements": list(map(lambda x
: x
.as_json(), self
.mouvements
))
459 return "Order({} {} {} at {} {} [{}]{})".format(
466 " ✂" if self
.close_if_possible
else "",
471 if self
.trade_type
== "long":
478 return self
.status
== "open"
482 return self
.status
== "pending"
486 return self
.status
.startswith("closed") or self
.status
== "canceled" or self
.status
== "error"
488 @retry((InsufficientFunds
, RetryException
, InvalidNonce
))
491 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
492 amount
= round(self
.amount
, self
.market
.ccxt
.order_precision(symbol
)).value
494 action
= "market.ccxt.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(symbol
, self
.action
, amount
, self
.rate
, self
.account
)
495 if self
.market
.debug
:
496 self
.market
.report
.log_debug_action(action
)
497 self
.results
.append({"debug": True, "id": -1}
)
499 self
.start_date
= datetime
.datetime
.now()
501 self
.results
.append(self
.market
.ccxt
.create_order(symbol
, 'limit', self
.action
, amount
, price
=self
.rate
, account
=self
.account
))
503 # Impossible to honor the order (dust amount)
504 self
.status
= "closed"
505 self
.mark_finished_order()
507 except InvalidNonce
as e
:
509 self
.market
.report
.log_error(action
, message
="Retrying after invalid nonce", exception
=e
)
512 self
.market
.report
.log_error(action
, message
="Giving up {} after invalid nonce".format(self
), exception
=e
)
513 self
.status
= "error"
515 except RequestTimeout
as e
:
516 if not self
.retrieve_order():
518 self
.market
.report
.log_error(action
, message
="Retrying after timeout", exception
=e
)
519 # We make a specific call in case retrieve_order
523 self
.market
.report
.log_error(action
, message
="Giving up {} after timeouts".format(self
), exception
=e
)
524 self
.status
= "error"
527 self
.market
.report
.log_error(action
, message
="Timeout, found the order")
528 except InsufficientFunds
as e
:
530 self
.market
.report
.log_error(action
, message
="Retrying with reduced amount", exception
=e
)
531 self
.amount
= self
.amount
* D("0.99")
534 self
.market
.report
.log_error(action
, message
="Giving up {}".format(self
), exception
=e
)
535 self
.status
= "error"
537 except Exception as e
:
538 self
.status
= "error"
539 self
.market
.report
.log_error(action
, exception
=e
)
541 self
.id = self
.results
[0]["id"]
544 def get_status(self
):
545 if self
.market
.debug
:
546 self
.market
.report
.log_debug_action("Getting {} status".format(self
))
548 # other states are "closed" and "canceled"
549 if not self
.finished
:
553 def mark_disappeared_order(self
):
554 if self
.status
.startswith("closed") and \
555 len(self
.mouvements
) > 0 and \
556 self
.mouvements
[-1].total_in_base
== 0:
557 self
.status
= "error_disappeared"
559 def mark_finished_order(self
):
560 if self
.status
.startswith("closed") and self
.market
.debug
:
561 self
.market
.report
.log_debug_action("Mark {} as finished".format(self
))
563 if self
.status
.startswith("closed"):
564 if self
.trade_type
== "short" and self
.action
== "buy" and self
.close_if_possible
:
565 self
.market
.ccxt
.close_margin_position(self
.amount
.currency
, self
.base_currency
)
568 if self
.market
.debug
:
569 self
.market
.report
.log_debug_action("Fetching {}".format(self
))
572 result
= self
.market
.ccxt
.fetch_order(self
.id)
573 self
.results
.append(result
)
574 self
.status
= result
["status"]
575 # Time at which the order started
576 self
.timestamp
= result
["datetime"]
577 except OrderNotCached
:
578 self
.status
= "closed_unknown"
580 self
.fetch_mouvements()
582 self
.mark_disappeared_order()
584 self
.mark_finished_order()
585 # FIXME: consider open order with dust remaining as closed
587 def dust_amount_remaining(self
):
588 return self
.remaining_amount() < Amount(self
.amount
.currency
, D("0.001"))
590 def remaining_amount(self
):
591 return self
.amount
- self
.filled_amount()
593 def filled_amount(self
, in_base_currency
=False):
594 if self
.status
== "open":
597 for mouvement
in self
.mouvements
:
599 filled_amount
+= mouvement
.total_in_base
601 filled_amount
+= mouvement
.total
604 def fetch_mouvements(self
):
606 mouvements
= self
.market
.ccxt
.privatePostReturnOrderTrades({"orderNumber": self.id}
)
607 except ExchangeError
:
611 for mouvement_hash
in mouvements
:
612 self
.mouvements
.append(Mouvement(self
.amount
.currency
,
613 self
.base_currency
, mouvement_hash
))
614 self
.mouvements
.sort(key
= lambda x
: x
.date
)
617 if self
.market
.debug
:
618 self
.market
.report
.log_debug_action("Mark {} as cancelled".format(self
))
619 self
.status
= "canceled"
621 if self
.open and self
.id is not None:
623 self
.market
.ccxt
.cancel_order(self
.id)
624 except OrderNotFound
as e
: # Closed inbetween
625 self
.market
.report
.log_error("cancel_order", message
="Already cancelled order", exception
=e
)
628 def retrieve_order(self
):
629 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
630 amount
= round(self
.amount
, self
.market
.ccxt
.order_precision(symbol
)).value
631 start_timestamp
= self
.start_date
.timestamp() - 5
633 similar_open_orders
= self
.market
.ccxt
.fetch_orders(symbol
=symbol
, since
=start_timestamp
)
634 for order
in similar_open_orders
:
635 if (order
["info"]["margin"] == 1 and self
.account
== "exchange") or\
636 (order
["info"]["margin"] != 1 and self
.account
== "margin"):
637 i_m_tested
= True # coverage bug ?!
639 if order
["info"]["side"] != self
.action
:
642 abs(D(order
["info"]["startingAmount"]) - amount
),
643 self
.market
.ccxt
.order_precision(symbol
))
645 abs(D(order
["info"]["rate"]) - self
.rate
),
646 self
.market
.ccxt
.order_precision(symbol
))
647 if amount_diff
!= 0 or rate_diff
!= 0:
649 self
.results
.append({"id": order["id"]}
)
652 similar_trades
= self
.market
.ccxt
.fetch_my_trades(symbol
=symbol
, since
=start_timestamp
)
653 # FIXME: use set instead of sorted(list(...))
654 for order_id
in sorted(list(map(lambda x
: x
["order"], similar_trades
))):
655 trades
= list(filter(lambda x
: x
["order"] == order_id
, similar_trades
))
656 if any(x
["timestamp"] < start_timestamp
for x
in trades
):
658 if any(x
["side"] != self
.action
for x
in trades
):
660 if any(x
["info"]["category"] == "exchange" and self
.account
== "margin" for x
in trades
) or\
661 any(x
["info"]["category"] == "marginTrade" and self
.account
== "exchange" for x
in trades
):
663 trade_sum
= sum(D(x
["info"]["amount"]) for x
in trades
)
664 amount_diff
= round(abs(trade_sum
- amount
),
665 self
.market
.ccxt
.order_precision(symbol
))
668 if (self
.action
== "sell" and any(D(x
["info"]["rate"]) < self
.rate
for x
in trades
)) or\
669 (self
.action
== "buy" and any(D(x
["info"]["rate"]) > self
.rate
for x
in trades
)):
671 self
.results
.append({"id": order_id}
)
677 def __init__(self
, currency
, base_currency
, hash_
):
678 self
.currency
= currency
679 self
.base_currency
= base_currency
680 self
.id = hash_
.get("tradeID")
681 self
.action
= hash_
.get("type")
682 self
.fee_rate
= D(hash_
.get("fee", -1))
684 self
.date
= datetime
.datetime
.strptime(hash_
.get("date", ""), '%Y-%m-%d %H:%M:%S')
687 self
.rate
= D(hash_
.get("rate", 0))
688 self
.total
= Amount(currency
, hash_
.get("amount", 0))
689 # rate * total = total_in_base
690 self
.total_in_base
= Amount(base_currency
, hash_
.get("total", 0))
694 "fee_rate": self
.fee_rate
,
696 "action": self
.action
,
697 "total": self
.total
.value
,
698 "currency": self
.currency
,
699 "total_in_base": self
.total_in_base
.value
,
700 "base_currency": self
.base_currency
704 if self
.fee_rate
> 0:
705 fee_rate
= " fee: {}%".format(self
.fee_rate
* 100)
708 if self
.date
is None:
712 return "Mouvement({} ; {} {} ({}){})".format(
713 date
, self
.action
, self
.total
, self
.total_in_base
,