+ @classmethod
+ def prepare_orders(cls, only=None, compute_value="default"):
+ for trade in cls.trades:
+ if only is None or trade.action == only:
+ trade.prepare_order(compute_value=compute_value)
+
+ @classmethod
+ def move_balances(cls, market, debug=False):
+ needed_in_margin = {}
+ for trade in cls.trades:
+ if trade.trade_type == "short":
+ if trade.value_to.currency not in needed_in_margin:
+ needed_in_margin[trade.value_to.currency] = 0
+ needed_in_margin[trade.value_to.currency] += abs(trade.value_to)
+ for currency, needed in needed_in_margin.items():
+ current_balance = Balance.known_balances[currency].margin_free
+ delta = (needed - current_balance).value
+ # FIXME: don't remove too much if there are open margin position
+ if delta > 0:
+ if debug:
+ print("market.transfer_balance({}, {}, 'exchange', 'margin')".format(currency, delta))
+ else:
+ market.transfer_balance(currency, delta, "exchange", "margin")
+ elif delta < 0:
+ if debug:
+ print("market.transfer_balance({}, {}, 'margin', 'exchange')".format(currency, -delta))
+ else:
+ market.transfer_balance(currency, -delta, "margin", "exchange")
+