X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=helper.py;h=87539f19e93f03cb0592e51b4b29a8d20f92d9ac;hb=3d0247f944d7510943dfaa64eeb0e15a43b6c989;hp=23b2d0be4654cc483b4c81eb756b25f3cbe7c7ef;hpb=c31df868c655612b8387a25111e69882f0fe6344;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/helper.py b/helper.py index 23b2d0b..87539f1 100644 --- a/helper.py +++ b/helper.py @@ -4,6 +4,8 @@ from store import * 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 @@ -14,18 +16,16 @@ def move_balances(market, debug=False): 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) @@ -73,6 +73,7 @@ def fetch_fees(market): return fees_cache[market.__class__] def prepare_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", debug=False): + ReportStore.log_stage("prepare_trades") BalanceStore.fetch_balances(market) values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value) total_base_value = sum(values_in_base.values()) @@ -82,6 +83,7 @@ def prepare_trades(market, base_currency="BTC", liquidity="medium", compute_valu TradeStore.compute_trades(values_in_base, new_repartition, market=market, debug=debug) def update_trades(market, base_currency="BTC", liquidity="medium", compute_value="average", only=None, debug=False): + ReportStore.log_stage("update_trades") BalanceStore.fetch_balances(market) values_in_base = BalanceStore.in_currency(base_currency, market, compute_value=compute_value) total_base_value = sum(values_in_base.values()) @@ -89,91 +91,76 @@ def update_trades(market, base_currency="BTC", liquidity="medium", compute_value 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): + ReportStore.log_stage("prepare_trades_to_sell_all") BalanceStore.fetch_balances(market) 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") 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, liquidity="medium", base_currency="BTC", debug=False): + ReportStore.log_stage("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() + ReportStore.log_stage("process_sell_needed__1_sell_end") def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC", debug=False): + ReportStore.log_stage("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() + ReportStore.log_stage("process_sell_needed__2_buy_end") 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") 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() + ReportStore.log_stage("process_sell_all__1_all_sell_end") 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") prepare_trades(market, liquidity=liquidity, 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("------------------") move_balances(market, debug=debug) TradeStore.run_orders() follow_orders() + ReportStore.log_stage("process_sell_all__2_all_buy_end")