+ def prepare_order(self, close_if_possible=None, compute_value="default"):
+ if self.action is None:
+ return None
+ ticker = self.market.get_ticker(self.currency, self.base_currency)
+ self.inverted = ticker["inverted"]
+ if self.inverted:
+ ticker = ticker["original"]
+ rate = Computation.compute_value(ticker, self.order_action(), compute_value=compute_value)
+
+ # FIXME: Dust amount should be removed from there if they werent
+ # honored in other sales
+ delta_in_base = abs(self.delta)
+ # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case)
+
+ if not self.inverted:
+ base_currency = self.base_currency
+ # BTC
+ if self.action == "dispose":
+ filled = self.filled_amount(in_base_currency=False)
+ delta = delta_in_base.in_currency(self.currency, self.market, rate=1/self.value_from.rate)
+ # I have 10 BTC worth of FOO, and I want to sell 9 BTC
+ # worth of it, computed first with rate 10 FOO = 1 BTC.
+ # -> I "sell" "90" FOO at proposed rate "rate".
+
+ delta = delta - filled
+ # I already sold 60 FOO, 30 left
+ else:
+ filled = self.filled_amount(in_base_currency=True)
+ delta = (delta_in_base - filled).in_currency(self.currency, self.market, rate=1/rate)
+ # I want to buy 9 BTC worth of FOO, computed with rate
+ # 10 FOO = 1 BTC
+ # -> I "buy" "9 / rate" FOO at proposed rate "rate"
+
+ # I already bought 3 / rate FOO, 6 / rate left
+ else:
+ base_currency = self.currency
+ # FOO
+ if self.action == "dispose":
+ filled = self.filled_amount(in_base_currency=True)
+ # Base is FOO
+
+ delta = (delta_in_base.in_currency(self.currency, self.market, rate=1/self.value_from.rate)
+ - filled).in_currency(self.base_currency, self.market, rate=1/rate)
+ # I have 10 BTC worth of FOO, and I want to sell 9 BTC worth of it
+ # computed at rate 1 Foo = 0.01 BTC
+ # Computation says I should sell it at 125 FOO / BTC
+ # -> delta_in_base = 9 BTC
+ # -> delta = (9 * 1/0.01 FOO) * 1/125 = 7.2 BTC
+ # Action: "buy" "7.2 BTC" at rate "125" "FOO" on market
+
+ # I already bought 300/125 BTC, only 600/125 left
+ else:
+ filled = self.filled_amount(in_base_currency=False)
+ # Base is FOO
+
+ delta = delta_in_base
+ # I have 1 BTC worth of FOO, and I want to buy 9 BTC worth of it
+ # At rate 100 Foo / BTC
+ # Computation says I should buy it at 125 FOO / BTC
+ # -> delta_in_base = 9 BTC
+ # Action: "sell" "9 BTC" at rate "125" "FOO" on market
+
+ delta = delta - filled
+ # I already sold 4 BTC, only 5 left
+
+ if close_if_possible is None:
+ close_if_possible = (self.value_to == 0)
+
+ if delta <= 0:
+ self.market.report.log_error("prepare_order", message="Less to do than already filled: {}".format(delta))
+ return None
+
+ order = Order(self.order_action(),
+ delta, rate, base_currency, self.trade_type,
+ self.market, self, close_if_possible=close_if_possible)
+ self.orders.append(order)
+ return order
+
+ def as_json(self):
+ return {
+ "action": self.action,
+ "from": self.value_from.as_json()["value"],
+ "to": self.value_to.as_json()["value"],
+ "currency": self.currency,
+ "base_currency": self.base_currency,
+ }