+ self.status = result["status"]
+ # Time at which the order started
+ self.timestamp = result["datetime"]
+ self.fetch_mouvements()
+
+ # FIXME: consider open order with dust remaining as closed
+
+ def dust_amount_remaining(self):
+ return self.remaining_amount() < Amount(self.amount.currency, D("0.001"))
+
+ def remaining_amount(self):
+ if self.status == "open":
+ self.fetch()
+ return self.amount - self.filled_amount()
+
+ def filled_amount(self, in_base_currency=False):
+ if self.status == "open":
+ self.fetch()
+ filled_amount = 0
+ for mouvement in self.mouvements:
+ if in_base_currency:
+ filled_amount += mouvement.total_in_base
+ else:
+ filled_amount += mouvement.total
+ return filled_amount
+
+ def fetch_mouvements(self):
+ try:
+ mouvements = self.market.privatePostReturnOrderTrades({"orderNumber": self.id})
+ except ExchangeError:
+ mouvements = []
+ self.mouvements = []
+
+ for mouvement_hash in mouvements:
+ self.mouvements.append(Mouvement(self.amount.currency,
+ self.base_currency, mouvement_hash))
+
+ def cancel(self):
+ if TradeStore.debug:
+ self.status = "canceled"
+ return
+ self.market.cancel_order(self.id)
+ self.fetch()
+
+class Mouvement:
+ def __init__(self, currency, base_currency, hash_):
+ self.currency = currency
+ self.base_currency = base_currency
+ self.id = hash_.get("tradeID")
+ self.action = hash_.get("type")
+ self.fee_rate = D(hash_.get("fee", -1))
+ try:
+ self.date = datetime.strptime(hash_.get("date", ""), '%Y-%m-%d %H:%M:%S')
+ except ValueError:
+ self.date = None
+ self.rate = D(hash_.get("rate", 0))
+ self.total = Amount(currency, hash_.get("amount", 0))
+ # rate * total = total_in_base
+ self.total_in_base = Amount(base_currency, hash_.get("total", 0))
+
+if __name__ == '__main__': # pragma: no cover
+ from market import market
+ h.print_orders(market)