def move_balances(market, debug=False):
needed_in_margin = {}
+ moving_to_margin = {}
+
for currency in BalanceStore.all:
if BalanceStore.all[currency].margin_free != 0:
needed_in_margin[currency] = 0
needed_in_margin[trade.value_to.currency] += abs(trade.value_to)
for currency, needed in needed_in_margin.items():
current_balance = BalanceStore.all[currency].margin_free
- delta = (needed - current_balance).value
- # FIXME: don't remove too much if there are open margin position
+ moving_to_margin[currency] = (needed - current_balance)
+ delta = moving_to_margin[currency].value
+ if debug:
+ ReportStore.log_debug_action("Moving {} from exchange to margin".format(moving_to_margin[currency]))
+ continue
if delta > 0:
- if debug:
- print("market.transfer_balance({}, {}, 'exchange', 'margin')".format(currency, delta))
- else:
- market.transfer_balance(currency, delta, "exchange", "margin")
+ 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")
+ market.transfer_balance(currency, -delta, "margin", "exchange")
+ ReportStore.log_move_balances(needed_in_margin, moving_to_margin, debug)
BalanceStore.fetch_balances(market)
fees_cache[market.__class__] = market.fetch_fees()
return fees_cache[market.__class__]
-def prepare_trades(market, base_currency="BTC", compute_value="average", debug=False):
- BalanceStore.fetch_balances(market)
+def prepare_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", debug=False):
+ ReportStore.log_stage("prepare_trades")
values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
total_base_value = sum(values_in_base.values())
- new_repartition = BalanceStore.dispatch_assets(total_base_value)
+ new_repartition = BalanceStore.dispatch_assets(total_base_value, liquidity=liquidity)
# Recompute it in case we have new currencies
values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug)
-def update_trades(market, base_currency="BTC", compute_value="average", only=None, debug=False):
- BalanceStore.fetch_balances(market)
+def update_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", only=None, debug=False):
+ ReportStore.log_stage("update_trades")
values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
total_base_value = sum(values_in_base.values())
- new_repartition = BalanceStore.dispatch_assets(total_base_value)
+ new_repartition = BalanceStore.dispatch_assets(total_base_value, liquidity=liquidity)
TradeStore.compute_trades(values_in_base, new_repartition, only=only, market=market, debug=debug)
def prepare_trades_to_sell_all(market, base_currency="BTC", compute_value="average", debug=False):
- BalanceStore.fetch_balances(market)
+ ReportStore.log_stage("prepare_trades_to_sell_all")
values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value)
total_base_value = sum(values_in_base.values())
new_repartition = BalanceStore.dispatch_assets(total_base_value, repartition={ base_currency: (1, "long") })
TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug)
-def follow_orders(verbose=True, sleep=None):
+def follow_orders(sleep=None):
if sleep is None:
sleep = 7 if TradeStore.debug else 30
+ if TradeStore.debug:
+ ReportStore.log_debug_action("Set follow_orders tick to {}s".format(sleep))
tick = 0
+ ReportStore.log_stage("follow_orders_begin")
while len(TradeStore.all_orders(state="open")) > 0:
time.sleep(sleep)
tick += 1
- for order in TradeStore.all_orders(state="open"):
+ open_orders = TradeStore.all_orders(state="open")
+ ReportStore.log_stage("follow_orders_tick_{}".format(tick))
+ ReportStore.log_orders(open_orders, tick=tick)
+ for order in open_orders:
if order.get_status() != "open":
- if verbose:
- print("finished {}".format(order))
+ ReportStore.log_order(order, tick, finished=True)
else:
order.trade.update_order(order, tick)
- if verbose:
- print("All orders finished")
+ ReportStore.log_stage("follow_orders_end")
def print_orders(market, base_currency="BTC"):
+ ReportStore.log_stage("print_orders")
+ BalanceStore.fetch_balances(market, tag="print_orders")
prepare_trades(market, base_currency=base_currency, compute_value="average", debug=True)
TradeStore.prepare_orders(compute_value="average")
- for currency, balance in BalanceStore.all.items():
- print(balance)
- TradeStore.print_all_with_order()
def print_balances(market, base_currency="BTC"):
BalanceStore.fetch_balances(market)
- for currency, balance in BalanceStore.all.items():
- print(balance)
if base_currency is not None:
- print("total:")
- print(sum(BalanceStore.in_currency(base_currency, market).values()))
+ ReportStore.print_log("total:")
+ ReportStore.print_log(sum(BalanceStore.in_currency(base_currency, market).values()))
-def process_sell_needed__1_sell(market, base_currency="BTC", debug=False):
- prepare_trades(market, base_currency=base_currency, debug=debug)
+def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC", debug=False):
+ ReportStore.log_stage("process_sell_needed__1_sell_begin")
+ BalanceStore.fetch_balances(market, tag="process_sell_needed__1_sell_begin")
+ prepare_trades(market, liquidity=liquidity, base_currency=base_currency, debug=debug)
TradeStore.prepare_orders(compute_value="average", only="dispose")
- print("------------------")
- for currency, balance in BalanceStore.all.items():
- print(balance)
- print("------------------")
- TradeStore.print_all_with_order()
- print("------------------")
TradeStore.run_orders()
follow_orders()
+ BalanceStore.fetch_balances(market, tag="process_sell_needed__1_sell_end")
+ ReportStore.log_stage("process_sell_needed__1_sell_end")
-def process_sell_needed__2_buy(market, base_currency="BTC", debug=False):
- update_trades(market, base_currency=base_currency, debug=debug, only="acquire")
+def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC", debug=False):
+ ReportStore.log_stage("process_sell_needed__2_buy_begin")
+ BalanceStore.fetch_balances(market, tag="process_sell_needed__2_buy_begin")
+ update_trades(market, base_currency=base_currency, liquidity=liquidity, debug=debug, only="acquire")
TradeStore.prepare_orders(compute_value="average", only="acquire")
- print("------------------")
- for currency, balance in BalanceStore.all.items():
- print(balance)
- print("------------------")
- TradeStore.print_all_with_order()
- print("------------------")
move_balances(market, debug=debug)
TradeStore.run_orders()
follow_orders()
+ BalanceStore.fetch_balances(market, tag="process_sell_needed__2_buy_end")
+ ReportStore.log_stage("process_sell_needed__2_buy_end")
-def process_sell_all__1_all_sell(market, base_currency="BTC", debug=False):
+def process_sell_all__1_all_sell(market, base_currency="BTC", debug=False, liquidity="medium"):
+ ReportStore.log_stage("process_sell_all__1_all_sell_begin")
+ BalanceStore.fetch_balances(market, tag="process_sell_all__1_all_sell_begin")
prepare_trades_to_sell_all(market, base_currency=base_currency, debug=debug)
TradeStore.prepare_orders(compute_value="average")
- print("------------------")
- for currency, balance in BalanceStore.all.items():
- print(balance)
- print("------------------")
- TradeStore.print_all_with_order()
- print("------------------")
TradeStore.run_orders()
follow_orders()
+ BalanceStore.fetch_balances(market, tag="process_sell_all__1_all_sell_end")
+ ReportStore.log_stage("process_sell_all__1_all_sell_end")
-def process_sell_all__2_all_buy(market, base_currency="BTC", debug=False):
- prepare_trades(market, base_currency=base_currency, debug=debug)
- TradeStore.prepare_orders()
- print("------------------")
- for currency, balance in BalanceStore.all.items():
- print(balance)
- print("------------------")
- TradeStore.print_all_with_order()
- print("------------------")
+def process_sell_all__2_all_buy(market, base_currency="BTC", debug=False, liquidity="medium"):
+ ReportStore.log_stage("process_sell_all__2_all_buy_begin")
+ BalanceStore.fetch_balances(market, tag="process_sell_all__2_all_buy_begin")
+ prepare_trades(market, liquidity=liquidity, base_currency=base_currency, debug=debug)
+ TradeStore.prepare_orders(compute_value="average")
move_balances(market, debug=debug)
TradeStore.run_orders()
follow_orders()
+ BalanceStore.fetch_balances(market, tag="process_sell_all__2_all_buy_end")
+ ReportStore.log_stage("process_sell_all__2_all_buy_end")