1 from datetime
import datetime
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
)
272 def update_order(self
, order
, tick
):
274 0: ["waiting", None],
275 1: ["waiting", None],
276 2: ["adjusting", lambda x
, y
: (x
[y
] + x
["average"]) / 2],
277 3: ["waiting", None],
278 4: ["waiting", None],
279 5: ["adjusting", lambda x
, y
: (x
[y
]*2 + x
["average"]) / 3],
280 6: ["waiting", None],
281 7: ["market_fallback", "default"],
285 update
, compute_value
= actions
[tick
]
287 update
= "market_adjust"
288 compute_value
= "default"
293 if compute_value
is not None:
295 new_order
= self
.prepare_order(compute_value
=compute_value
)
299 self
.market
.report
.log_order(order
, tick
, update
=update
,
300 compute_value
=compute_value
, new_order
=new_order
)
302 if new_order
is not None:
304 self
.market
.report
.log_order(order
, tick
, new_order
=new_order
)
306 def prepare_order(self
, close_if_possible
=None, compute_value
="default"):
307 if self
.action
is None:
309 ticker
= self
.market
.get_ticker(self
.currency
, self
.base_currency
)
310 self
.inverted
= ticker
["inverted"]
312 ticker
= ticker
["original"]
313 rate
= Computation
.compute_value(ticker
, self
.order_action(), compute_value
=compute_value
)
315 # FIXME: Dust amount should be removed from there if they werent
316 # honored in other sales
317 delta_in_base
= abs(self
.delta
)
318 # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case)
320 if not self
.inverted
:
321 base_currency
= self
.base_currency
323 if self
.action
== "dispose":
324 filled
= self
.filled_amount(in_base_currency
=False)
325 delta
= delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
326 # I have 10 BTC worth of FOO, and I want to sell 9 BTC
327 # worth of it, computed first with rate 10 FOO = 1 BTC.
328 # -> I "sell" "90" FOO at proposed rate "rate".
330 delta
= delta
- filled
331 # I already sold 60 FOO, 30 left
333 filled
= self
.filled_amount(in_base_currency
=True)
334 delta
= (delta_in_base
- filled
).in_currency(self
.currency
, self
.market
, rate
=1/rate
)
335 # I want to buy 9 BTC worth of FOO, computed with rate
337 # -> I "buy" "9 / rate" FOO at proposed rate "rate"
339 # I already bought 3 / rate FOO, 6 / rate left
341 base_currency
= self
.currency
343 if self
.action
== "dispose":
344 filled
= self
.filled_amount(in_base_currency
=True)
347 delta
= (delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
348 - filled
).in_currency(self
.base_currency
, self
.market
, rate
=1/rate
)
349 # I have 10 BTC worth of FOO, and I want to sell 9 BTC worth of it
350 # computed at rate 1 Foo = 0.01 BTC
351 # Computation says I should sell it at 125 FOO / BTC
352 # -> delta_in_base = 9 BTC
353 # -> delta = (9 * 1/0.01 FOO) * 1/125 = 7.2 BTC
354 # Action: "buy" "7.2 BTC" at rate "125" "FOO" on market
356 # I already bought 300/125 BTC, only 600/125 left
358 filled
= self
.filled_amount(in_base_currency
=False)
361 delta
= delta_in_base
362 # I have 1 BTC worth of FOO, and I want to buy 9 BTC worth of it
363 # At rate 100 Foo / BTC
364 # Computation says I should buy it at 125 FOO / BTC
365 # -> delta_in_base = 9 BTC
366 # Action: "sell" "9 BTC" at rate "125" "FOO" on market
368 delta
= delta
- filled
369 # I already sold 4 BTC, only 5 left
371 if close_if_possible
is None:
372 close_if_possible
= (self
.value_to
== 0)
375 self
.market
.report
.log_error("prepare_order", message
="Less to do than already filled: {}".format(delta
))
378 order
= Order(self
.order_action(),
379 delta
, rate
, base_currency
, self
.trade_type
,
380 self
.market
, self
, close_if_possible
=close_if_possible
)
381 self
.orders
.append(order
)
386 "action": self
.action
,
387 "from": self
.value_from
.as_json()["value"],
388 "to": self
.value_to
.as_json()["value"],
389 "currency": self
.currency
,
390 "base_currency": self
.base_currency
,
394 if self
.closed
and not self
.is_fullfiled
:
396 elif self
.is_fullfiled
:
401 return "Trade({} -> {} in {}, {}{})".format(
408 def print_with_order(self
, ind
=""):
409 self
.market
.report
.print_log("{}{}".format(ind
, self
))
410 for order
in self
.orders
:
411 self
.market
.report
.print_log("{}\t{}".format(ind
, order
))
412 for mouvement
in order
.mouvements
:
413 self
.market
.report
.print_log("{}\t\t{}".format(ind
, mouvement
))
415 class RetryException(Exception):
419 def __init__(self
, action
, amount
, rate
, base_currency
, trade_type
, market
,
420 trade
, close_if_possible
=False):
424 self
.base_currency
= base_currency
426 self
.trade_type
= trade_type
429 self
.status
= "pending"
431 self
.close_if_possible
= close_if_possible
434 self
.start_date
= None
438 "action": self
.action
,
439 "trade_type": self
.trade_type
,
440 "amount": self
.amount
.as_json()["value"],
441 "currency": self
.amount
.as_json()["currency"],
442 "base_currency": self
.base_currency
,
444 "status": self
.status
,
445 "close_if_possible": self
.close_if_possible
,
447 "mouvements": list(map(lambda x
: x
.as_json(), self
.mouvements
))
451 return "Order({} {} {} at {} {} [{}]{})".format(
458 " ✂" if self
.close_if_possible
else "",
463 if self
.trade_type
== "long":
470 return self
.status
== "open"
474 return self
.status
== "pending"
478 return self
.status
.startswith("closed") or self
.status
== "canceled" or self
.status
== "error"
480 @retry((InsufficientFunds
, RetryException
, InvalidNonce
))
483 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
484 amount
= round(self
.amount
, self
.market
.ccxt
.order_precision(symbol
)).value
486 action
= "market.ccxt.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(symbol
, self
.action
, amount
, self
.rate
, self
.account
)
487 if self
.market
.debug
:
488 self
.market
.report
.log_debug_action(action
)
489 self
.results
.append({"debug": True, "id": -1}
)
491 self
.start_date
= datetime
.now()
493 self
.results
.append(self
.market
.ccxt
.create_order(symbol
, 'limit', self
.action
, amount
, price
=self
.rate
, account
=self
.account
))
495 # Impossible to honor the order (dust amount)
496 self
.status
= "closed"
497 self
.mark_finished_order()
499 except InvalidNonce
as e
:
501 self
.market
.report
.log_error(action
, message
="Retrying after invalid nonce", exception
=e
)
504 self
.market
.report
.log_error(action
, message
="Giving up {} after invalid nonce".format(self
), exception
=e
)
505 self
.status
= "error"
507 except RequestTimeout
as e
:
508 if not self
.retrieve_order():
510 self
.market
.report
.log_error(action
, message
="Retrying after timeout", exception
=e
)
511 # We make a specific call in case retrieve_order
515 self
.market
.report
.log_error(action
, message
="Giving up {} after timeouts".format(self
), exception
=e
)
516 self
.status
= "error"
519 self
.market
.report
.log_error(action
, message
="Timeout, found the order")
520 except InsufficientFunds
as e
:
522 self
.market
.report
.log_error(action
, message
="Retrying with reduced amount", exception
=e
)
523 self
.amount
= self
.amount
* D("0.99")
526 self
.market
.report
.log_error(action
, message
="Giving up {}".format(self
), exception
=e
)
527 self
.status
= "error"
529 except Exception as e
:
530 self
.status
= "error"
531 self
.market
.report
.log_error(action
, exception
=e
)
533 self
.id = self
.results
[0]["id"]
536 def get_status(self
):
537 if self
.market
.debug
:
538 self
.market
.report
.log_debug_action("Getting {} status".format(self
))
540 # other states are "closed" and "canceled"
541 if not self
.finished
:
545 def mark_finished_order(self
):
546 if self
.status
.startswith("closed") and self
.market
.debug
:
547 self
.market
.report
.log_debug_action("Mark {} as finished".format(self
))
549 if self
.status
.startswith("closed"):
550 if self
.trade_type
== "short" and self
.action
== "buy" and self
.close_if_possible
:
551 self
.market
.ccxt
.close_margin_position(self
.amount
.currency
, self
.base_currency
)
554 if self
.market
.debug
:
555 self
.market
.report
.log_debug_action("Fetching {}".format(self
))
558 result
= self
.market
.ccxt
.fetch_order(self
.id)
559 self
.results
.append(result
)
560 self
.status
= result
["status"]
561 # Time at which the order started
562 self
.timestamp
= result
["datetime"]
563 except OrderNotCached
:
564 self
.status
= "closed_unknown"
566 self
.fetch_mouvements()
568 self
.mark_finished_order()
569 # FIXME: consider open order with dust remaining as closed
571 def dust_amount_remaining(self
):
572 return self
.remaining_amount() < Amount(self
.amount
.currency
, D("0.001"))
574 def remaining_amount(self
):
575 return self
.amount
- self
.filled_amount()
577 def filled_amount(self
, in_base_currency
=False):
578 if self
.status
== "open":
581 for mouvement
in self
.mouvements
:
583 filled_amount
+= mouvement
.total_in_base
585 filled_amount
+= mouvement
.total
588 def fetch_mouvements(self
):
590 mouvements
= self
.market
.ccxt
.privatePostReturnOrderTrades({"orderNumber": self.id}
)
591 except ExchangeError
:
595 for mouvement_hash
in mouvements
:
596 self
.mouvements
.append(Mouvement(self
.amount
.currency
,
597 self
.base_currency
, mouvement_hash
))
600 if self
.market
.debug
:
601 self
.market
.report
.log_debug_action("Mark {} as cancelled".format(self
))
602 self
.status
= "canceled"
604 if self
.open and self
.id is not None:
606 self
.market
.ccxt
.cancel_order(self
.id)
607 except OrderNotFound
as e
: # Closed inbetween
608 self
.market
.report
.log_error("cancel_order", message
="Already cancelled order", exception
=e
)
611 def retrieve_order(self
):
612 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
613 amount
= round(self
.amount
, self
.market
.ccxt
.order_precision(symbol
)).value
614 start_timestamp
= self
.start_date
.timestamp() - 5
616 similar_open_orders
= self
.market
.ccxt
.fetch_orders(symbol
=symbol
, since
=start_timestamp
)
617 for order
in similar_open_orders
:
618 if (order
["info"]["margin"] == 1 and self
.account
== "exchange") or\
619 (order
["info"]["margin"] != 1 and self
.account
== "margin"):
620 i_m_tested
= True # coverage bug ?!
622 if order
["info"]["side"] != self
.action
:
625 abs(D(order
["info"]["startingAmount"]) - amount
),
626 self
.market
.ccxt
.order_precision(symbol
))
628 abs(D(order
["info"]["rate"]) - self
.rate
),
629 self
.market
.ccxt
.order_precision(symbol
))
630 if amount_diff
!= 0 or rate_diff
!= 0:
632 self
.results
.append({"id": order["id"]}
)
635 similar_trades
= self
.market
.ccxt
.fetch_my_trades(symbol
=symbol
, since
=start_timestamp
)
636 # FIXME: use set instead of sorted(list(...))
637 for order_id
in sorted(list(map(lambda x
: x
["order"], similar_trades
))):
638 trades
= list(filter(lambda x
: x
["order"] == order_id
, similar_trades
))
639 if any(x
["timestamp"] < start_timestamp
for x
in trades
):
641 if any(x
["side"] != self
.action
for x
in trades
):
643 if any(x
["info"]["category"] == "exchange" and self
.account
== "margin" for x
in trades
) or\
644 any(x
["info"]["category"] == "marginTrade" and self
.account
== "exchange" for x
in trades
):
646 trade_sum
= sum(D(x
["info"]["amount"]) for x
in trades
)
647 amount_diff
= round(abs(trade_sum
- amount
),
648 self
.market
.ccxt
.order_precision(symbol
))
651 if (self
.action
== "sell" and any(D(x
["info"]["rate"]) < self
.rate
for x
in trades
)) or\
652 (self
.action
== "buy" and any(D(x
["info"]["rate"]) > self
.rate
for x
in trades
)):
654 self
.results
.append({"id": order_id}
)
660 def __init__(self
, currency
, base_currency
, hash_
):
661 self
.currency
= currency
662 self
.base_currency
= base_currency
663 self
.id = hash_
.get("tradeID")
664 self
.action
= hash_
.get("type")
665 self
.fee_rate
= D(hash_
.get("fee", -1))
667 self
.date
= datetime
.strptime(hash_
.get("date", ""), '%Y-%m-%d %H:%M:%S')
670 self
.rate
= D(hash_
.get("rate", 0))
671 self
.total
= Amount(currency
, hash_
.get("amount", 0))
672 # rate * total = total_in_base
673 self
.total_in_base
= Amount(base_currency
, hash_
.get("total", 0))
677 "fee_rate": self
.fee_rate
,
679 "action": self
.action
,
680 "total": self
.total
.value
,
681 "currency": self
.currency
,
682 "total_in_base": self
.total_in_base
.value
,
683 "base_currency": self
.base_currency
687 if self
.fee_rate
> 0:
688 fee_rate
= " fee: {}%".format(self
.fee_rate
* 100)
691 if self
.date
is None:
695 return "Mouvement({} ; {} {} ({}){})".format(
696 date
, self
.action
, self
.total
, self
.total_in_base
,