X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git;a=blobdiff_plain;f=helper.py;h=8f726d5a831b25c12929af9ff8e1b9e5c5a98979;hp=95b233ade5fecdc1196ca5b14feae61f94049ddd;hb=66f1aed55a17d65e0b5993c59688769dcc2b7b58;hpb=17598517c544a3dda8b9f773dfeb669c886ea92b diff --git a/helper.py b/helper.py index 95b233a..8f726d5 100644 --- a/helper.py +++ b/helper.py @@ -202,7 +202,7 @@ class Processor: "before": True, "after": False, "fetch_balances": ["begin", "end"], - "prepare_trades": { "repartition": { "BTC": (1, "long") } }, + "prepare_trades": { "repartition": { "base_currency": (1, "long") } }, "prepare_orders": { "compute_value": "average" }, "run_orders": {}, "follow_orders": {}, @@ -226,18 +226,15 @@ class Processor: "move_balances": {}, "run_orders": {}, "follow_orders": {}, + "close_trades": {}, }, ] } - allowed_arguments = { - "wait_for_recent": ["delta"], - "prepare_trades": ["base_currency", "liquidity", "compute_value", "repartition", "only"], - "prepare_orders": ["only", "compute_value"], - "move_balances": [], - "run_orders": [], - "follow_orders": ["sleep"], - } + ordered_actions = [ + "wait_for_recent", "prepare_trades", "prepare_orders", + "move_balances", "run_orders", "follow_orders", + "close_trades"] def __init__(self, market): self.market = market @@ -264,57 +261,60 @@ class Processor: for step in steps: selected_steps += self.select_steps(scenario, step) for step in selected_steps: - self.process_step(scenario_name, step, **kwargs) + self.process_step(scenario_name, step, kwargs) - def process_step(self, scenario_name, step, **kwargs): + def process_step(self, scenario_name, step, kwargs): process_name = "process_{}__{}_{}".format(scenario_name, step["number"], step["name"]) self.market.report.log_stage("{}_begin".format(process_name)) if "begin" in step.get("fetch_balances", []): self.market.balances.fetch_balances(tag="{}_begin".format(process_name)) - for action in ["wait_for_recent", "prepare_trades", - "prepare_orders", "move_balances", "run_orders", - "follow_orders"]: + for action in self.ordered_actions: if action in step: - self.run_action(action, step[action], **kwargs) + self.run_action(action, step[action], kwargs) if "end" in step.get("fetch_balances", []): self.market.balances.fetch_balances(tag="{}_end".format(process_name)) self.market.report.log_stage("{}_end".format(process_name)) - def run_action(self, action, default_args, **kwargs): - args = {k: v for k, v in {**default_args, **kwargs}.items() if k in self.allowed_arguments[action] } + def method_arguments(self, action): + import inspect if action == "wait_for_recent": - portfolio.Portfolio.wait_for_recent(self.market, **args) - if action == "prepare_trades": - self.market.prepare_trades(**args) - if action == "prepare_orders": - self.market.trades.prepare_orders(**args) - if action == "move_balances": - self.market.move_balances(**args) - if action == "run_orders": - self.market.trades.run_orders(**args) - if action == "follow_orders": - self.market.follow_orders(**args) - -def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC"): - Processor(market).process("sell_needed", steps="sell", - liquidity=liquidity, base_currency=base_currency) - -def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC"): - Processor(market).process("sell_needed", steps="buy", - liquidity=liquidity, base_currency=base_currency) - -def process_sell_all__1_all_sell(market, base_currency="BTC", liquidity="medium"): - Processor(market).process("sell_all", steps="all_sell", - liquidity=liquidity, base_currency=base_currency) - -def process_sell_all__2_wait(market, liquidity="medium", base_currency="BTC"): - Processor(market).process("sell_all", steps="wait", - liquidity=liquidity, base_currency=base_currency) - -def process_sell_all__3_all_buy(market, base_currency="BTC", liquidity="medium"): - Processor(market).process("sell_all", steps="all_buy", - liquidity=liquidity, base_currency=base_currency) + method = portfolio.Portfolio.wait_for_recent + elif action == "prepare_trades": + method = self.market.prepare_trades + elif action == "prepare_orders": + method = self.market.trades.prepare_orders + elif action == "move_balances": + method = self.market.move_balances + elif action == "run_orders": + method = self.market.trades.run_orders + elif action == "follow_orders": + method = self.market.follow_orders + elif action == "close_trades": + method = self.market.trades.close_trades + + signature = inspect.getfullargspec(method) + defaults = signature.defaults or [] + kwargs = signature.args[-len(defaults):] + + return [method, kwargs] + + def parse_args(self, action, default_args, kwargs): + method, allowed_arguments = self.method_arguments(action) + args = {k: v for k, v in {**default_args, **kwargs}.items() if k in allowed_arguments } + + if "repartition" in args and "base_currency" in args["repartition"]: + r = args["repartition"] + r[args.get("base_currency", "BTC")] = r.pop("base_currency") + + return method, args + + def run_action(self, action, default_args, kwargs): + method, args = self.parse_args(action, default_args, kwargs) + if action == "wait_for_recent": + method(self.market, **args) + else: + method(**args)