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 delta_in_base
= abs(self
.delta
)
324 # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case)
326 if not self
.inverted
:
327 base_currency
= self
.base_currency
329 if self
.action
== "dispose":
330 filled
= self
.filled_amount(in_base_currency
=False)
331 delta
= delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
332 # I have 10 BTC worth of FOO, and I want to sell 9 BTC
333 # worth of it, computed first with rate 10 FOO = 1 BTC.
334 # -> I "sell" "90" FOO at proposed rate "rate".
336 delta
= delta
- filled
337 # I already sold 60 FOO, 30 left
339 filled
= self
.filled_amount(in_base_currency
=True)
340 delta
= (delta_in_base
- filled
).in_currency(self
.currency
, self
.market
, rate
=1/rate
)
341 # I want to buy 9 BTC worth of FOO, computed with rate
343 # -> I "buy" "9 / rate" FOO at proposed rate "rate"
345 # I already bought 3 / rate FOO, 6 / rate left
347 base_currency
= self
.currency
349 if self
.action
== "dispose":
350 filled
= self
.filled_amount(in_base_currency
=True)
353 delta
= (delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
354 - filled
).in_currency(self
.base_currency
, self
.market
, rate
=1/rate
)
355 # I have 10 BTC worth of FOO, and I want to sell 9 BTC worth of it
356 # computed at rate 1 Foo = 0.01 BTC
357 # Computation says I should sell it at 125 FOO / BTC
358 # -> delta_in_base = 9 BTC
359 # -> delta = (9 * 1/0.01 FOO) * 1/125 = 7.2 BTC
360 # Action: "buy" "7.2 BTC" at rate "125" "FOO" on market
362 # I already bought 300/125 BTC, only 600/125 left
364 filled
= self
.filled_amount(in_base_currency
=False)
367 delta
= delta_in_base
368 # I have 1 BTC worth of FOO, and I want to buy 9 BTC worth of it
369 # At rate 100 Foo / BTC
370 # Computation says I should buy it at 125 FOO / BTC
371 # -> delta_in_base = 9 BTC
372 # Action: "sell" "9 BTC" at rate "125" "FOO" on market
374 delta
= delta
- filled
375 # I already sold 4 BTC, only 5 left
377 if close_if_possible
is None:
378 close_if_possible
= (self
.value_to
== 0)
381 self
.market
.report
.log_error("prepare_order", message
="Less to do than already filled: {}".format(delta
))
384 order
= Order(self
.order_action(),
385 delta
, rate
, base_currency
, self
.trade_type
,
386 self
.market
, self
, close_if_possible
=close_if_possible
)
387 self
.orders
.append(order
)
392 "action": self
.action
,
393 "from": self
.value_from
.as_json()["value"],
394 "to": self
.value_to
.as_json()["value"],
395 "currency": self
.currency
,
396 "base_currency": self
.base_currency
,
400 if self
.closed
and not self
.is_fullfiled
:
402 elif self
.is_fullfiled
:
407 return "Trade({} -> {} in {}, {}{})".format(
414 def print_with_order(self
, ind
=""):
415 self
.market
.report
.print_log("{}{}".format(ind
, self
))
416 for order
in self
.orders
:
417 self
.market
.report
.print_log("{}\t{}".format(ind
, order
))
418 for mouvement
in order
.mouvements
:
419 self
.market
.report
.print_log("{}\t\t{}".format(ind
, mouvement
))
421 class RetryException(Exception):
425 def __init__(self
, action
, amount
, rate
, base_currency
, trade_type
, market
,
426 trade
, close_if_possible
=False):
430 self
.base_currency
= base_currency
432 self
.trade_type
= trade_type
435 self
.status
= "pending"
437 self
.close_if_possible
= close_if_possible
440 self
.start_date
= None
444 "action": self
.action
,
445 "trade_type": self
.trade_type
,
446 "amount": self
.amount
.as_json()["value"],
447 "currency": self
.amount
.as_json()["currency"],
448 "base_currency": self
.base_currency
,
450 "status": self
.status
,
451 "close_if_possible": self
.close_if_possible
,
453 "mouvements": list(map(lambda x
: x
.as_json(), self
.mouvements
))
457 return "Order({} {} {} at {} {} [{}]{})".format(
464 " ✂" if self
.close_if_possible
else "",
469 if self
.trade_type
== "long":
476 return self
.status
== "open"
480 return self
.status
== "pending"
484 return self
.status
.startswith("closed") or self
.status
== "canceled" or self
.status
== "error"
486 @retry((InsufficientFunds
, RetryException
, InvalidNonce
))
489 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
490 amount
= round(self
.amount
, self
.market
.ccxt
.order_precision(symbol
)).value
492 action
= "market.ccxt.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(symbol
, self
.action
, amount
, self
.rate
, self
.account
)
493 if self
.market
.debug
:
494 self
.market
.report
.log_debug_action(action
)
495 self
.results
.append({"debug": True, "id": -1}
)
497 self
.start_date
= datetime
.datetime
.now()
499 self
.results
.append(self
.market
.ccxt
.create_order(symbol
, 'limit', self
.action
, amount
, price
=self
.rate
, account
=self
.account
))
501 # Impossible to honor the order (dust amount)
502 self
.status
= "closed"
503 self
.mark_finished_order()
505 except InvalidNonce
as e
:
507 self
.market
.report
.log_error(action
, message
="Retrying after invalid nonce", exception
=e
)
510 self
.market
.report
.log_error(action
, message
="Giving up {} after invalid nonce".format(self
), exception
=e
)
511 self
.status
= "error"
513 except RequestTimeout
as e
:
514 if not self
.retrieve_order():
516 self
.market
.report
.log_error(action
, message
="Retrying after timeout", exception
=e
)
517 # We make a specific call in case retrieve_order
521 self
.market
.report
.log_error(action
, message
="Giving up {} after timeouts".format(self
), exception
=e
)
522 self
.status
= "error"
525 self
.market
.report
.log_error(action
, message
="Timeout, found the order")
526 except InsufficientFunds
as e
:
528 self
.market
.report
.log_error(action
, message
="Retrying with reduced amount", exception
=e
)
529 self
.amount
= self
.amount
* D("0.99")
532 self
.market
.report
.log_error(action
, message
="Giving up {}".format(self
), exception
=e
)
533 self
.status
= "error"
535 except Exception as e
:
536 self
.status
= "error"
537 self
.market
.report
.log_error(action
, exception
=e
)
539 self
.id = self
.results
[0]["id"]
542 def get_status(self
):
543 if self
.market
.debug
:
544 self
.market
.report
.log_debug_action("Getting {} status".format(self
))
546 # other states are "closed" and "canceled"
547 if not self
.finished
:
551 def mark_disappeared_order(self
):
552 if self
.status
.startswith("closed") and \
553 len(self
.mouvements
) > 0 and \
554 self
.mouvements
[-1].total_in_base
== 0:
555 self
.status
= "error_disappeared"
557 def mark_finished_order(self
):
558 if self
.status
.startswith("closed") and self
.market
.debug
:
559 self
.market
.report
.log_debug_action("Mark {} as finished".format(self
))
561 if self
.status
.startswith("closed"):
562 if self
.trade_type
== "short" and self
.action
== "buy" and self
.close_if_possible
:
563 self
.market
.ccxt
.close_margin_position(self
.amount
.currency
, self
.base_currency
)
566 if self
.market
.debug
:
567 self
.market
.report
.log_debug_action("Fetching {}".format(self
))
570 result
= self
.market
.ccxt
.fetch_order(self
.id)
571 self
.results
.append(result
)
572 self
.status
= result
["status"]
573 # Time at which the order started
574 self
.timestamp
= result
["datetime"]
575 except OrderNotCached
:
576 self
.status
= "closed_unknown"
578 self
.fetch_mouvements()
580 self
.mark_disappeared_order()
581 self
.mark_dust_amount_remaining_order()
582 self
.mark_finished_order()
584 def mark_dust_amount_remaining_order(self
):
585 if self
.market
.ccxt
.is_dust_trade(self
.remaining_amount().value
, self
.rate
):
586 self
.status
= "closed_dust_remaining"
588 def remaining_amount(self
):
589 return self
.amount
- self
.filled_amount()
591 def filled_amount(self
, in_base_currency
=False):
592 if self
.status
== "open":
595 for mouvement
in self
.mouvements
:
597 filled_amount
+= mouvement
.total_in_base
599 filled_amount
+= mouvement
.total
602 def fetch_mouvements(self
):
604 mouvements
= self
.market
.ccxt
.privatePostReturnOrderTrades({"orderNumber": self.id}
)
605 except ExchangeError
:
609 for mouvement_hash
in mouvements
:
610 self
.mouvements
.append(Mouvement(self
.amount
.currency
,
611 self
.base_currency
, mouvement_hash
))
612 self
.mouvements
.sort(key
= lambda x
: x
.date
)
615 if self
.market
.debug
:
616 self
.market
.report
.log_debug_action("Mark {} as cancelled".format(self
))
617 self
.status
= "canceled"
619 if (self
.status
== "closed_dust_remaining" or self
.open) and self
.id is not None:
621 self
.market
.ccxt
.cancel_order(self
.id)
622 except OrderNotFound
as e
: # Closed inbetween
623 self
.market
.report
.log_error("cancel_order", message
="Already cancelled order", exception
=e
)
626 def retrieve_order(self
):
627 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
628 amount
= round(self
.amount
, self
.market
.ccxt
.order_precision(symbol
)).value
629 start_timestamp
= self
.start_date
.timestamp() - 5
631 similar_open_orders
= self
.market
.ccxt
.fetch_orders(symbol
=symbol
, since
=start_timestamp
)
632 for order
in similar_open_orders
:
633 if (order
["info"]["margin"] == 1 and self
.account
== "exchange") or\
634 (order
["info"]["margin"] != 1 and self
.account
== "margin"):
635 i_m_tested
= True # coverage bug ?!
637 if order
["info"]["side"] != self
.action
:
640 abs(D(order
["info"]["startingAmount"]) - amount
),
641 self
.market
.ccxt
.order_precision(symbol
))
643 abs(D(order
["info"]["rate"]) - self
.rate
),
644 self
.market
.ccxt
.order_precision(symbol
))
645 if amount_diff
!= 0 or rate_diff
!= 0:
647 self
.results
.append({"id": order["id"]}
)
650 similar_trades
= self
.market
.ccxt
.fetch_my_trades(symbol
=symbol
, since
=start_timestamp
)
651 for order_id
in sorted(list(map(lambda x
: x
["order"], similar_trades
))):
652 trades
= list(filter(lambda x
: x
["order"] == order_id
, similar_trades
))
653 if any(x
["timestamp"] < start_timestamp
for x
in trades
):
655 if any(x
["side"] != self
.action
for x
in trades
):
657 if any(x
["info"]["category"] == "exchange" and self
.account
== "margin" for x
in trades
) or\
658 any(x
["info"]["category"] == "marginTrade" and self
.account
== "exchange" for x
in trades
):
660 trade_sum
= sum(D(x
["info"]["amount"]) for x
in trades
)
661 amount_diff
= round(abs(trade_sum
- amount
),
662 self
.market
.ccxt
.order_precision(symbol
))
665 if (self
.action
== "sell" and any(D(x
["info"]["rate"]) < self
.rate
for x
in trades
)) or\
666 (self
.action
== "buy" and any(D(x
["info"]["rate"]) > self
.rate
for x
in trades
)):
668 self
.results
.append({"id": order_id}
)
674 def __init__(self
, currency
, base_currency
, hash_
):
675 self
.currency
= currency
676 self
.base_currency
= base_currency
677 self
.id = hash_
.get("tradeID")
678 self
.action
= hash_
.get("type")
679 self
.fee_rate
= D(hash_
.get("fee", -1))
681 self
.date
= datetime
.datetime
.strptime(hash_
.get("date", ""), '%Y-%m-%d %H:%M:%S')
684 self
.rate
= D(hash_
.get("rate", 0))
685 self
.total
= Amount(currency
, hash_
.get("amount", 0))
686 # rate * total = total_in_base
687 self
.total_in_base
= Amount(base_currency
, hash_
.get("total", 0))
691 "fee_rate": self
.fee_rate
,
693 "action": self
.action
,
694 "total": self
.total
.value
,
695 "currency": self
.currency
,
696 "total_in_base": self
.total_in_base
.value
,
697 "base_currency": self
.base_currency
701 if self
.fee_rate
> 0:
702 fee_rate
= " fee: {}%".format(self
.fee_rate
* 100)
705 if self
.date
is None:
709 return "Mouvement({} ; {} {} ({}){})".format(
710 date
, self
.action
, self
.total
, self
.total_in_base
,