X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=helper.py;h=fa92ac71ab631d78f36ffeacc4ad1d94bfecb63f;hb=eb9c92e155941b51042ba57e23f651454bd8e55a;hp=3e39785d4bb1a75d5bde324403161ddc3dcc053a;hpb=97922ff1826cc6b9c0329cc30e8d4621bb2644ee;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/helper.py b/helper.py index 3e39785..fa92ac7 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) @@ -72,108 +72,107 @@ def fetch_fees(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())) - -def process_sell_needed__1_sell(market, base_currency="BTC", debug=False): - prepare_trades(market, base_currency=base_currency, debug=debug) + ReportStore.print_log("total:") + ReportStore.print_log(sum(BalanceStore.in_currency(base_currency, market).values())) + +def reset_all(): + # use them as regular classes, sub-object of market + ReportStore.logs = [] + BalanceStore.all = {} + TradeStore.all = [] + +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")