-@static_var("cache", {})
-def fetch_fees(market):
- if market.__class__ not in fetch_fees.cache:
- fetch_fees.cache[market.__class__] = market.fetch_fees()
- return fetch_fees.cache[market.__class__]
-
-def print_orders(market, base_currency="BTC"):
- Balance.prepare_trades(market, base_currency=base_currency)
- for currency, trade in Trade.trades.items():
- print(trade)
- for order in trade.orders:
- print("\t", order, sep="")
-
-def make_orders(market, base_currency="BTC"):
- Balance.prepare_trades(market, base_currency=base_currency)
- for currency, trade in Trade.trades.items():
- print(trade)
- for order in trade.orders:
- print("\t", order, sep="")
- order.run(market)
-
-if __name__ == '__main__':
- print_orders(market)
+ def mark_finished_order(self):
+ if TradeStore.debug:
+ ReportStore.log_debug_action("Mark {} as finished".format(self))
+ return
+ if self.status == "closed":
+ if self.trade_type == "short" and self.action == "buy" and self.close_if_possible:
+ self.market.close_margin_position(self.amount.currency, self.base_currency)
+
+ def fetch(self, force=False):
+ if TradeStore.debug:
+ ReportStore.log_debug_action("Fetching {}".format(self))
+ return
+ if (not force and self.fetch_cache_timestamp is not None
+ and time.time() - self.fetch_cache_timestamp < 10):
+ return
+ self.fetch_cache_timestamp = time.time()
+
+ result = self.market.fetch_order(self.id)
+ self.results.append(result)
+
+ 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:
+ ReportStore.log_debug_action("Mark {} as cancelled".format(self))
+ 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))
+
+ def as_json(self):
+ return {
+ "fee_rate": self.fee_rate,
+ "date": self.date,
+ "action": self.action,
+ "total": self.total.value,
+ "currency": self.currency,
+ "total_in_base": self.total_in_base.value,
+ "base_currency": self.base_currency
+ }
+
+ def __repr__(self):
+ if self.fee_rate > 0:
+ fee_rate = " fee: {}%".format(self.fee_rate * 100)
+ else:
+ fee_rate = ""
+ if self.date is None:
+ date = "No date"
+ else:
+ date = self.date
+ return "Mouvement({} ; {} {} ({}){})".format(
+ date, self.action, self.total, self.total_in_base,
+ fee_rate)
+
+if __name__ == '__main__': # pragma: no cover
+ from market import market
+ h.print_orders(market)