+ def print_tickers(self, base_currency="BTC"):
+ if base_currency is not None:
+ self.report.print_log("total:")
+ self.report.print_log(sum(self.balances.in_currency(base_currency).values()))
+
+class Processor:
+ scenarios = {
+ "wait_for_cryptoportfolio": [
+ {
+ "name": "wait",
+ "number": 1,
+ "before": False,
+ "after": True,
+ "wait_for_recent": {},
+ },
+ ],
+ "print_balances": [
+ {
+ "name": "print_balances",
+ "number": 1,
+ "fetch_balances": ["begin"],
+ "print_tickers": { "base_currency": "BTC" },
+ }
+ ],
+ "print_orders": [
+ {
+ "name": "wait",
+ "number": 1,
+ "before": True,
+ "after": False,
+ "wait_for_recent": {},
+ },
+ {
+ "name": "make_orders",
+ "number": 2,
+ "before": False,
+ "after": True,
+ "fetch_balances": ["begin"],
+ "prepare_trades": { "compute_value": "average" },
+ "prepare_orders": { "compute_value": "average" },
+ },
+ ],
+ "sell_needed": [
+ {
+ "name": "wait",
+ "number": 0,
+ "before": False,
+ "after": True,
+ "wait_for_recent": {},
+ },
+ {
+ "name": "sell",
+ "number": 1,
+ "before": False,
+ "after": True,
+ "fetch_balances": ["begin", "end"],
+ "prepare_trades": {},
+ "prepare_orders": { "only": "dispose", "compute_value": "average" },
+ "run_orders": {},
+ "follow_orders": {},
+ "close_trades": {},
+ },
+ {
+ "name": "buy",
+ "number": 2,
+ "before": False,
+ "after": True,
+ "fetch_balances": ["begin", "end"],
+ "prepare_trades": { "only": "acquire", "available_balance_only": True },
+ "prepare_orders": { "only": "acquire", "compute_value": "average" },
+ "move_balances": {},
+ "run_orders": {},
+ "follow_orders": {},
+ "close_trades": {},
+ },
+ ],
+ "sell_all": [
+ {
+ "name": "all_sell",
+ "number": 1,
+ "before": True,
+ "after": False,
+ "fetch_balances": ["begin", "end"],
+ "prepare_trades": { "repartition": { "base_currency": (1, "long") } },
+ "prepare_orders": { "compute_value": "average" },
+ "run_orders": {},
+ "follow_orders": {},
+ "close_trades": {},
+ },
+ {
+ "name": "wait",
+ "number": 2,
+ "before": False,
+ "after": True,
+ "wait_for_recent": {},
+ },
+ {
+ "name": "all_buy",
+ "number": 3,
+ "before": False,
+ "after": True,
+ "fetch_balances": ["begin", "end"],
+ "prepare_trades": { "available_balance_only": True },
+ "prepare_orders": { "compute_value": "average" },
+ "move_balances": {},
+ "run_orders": {},
+ "follow_orders": {},
+ "close_trades": {},
+ },
+ ]
+ }
+
+ ordered_actions = [
+ "wait_for_recent", "prepare_trades", "prepare_orders",
+ "move_balances", "run_orders", "follow_orders",
+ "close_trades", "print_tickers"]
+
+ def __init__(self, market):
+ self.market = market
+
+ def select_steps(self, scenario, step):
+ if step == "all":
+ return scenario
+ elif step == "before" or step == "after":
+ return list(filter(lambda x: x.get(step, False), scenario))
+ elif type(step) == int:
+ return [scenario[step-1]]
+ elif type(step) == str:
+ return list(filter(lambda x: x["name"] == step, scenario))
+ else:
+ raise TypeError("Unknown step {}".format(step))
+
+ def can_process(self, scenario_name):
+ return scenario_name in self.scenarios
+
+ def process(self, scenario_name, steps="all", **kwargs):
+ if not self.can_process(scenario_name):
+ raise TypeError("Unknown scenario {}".format(scenario_name))
+ scenario = self.scenarios[scenario_name]
+ selected_steps = []
+
+ if type(steps) == str or type(steps) == int:
+ selected_steps += self.select_steps(scenario, steps)
+ else:
+ for step in steps:
+ selected_steps += self.select_steps(scenario, step)
+ for step in selected_steps:
+ self.process_step(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 self.ordered_actions:
+ if action in step:
+ 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 method_arguments(self, action):
+ import inspect
+
+ if action == "wait_for_recent":
+ method = 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
+ elif action == "print_tickers":
+ method = self.market.print_tickers
+
+ 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