- self.market.cancel_order(self.result['id'])
-
-def print_orders(market, base_currency="BTC"):
- Balance.prepare_trades(market, base_currency=base_currency, compute_value="average")
- Trade.prepare_orders(compute_value="average")
- for currency, balance in Balance.known_balances.items():
- print(balance)
- portfolio.Trade.print_all_with_order()
-
-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()
-
-if __name__ == '__main__':
- print_orders(market)
+ if self.market.debug:
+ self.market.report.log_debug_action("Mark {} as cancelled".format(self))
+ self.status = "canceled"
+ return
+ if self.open and self.id is not None:
+ try:
+ self.market.ccxt.cancel_order(self.id)
+ except OrderNotFound as e: # Closed inbetween
+ self.market.report.log_error("cancel_order", message="Already cancelled order", exception=e)
+ self.fetch()
+
+ def retrieve_order(self):
+ symbol = "{}/{}".format(self.amount.currency, self.base_currency)
+ amount = round(self.amount, self.market.ccxt.order_precision(symbol)).value
+ start_timestamp = self.start_date.timestamp() - 5
+
+ similar_open_orders = self.market.ccxt.fetch_orders(symbol=symbol, since=start_timestamp)
+ for order in similar_open_orders:
+ if (order["info"]["margin"] == 1 and self.account == "exchange") or\
+ (order["info"]["margin"] != 1 and self.account == "margin"):
+ i_m_tested = True # coverage bug ?!
+ continue
+ if order["info"]["side"] != self.action:
+ continue
+ amount_diff = round(
+ abs(D(order["info"]["startingAmount"]) - amount),
+ self.market.ccxt.order_precision(symbol))
+ rate_diff = round(
+ abs(D(order["info"]["rate"]) - self.rate),
+ self.market.ccxt.order_precision(symbol))
+ if amount_diff != 0 or rate_diff != 0:
+ continue
+ self.results.append({"id": order["id"]})
+ return True
+
+ similar_trades = self.market.ccxt.fetch_my_trades(symbol=symbol, since=start_timestamp)
+ for order_id in sorted(list(map(lambda x: x["order"], similar_trades))):
+ trades = list(filter(lambda x: x["order"] == order_id, similar_trades))
+ if any(x["timestamp"] < start_timestamp for x in trades):
+ continue
+ if any(x["side"] != self.action for x in trades):
+ continue
+ if any(x["info"]["category"] == "exchange" and self.account == "margin" for x in trades) or\
+ any(x["info"]["category"] == "marginTrade" and self.account == "exchange" for x in trades):
+ continue
+ trade_sum = sum(D(x["info"]["amount"]) for x in trades)
+ amount_diff = round(abs(trade_sum - amount),
+ self.market.ccxt.order_precision(symbol))
+ if amount_diff != 0:
+ continue
+ if (self.action == "sell" and any(D(x["info"]["rate"]) < self.rate for x in trades)) or\
+ (self.action == "buy" and any(D(x["info"]["rate"]) > self.rate for x in trades)):
+ continue
+ self.results.append({"id": order_id})
+ return True
+
+ return False
+
+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.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)
+