]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Move helper methods to their due places
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 5 Mar 2018 23:21:52 +0000 (00:21 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 5 Mar 2018 23:21:52 +0000 (00:21 +0100)
helper.py [deleted file]
main.py
market.py
test.py

diff --git a/helper.py b/helper.py
deleted file mode 100644 (file)
index 8f726d5..0000000
--- a/helper.py
+++ /dev/null
@@ -1,320 +0,0 @@
-from datetime import datetime
-import argparse
-import configparser
-import psycopg2
-import os
-import sys
-
-import portfolio
-
-def make_order(market, value, currency, action="acquire",
-        close_if_possible=False, base_currency="BTC", follow=True,
-        compute_value="average"):
-    """
-    Make an order on market
-    "market": The market on which to place the order
-    "value": The value in *base_currency* to acquire,
-             or in *currency* to dispose.
-             use negative for margin trade.
-    "action": "acquire" or "dispose".
-                "acquire" will buy long or sell short,
-                "dispose" will sell long or buy short.
-    "currency": The currency to acquire or dispose
-    "base_currency": The base currency. The value is expressed in that
-                     currency (default: BTC)
-    "follow": Whether to follow the order once run (default: True)
-    "close_if_possible": Whether to try to close the position at the end
-                         of the trade, i.e. reach exactly 0 at the end
-                         (only meaningful in "dispose"). May have
-                         unwanted effects if the end value of the
-                         currency is not 0.
-    "compute_value": Compute value to place the order
-    """
-    market.report.log_stage("make_order_begin")
-    market.balances.fetch_balances(tag="make_order_begin")
-    if action == "acquire":
-        trade = portfolio.Trade(
-                portfolio.Amount(base_currency, 0),
-                portfolio.Amount(base_currency, value),
-                currency, market)
-    else:
-        amount = portfolio.Amount(currency, value)
-        trade = portfolio.Trade(
-                amount.in_currency(base_currency, market, compute_value=compute_value),
-                portfolio.Amount(base_currency, 0),
-                currency, market)
-    market.trades.all.append(trade)
-    order = trade.prepare_order(
-            close_if_possible=close_if_possible,
-            compute_value=compute_value)
-    market.report.log_orders([order], None, compute_value)
-    market.trades.run_orders()
-    if follow:
-        market.follow_orders()
-        market.balances.fetch_balances(tag="make_order_end")
-    else:
-        market.report.log_stage("make_order_end_not_followed")
-        return order
-    market.report.log_stage("make_order_end")
-
-def get_user_market(config_path, user_id, debug=False):
-    import market
-    pg_config, report_path = main_parse_config(config_path)
-    market_config = list(main_fetch_markets(pg_config, str(user_id)))[0][0]
-    return market.Market.from_config(market_config, debug=debug)
-
-def main_parse_args(argv):
-    parser = argparse.ArgumentParser(
-            description="Run the trade bot")
-
-    parser.add_argument("-c", "--config",
-            default="config.ini",
-            required=False,
-            help="Config file to load (default: config.ini)")
-    parser.add_argument("--before",
-            default=False, action='store_const', const=True,
-            help="Run the steps before the cryptoportfolio update")
-    parser.add_argument("--after",
-            default=False, action='store_const', const=True,
-            help="Run the steps after the cryptoportfolio update")
-    parser.add_argument("--debug",
-            default=False, action='store_const', const=True,
-            help="Run in debug mode")
-    parser.add_argument("--user",
-            default=None, required=False, help="Only run for that user")
-    parser.add_argument("--action",
-            action='append',
-            help="Do a different action than trading (add several times to chain)")
-
-    args = parser.parse_args(argv)
-
-    if not os.path.exists(args.config):
-        print("no config file found, exiting")
-        sys.exit(1)
-
-    return args
-
-def main_parse_config(config_file):
-    config = configparser.ConfigParser()
-    config.read(config_file)
-
-    if "postgresql" not in config:
-        print("no configuration for postgresql in config file")
-        sys.exit(1)
-
-    if "app" in config and "report_path" in config["app"]:
-        report_path = config["app"]["report_path"]
-
-        if not os.path.exists(report_path):
-            os.makedirs(report_path)
-    else:
-        report_path = None
-
-    return [config["postgresql"], report_path]
-
-def main_fetch_markets(pg_config, user):
-    connection = psycopg2.connect(**pg_config)
-    cursor = connection.cursor()
-
-    if user is None:
-        cursor.execute("SELECT config,user_id FROM market_configs")
-    else:
-        cursor.execute("SELECT config,user_id FROM market_configs WHERE user_id = %s", user)
-
-    for row in cursor:
-        yield row
-
-def main_process_market(user_market, actions, before=False, after=False):
-    if len(actions or []) == 0:
-        if before:
-            Processor(user_market).process("sell_all", steps="before")
-        if after:
-            Processor(user_market).process("sell_all", steps="after")
-    else:
-        for action in actions:
-            if action in globals():
-                (globals()[action])(user_market)
-            else:
-                raise NotImplementedError("Unknown action {}".format(action))
-
-def main_store_report(report_path, user_id, user_market):
-    try:
-        if report_path is not None:
-            report_file = "{}/{}_{}.json".format(report_path, datetime.now().isoformat(), user_id)
-            with open(report_file, "w") as f:
-                f.write(user_market.report.to_json())
-    except Exception as e:
-        print("impossible to store report file: {}; {}".format(e.__class__.__name__, e))
-
-def print_orders(market, base_currency="BTC"):
-    market.report.log_stage("print_orders")
-    market.balances.fetch_balances(tag="print_orders")
-    market.prepare_trades(base_currency=base_currency, compute_value="average")
-    market.trades.prepare_orders(compute_value="average")
-
-def print_balances(market, base_currency="BTC"):
-    market.report.log_stage("print_balances")
-    market.balances.fetch_balances()
-    if base_currency is not None:
-        market.report.print_log("total:")
-        market.report.print_log(sum(market.balances.in_currency(base_currency).values()))
-
-class Processor:
-    scenarios = {
-            "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" },
-                    "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": {},
-                    "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"]
-
-    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: step in x and x[step], 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 process(self, scenario_name, steps="all", **kwargs):
-        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.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)
diff --git a/main.py b/main.py
index d4bab0296855383ff439341b87ab70f45297fc00..dcbc2a34c197e6540d8420cfb219d8152bdfa8c6 100644 (file)
--- a/main.py
+++ b/main.py
+from datetime import datetime
+import argparse
+import configparser
+import psycopg2
+import os
 import sys
-import helper, market
 
-args = helper.main_parse_args(sys.argv[1:])
+import market
+import portfolio
 
-pg_config, report_path = helper.main_parse_config(args.config)
+__all__ = ["make_order", "get_user_market"]
 
-for market_config, user_id in helper.main_fetch_markets(pg_config, args.user):
-    try:
-        user_market = market.Market.from_config(market_config, debug=args.debug)
-        helper.main_process_market(user_market, args.action, before=args.before, after=args.after)
-    except Exception as e:
+def make_order(market, value, currency, action="acquire",
+        close_if_possible=False, base_currency="BTC", follow=True,
+        compute_value="average"):
+    """
+    Make an order on market
+    "market": The market on which to place the order
+    "value": The value in *base_currency* to acquire,
+             or in *currency* to dispose.
+             use negative for margin trade.
+    "action": "acquire" or "dispose".
+                "acquire" will buy long or sell short,
+                "dispose" will sell long or buy short.
+    "currency": The currency to acquire or dispose
+    "base_currency": The base currency. The value is expressed in that
+                     currency (default: BTC)
+    "follow": Whether to follow the order once run (default: True)
+    "close_if_possible": Whether to try to close the position at the end
+                         of the trade, i.e. reach exactly 0 at the end
+                         (only meaningful in "dispose"). May have
+                         unwanted effects if the end value of the
+                         currency is not 0.
+    "compute_value": Compute value to place the order
+    """
+    market.report.log_stage("make_order_begin")
+    market.balances.fetch_balances(tag="make_order_begin")
+    if action == "acquire":
+        trade = portfolio.Trade(
+                portfolio.Amount(base_currency, 0),
+                portfolio.Amount(base_currency, value),
+                currency, market)
+    else:
+        amount = portfolio.Amount(currency, value)
+        trade = portfolio.Trade(
+                amount.in_currency(base_currency, market, compute_value=compute_value),
+                portfolio.Amount(base_currency, 0),
+                currency, market)
+    market.trades.all.append(trade)
+    order = trade.prepare_order(
+            close_if_possible=close_if_possible,
+            compute_value=compute_value)
+    market.report.log_orders([order], None, compute_value)
+    market.trades.run_orders()
+    if follow:
+        market.follow_orders()
+        market.balances.fetch_balances(tag="make_order_end")
+    else:
+        market.report.log_stage("make_order_end_not_followed")
+        return order
+    market.report.log_stage("make_order_end")
+
+def get_user_market(config_path, user_id, debug=False):
+    pg_config, report_path = parse_config(config_path)
+    market_config = list(fetch_markets(pg_config, str(user_id)))[0][0]
+    return market.Market.from_config(market_config, debug=debug)
+
+def fetch_markets(pg_config, user):
+    connection = psycopg2.connect(**pg_config)
+    cursor = connection.cursor()
+
+    if user is None:
+        cursor.execute("SELECT config,user_id FROM market_configs")
+    else:
+        cursor.execute("SELECT config,user_id FROM market_configs WHERE user_id = %s", user)
+
+    for row in cursor:
+        yield row
+
+def parse_config(config_file):
+    config = configparser.ConfigParser()
+    config.read(config_file)
+
+    if "postgresql" not in config:
+        print("no configuration for postgresql in config file")
+        sys.exit(1)
+
+    if "app" in config and "report_path" in config["app"]:
+        report_path = config["app"]["report_path"]
+
+        if not os.path.exists(report_path):
+            os.makedirs(report_path)
+    else:
+        report_path = None
+
+    return [config["postgresql"], report_path]
+
+def parse_args(argv):
+    parser = argparse.ArgumentParser(
+            description="Run the trade bot")
+
+    parser.add_argument("-c", "--config",
+            default="config.ini",
+            required=False,
+            help="Config file to load (default: config.ini)")
+    parser.add_argument("--before",
+            default=False, action='store_const', const=True,
+            help="Run the steps before the cryptoportfolio update")
+    parser.add_argument("--after",
+            default=False, action='store_const', const=True,
+            help="Run the steps after the cryptoportfolio update")
+    parser.add_argument("--debug",
+            default=False, action='store_const', const=True,
+            help="Run in debug mode")
+    parser.add_argument("--user",
+            default=None, required=False, help="Only run for that user")
+    parser.add_argument("--action",
+            action='append',
+            help="Do a different action than trading (add several times to chain)")
+
+    args = parser.parse_args(argv)
+
+    if not os.path.exists(args.config):
+        print("no config file found, exiting")
+        sys.exit(1)
+
+    return args
+
+def main(argv):
+    args = parse_args(argv)
+
+    pg_config, report_path = parse_config(args.config)
+
+    for market_config, user_id in fetch_markets(pg_config, args.user):
         try:
-            user_market.report.log_error("main", exception=e)
-        except:
+            market.Market\
+                    .from_config(market_config, debug=args.debug, user_id=user_id, report_path=report_path)\
+                    .process(args.action, before=args.before, after=args.after)
+        except Exception as e:
             print("{}: {}".format(e.__class__.__name__, e))
-    finally:
-        helper.main_store_report(report_path, user_id, user_market)
+
+if __name__ == '__main__': # pragma: no cover
+    main(sys.argv[1:])
index 3381d1e2643e03cb35019cbd14eb29f0f9108cbf..6c14ae208af3add2eb1bf65ab6627696945e8b22 100644 (file)
--- a/market.py
+++ b/market.py
@@ -3,6 +3,8 @@ import ccxt_wrapper as ccxt
 import time
 from store import *
 from cachetools.func import ttl_cache
+from datetime import datetime
+import portfolio
 
 class Market:
     debug = False
@@ -11,17 +13,21 @@ class Market:
     trades = None
     balances = None
 
-    def __init__(self, ccxt_instance, debug=False):
+    def __init__(self, ccxt_instance, debug=False, user_id=None, report_path=None):
         self.debug = debug
         self.ccxt = ccxt_instance
         self.ccxt._market = self
         self.report = ReportStore(self)
         self.trades = TradeStore(self)
         self.balances = BalanceStore(self)
+        self.processor = Processor(self)
+
+        self.user_id = user_id
+        self.report_path = report_path
 
     @classmethod
-    def from_config(cls, config, debug=False):
-        config["apiKey"] = config.pop("key")
+    def from_config(cls, config, debug=False, user_id=None, report_path=None):
+        config["apiKey"] = config.pop("key", None)
 
         ccxt_instance = ccxt.poloniexE(config)
 
@@ -37,7 +43,34 @@ class Market:
         ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session,
                 ccxt_instance.session.__class__)
 
-        return cls(ccxt_instance, debug=debug)
+        return cls(ccxt_instance, debug=debug, user_id=user_id, report_path=report_path)
+
+    def store_report(self):
+        try:
+            if self.report_path is not None:
+                report_file = "{}/{}_{}.json".format(self.report_path, datetime.now().isoformat(), self.user_id)
+                with open(report_file, "w") as f:
+                    f.write(self.report.to_json())
+        except Exception as e:
+            print("impossible to store report file: {}; {}".format(e.__class__.__name__, e))
+
+    def process(self, actions, before=False, after=False):
+        try:
+            if len(actions or []) == 0:
+                if before:
+                    self.processor.process("sell_all", steps="before")
+                if after:
+                    self.processor.process("sell_all", steps="after")
+            else:
+                for action in actions:
+                    if hasattr(self, action):
+                        getattr(self, action)()
+                    else:
+                        self.report.log_error("market_process", message="Unknown action {}".format(action))
+        except Exception as e:
+            self.report.log_error("market_process", exception=e)
+        finally:
+            self.store_report()
 
     def move_balances(self):
         needed_in_margin = {} 
@@ -143,3 +176,177 @@ class Market:
                 liquidity=liquidity, repartition=repartition)
         self.trades.compute_trades(values_in_base, new_repartition, only=only)
 
+    # Helpers
+    def print_orders(self, base_currency="BTC"):
+        self.report.log_stage("print_orders")
+        self.balances.fetch_balances(tag="print_orders")
+        self.prepare_trades(base_currency=base_currency, compute_value="average")
+        self.trades.prepare_orders(compute_value="average")
+
+    def print_balances(self, base_currency="BTC"):
+        self.report.log_stage("print_balances")
+        self.balances.fetch_balances()
+        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 = {
+            "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" },
+                    "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": {},
+                    "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"]
+
+    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: step in x and x[step], 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 process(self, scenario_name, steps="all", **kwargs):
+        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.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)
+
diff --git a/test.py b/test.py
index a45010b7c2922929866ad82808cfeebc2bce88e7..feac62f7be7755cebac97c4ccdd4774285a3f457 100644 (file)
--- a/test.py
+++ b/test.py
@@ -7,7 +7,7 @@ from unittest import mock
 import requests
 import requests_mock
 from io import StringIO
-import portfolio, helper, market
+import portfolio, market, main
 
 limits = ["acceptance", "unit"]
 for test_type in limits:
@@ -907,7 +907,163 @@ class MarketTest(WebMockTestCase):
                     self.ccxt.transfer_balance.assert_any_call("BTC", 3, "exchange", "margin")
                     self.ccxt.transfer_balance.assert_any_call("USDT", 100, "exchange", "margin")
                     self.ccxt.transfer_balance.assert_any_call("ETC", 5, "margin", "exchange")
-  
+
+    def test_store_report(self):
+
+        file_open = mock.mock_open()
+        with self.subTest(file=None), mock.patch("market.open", file_open):
+            m = market.Market(self.ccxt, user_id=1)
+            m.store_report()
+            file_open.assert_not_called()
+
+        file_open = mock.mock_open()
+        m = market.Market(self.ccxt, report_path="present", user_id=1)
+        with self.subTest(file="present"),\
+                mock.patch("market.open", file_open),\
+                mock.patch.object(m, "report") as report,\
+                mock.patch.object(market, "datetime") as time_mock:
+
+            time_mock.now.return_value = datetime.datetime(2018, 2, 25)
+            report.to_json.return_value = "json_content"
+
+            m.store_report()
+
+            file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
+            file_open().write.assert_called_once_with("json_content")
+            m.report.to_json.assert_called_once_with()
+
+        m = market.Market(self.ccxt, report_path="error", user_id=1)
+        with self.subTest(file="error"),\
+                mock.patch("market.open") as file_open,\
+                mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+            file_open.side_effect = FileNotFoundError
+
+            m.store_report()
+
+            self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;")
+
+    def test_print_orders(self):
+        m = market.Market(self.ccxt)
+        with mock.patch.object(m.report, "log_stage") as log_stage,\
+                mock.patch.object(m.balances, "fetch_balances") as fetch_balances,\
+                mock.patch.object(m, "prepare_trades") as prepare_trades,\
+                mock.patch.object(m.trades, "prepare_orders") as prepare_orders:
+            m.print_orders()
+
+            log_stage.assert_called_with("print_orders")
+            fetch_balances.assert_called_with(tag="print_orders")
+            prepare_trades.assert_called_with(base_currency="BTC",
+                    compute_value="average")
+            prepare_orders.assert_called_with(compute_value="average")
+
+    def test_print_balances(self):
+        m = market.Market(self.ccxt)
+
+        with mock.patch.object(m.balances, "in_currency") as in_currency,\
+                mock.patch.object(m.report, "log_stage") as log_stage,\
+                mock.patch.object(m.balances, "fetch_balances") as fetch_balances,\
+                mock.patch.object(m.report, "print_log") as print_log:
+
+            in_currency.return_value = {
+                    "BTC": portfolio.Amount("BTC", "0.65"),
+                    "ETH": portfolio.Amount("BTC", "0.3"),
+                    }
+
+            m.print_balances()
+
+            log_stage.assert_called_once_with("print_balances")
+            fetch_balances.assert_called_with()
+            print_log.assert_has_calls([
+                mock.call("total:"),
+                mock.call(portfolio.Amount("BTC", "0.95")),
+                ])
+
+    @mock.patch("market.Processor.process")
+    @mock.patch("market.ReportStore.log_error")
+    @mock.patch("market.Market.store_report")
+    def test_process(self, store_report, log_error, process):
+        m = market.Market(self.ccxt)
+        with self.subTest(before=False, after=False):
+            m.process(None)
+
+            process.assert_not_called()
+            store_report.assert_called_once()
+            log_error.assert_not_called()
+
+        process.reset_mock()
+        log_error.reset_mock()
+        store_report.reset_mock()
+        with self.subTest(before=True, after=False):
+            m.process(None, before=True)
+
+            process.assert_called_once_with("sell_all", steps="before")
+            store_report.assert_called_once()
+            log_error.assert_not_called()
+
+        process.reset_mock()
+        log_error.reset_mock()
+        store_report.reset_mock()
+        with self.subTest(before=False, after=True):
+            m.process(None, after=True)
+
+            process.assert_called_once_with("sell_all", steps="after")
+            store_report.assert_called_once()
+            log_error.assert_not_called()
+
+        process.reset_mock()
+        log_error.reset_mock()
+        store_report.reset_mock()
+        with self.subTest(before=True, after=True):
+            m.process(None, before=True, after=True)
+
+            process.assert_has_calls([
+                mock.call("sell_all", steps="before"),
+                mock.call("sell_all", steps="after"),
+                ])
+            store_report.assert_called_once()
+            log_error.assert_not_called()
+
+        process.reset_mock()
+        log_error.reset_mock()
+        store_report.reset_mock()
+        with self.subTest(action="print_balances"),\
+                mock.patch.object(m, "print_balances") as print_balances:
+            m.process(["print_balances"])
+
+            process.assert_not_called()
+            log_error.assert_not_called()
+            store_report.assert_called_once()
+            print_balances.assert_called_once_with()
+
+        log_error.reset_mock()
+        store_report.reset_mock()
+        with self.subTest(action="print_orders"),\
+                mock.patch.object(m, "print_orders") as print_orders,\
+                mock.patch.object(m, "print_balances") as print_balances:
+            m.process(["print_orders", "print_balances"])
+
+            process.assert_not_called()
+            log_error.assert_not_called()
+            store_report.assert_called_once()
+            print_orders.assert_called_once_with()
+            print_balances.assert_called_once_with()
+
+        log_error.reset_mock()
+        store_report.reset_mock()
+        with self.subTest(action="unknown"):
+            m.process(["unknown"])
+            log_error.assert_called_once_with("market_process", message="Unknown action unknown")
+            store_report.assert_called_once()
+
+        log_error.reset_mock()
+        store_report.reset_mock()
+        with self.subTest(unhandled_exception=True):
+            process.side_effect = Exception("bouh")
+
+            m.process(None, before=True)
+            log_error.assert_called_with("market_process", exception=mock.ANY)
+            store_report.assert_called_once()
 @unittest.skipUnless("unit" in limits, "Unit skipped")
 class TradeStoreTest(WebMockTestCase):
     def test_compute_trades(self):
@@ -2752,7 +2908,7 @@ class ReportStoreTest(WebMockTestCase):
             })
 
 @unittest.skipUnless("unit" in limits, "Unit skipped")
-class HelperTest(WebMockTestCase):
+class MainTest(WebMockTestCase):
     def test_make_order(self):
         self.m.get_ticker.return_value = {
                 "inverted": False,
@@ -2762,7 +2918,7 @@ class HelperTest(WebMockTestCase):
                 }
 
         with self.subTest(description="nominal case"):
-            helper.make_order(self.m, 10, "ETH")
+            main.make_order(self.m, 10, "ETH")
 
             self.m.report.log_stage.assert_has_calls([
                 mock.call("make_order_begin"),
@@ -2787,7 +2943,7 @@ class HelperTest(WebMockTestCase):
 
             self.m.reset_mock()
             with self.subTest(compute_value="default"):
-                helper.make_order(self.m, 10, "ETH", action="dispose",
+                main.make_order(self.m, 10, "ETH", action="dispose",
                         compute_value="ask")
 
                 trade = self.m.trades.all.append.mock_calls[0][1][0]
@@ -2796,7 +2952,7 @@ class HelperTest(WebMockTestCase):
 
         self.m.reset_mock()
         with self.subTest(follow=False):
-            result = helper.make_order(self.m, 10, "ETH", follow=False)
+            result = main.make_order(self.m, 10, "ETH", follow=False)
 
             self.m.report.log_stage.assert_has_calls([
                 mock.call("make_order_begin"),
@@ -2816,7 +2972,7 @@ class HelperTest(WebMockTestCase):
 
         self.m.reset_mock()
         with self.subTest(base_currency="USDT"):
-            helper.make_order(self.m, 1, "BTC", base_currency="USDT")
+            main.make_order(self.m, 1, "BTC", base_currency="USDT")
 
             trade = self.m.trades.all.append.mock_calls[0][1][0]
             self.assertEqual("BTC", trade.currency)
@@ -2824,14 +2980,14 @@ class HelperTest(WebMockTestCase):
 
         self.m.reset_mock()
         with self.subTest(close_if_possible=True):
-            helper.make_order(self.m, 10, "ETH", close_if_possible=True)
+            main.make_order(self.m, 10, "ETH", close_if_possible=True)
 
             trade = self.m.trades.all.append.mock_calls[0][1][0]
             self.assertEqual(True, trade.orders[0].close_if_possible)
 
         self.m.reset_mock()
         with self.subTest(action="dispose"):
-            helper.make_order(self.m, 10, "ETH", action="dispose")
+            main.make_order(self.m, 10, "ETH", action="dispose")
 
             trade = self.m.trades.all.append.mock_calls[0][1][0]
             self.assertEqual(0, trade.value_to)
@@ -2841,19 +2997,19 @@ class HelperTest(WebMockTestCase):
 
             self.m.reset_mock()
             with self.subTest(compute_value="default"):
-                helper.make_order(self.m, 10, "ETH", action="dispose",
+                main.make_order(self.m, 10, "ETH", action="dispose",
                         compute_value="bid")
 
                 trade = self.m.trades.all.append.mock_calls[0][1][0]
                 self.assertEqual(D("0.9"), trade.value_from.value)
 
-    def test_user_market(self):
-        with mock.patch("helper.main_fetch_markets") as main_fetch_markets,\
-                mock.patch("helper.main_parse_config") as main_parse_config:
+    def test_get_user_market(self):
+        with mock.patch("main.fetch_markets") as main_fetch_markets,\
+                mock.patch("main.parse_config") as main_parse_config:
             with self.subTest(debug=False):
                 main_parse_config.return_value = ["pg_config", "report_path"]
                 main_fetch_markets.return_value = [({"key": "market_config"},)]
-                m = helper.get_user_market("config_path.ini", 1)
+                m = main.get_user_market("config_path.ini", 1)
 
                 self.assertIsInstance(m, market.Market)
                 self.assertFalse(m.debug)
@@ -2861,141 +3017,56 @@ class HelperTest(WebMockTestCase):
             with self.subTest(debug=True):
                 main_parse_config.return_value = ["pg_config", "report_path"]
                 main_fetch_markets.return_value = [({"key": "market_config"},)]
-                m = helper.get_user_market("config_path.ini", 1, debug=True)
+                m = main.get_user_market("config_path.ini", 1, debug=True)
 
                 self.assertIsInstance(m, market.Market)
                 self.assertTrue(m.debug)
 
-    def test_main_store_report(self):
-        file_open = mock.mock_open()
-        with self.subTest(file=None), mock.patch("__main__.open", file_open):
-            helper.main_store_report(None, 1, self.m)
-            file_open.assert_not_called()
-
-        file_open = mock.mock_open()
-        with self.subTest(file="present"), mock.patch("helper.open", file_open),\
-                mock.patch.object(helper, "datetime") as time_mock:
-            time_mock.now.return_value = datetime.datetime(2018, 2, 25)
-            self.m.report.to_json.return_value = "json_content"
-
-            helper.main_store_report("present", 1, self.m)
-
-            file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
-            file_open().write.assert_called_once_with("json_content")
-            self.m.report.to_json.assert_called_once_with()
-
-        with self.subTest(file="error"),\
-                mock.patch("helper.open") as file_open,\
+    def test_main(self):
+        with mock.patch("main.parse_args") as parse_args,\
+                mock.patch("main.parse_config") as parse_config,\
+                mock.patch("main.fetch_markets") as fetch_markets,\
+                mock.patch("market.Market") as market_mock,\
                 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
-            file_open.side_effect = FileNotFoundError
 
-            helper.main_store_report("error", 1, self.m)
+            args_mock = mock.Mock()
+            args_mock.action = "action"
+            args_mock.config = "config"
+            args_mock.user = "user"
+            args_mock.debug = "debug"
+            args_mock.before = "before"
+            args_mock.after = "after"
+            parse_args.return_value = args_mock
 
-            self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;")
+            parse_config.return_value = ["pg_config", "report_path"]
 
-    @mock.patch("helper.Processor.process")
-    def test_main_process_market(self, process):
-        with self.subTest(before=False, after=False):
-            m = mock.Mock()
-            helper.main_process_market(m, None)
+            fetch_markets.return_value = [["config1", 1], ["config2", 2]]
 
-            process.assert_not_called()
+            main.main(["Foo", "Bar"])
 
-        process.reset_mock()
-        with self.subTest(before=True, after=False):
-            helper.main_process_market(m, None, before=True)
+            parse_args.assert_called_with(["Foo", "Bar"])
+            parse_config.assert_called_with("config")
+            fetch_markets.assert_called_with("pg_config", "user")
 
-            process.assert_called_once_with("sell_all", steps="before")
-
-        process.reset_mock()
-        with self.subTest(before=False, after=True):
-            helper.main_process_market(m, None, after=True)
-            
-            process.assert_called_once_with("sell_all", steps="after")
-
-        process.reset_mock()
-        with self.subTest(before=True, after=True):
-            helper.main_process_market(m, None, before=True, after=True)
-
-            process.assert_has_calls([
-                mock.call("sell_all", steps="before"),
-                mock.call("sell_all", steps="after"),
+            self.assertEqual(2, market_mock.from_config.call_count)
+            market_mock.from_config.assert_has_calls([
+                mock.call("config1", debug="debug", user_id=1, report_path="report_path"),
+                mock.call().process("action", before="before", after="after"),
+                mock.call("config2", debug="debug", user_id=2, report_path="report_path"),
+                mock.call().process("action", before="before", after="after")
                 ])
 
-        process.reset_mock()
-        with self.subTest(action="print_balances"),\
-                mock.patch("helper.print_balances") as print_balances:
-            helper.main_process_market("user", ["print_balances"])
-
-            process.assert_not_called()
-            print_balances.assert_called_once_with("user")
-
-        with self.subTest(action="print_orders"),\
-                mock.patch("helper.print_orders") as print_orders,\
-                mock.patch("helper.print_balances") as print_balances:
-            helper.main_process_market("user", ["print_orders", "print_balances"])
-
-            process.assert_not_called()
-            print_orders.assert_called_once_with("user")
-            print_balances.assert_called_once_with("user")
-
-        with self.subTest(action="unknown"),\
-                self.assertRaises(NotImplementedError):
-            helper.main_process_market("user", ["unknown"])
-
-    @mock.patch.object(helper, "psycopg2")
-    def test_fetch_markets(self, psycopg2):
-        connect_mock = mock.Mock()
-        cursor_mock = mock.MagicMock()
-        cursor_mock.__iter__.return_value = ["row_1", "row_2"]
-
-        connect_mock.cursor.return_value = cursor_mock
-        psycopg2.connect.return_value = connect_mock
-
-        with self.subTest(user=None):
-            rows = list(helper.main_fetch_markets({"foo": "bar"}, None))
-
-            psycopg2.connect.assert_called_once_with(foo="bar")
-            cursor_mock.execute.assert_called_once_with("SELECT config,user_id FROM market_configs")
-
-            self.assertEqual(["row_1", "row_2"], rows)
-
-        psycopg2.connect.reset_mock()
-        cursor_mock.execute.reset_mock()
-        with self.subTest(user=1):
-            rows = list(helper.main_fetch_markets({"foo": "bar"}, 1))
-
-            psycopg2.connect.assert_called_once_with(foo="bar")
-            cursor_mock.execute.assert_called_once_with("SELECT config,user_id FROM market_configs WHERE user_id = %s", 1)
-
-            self.assertEqual(["row_1", "row_2"], rows)
-
-    @mock.patch.object(helper.sys, "exit")
-    def test_main_parse_args(self, exit):
-        with self.subTest(config="config.ini"):
-            args = helper.main_parse_args([])
-            self.assertEqual("config.ini", args.config)
-            self.assertFalse(args.before)
-            self.assertFalse(args.after)
-            self.assertFalse(args.debug)
-
-            args = helper.main_parse_args(["--before", "--after", "--debug"])
-            self.assertTrue(args.before)
-            self.assertTrue(args.after)
-            self.assertTrue(args.debug)
-
-            exit.assert_not_called()
+            self.assertEqual("", stdout_mock.getvalue())
 
-        with self.subTest(config="inexistant"),\
-                mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
-            args = helper.main_parse_args(["--config", "foo.bar"])
-            exit.assert_called_once_with(1)
-            self.assertEqual("no config file found, exiting\n", stdout_mock.getvalue())
+            with self.subTest(exception=True):
+                market_mock.from_config.side_effect = Exception("boo")
+                main.main(["Foo", "Bar"])
+                self.assertEqual("Exception: boo\nException: boo\n", stdout_mock.getvalue())
 
-    @mock.patch.object(helper.sys, "exit")
-    @mock.patch("helper.configparser")
-    @mock.patch("helper.os")
-    def test_main_parse_config(self, os, configparser, exit):
+    @mock.patch.object(main.sys, "exit")
+    @mock.patch("main.configparser")
+    @mock.patch("main.os")
+    def test_parse_config(self, os, configparser, exit):
         with self.subTest(pg_config=True, report_path=None):
             config_mock = mock.MagicMock()
             configparser.ConfigParser.return_value = config_mock
@@ -3005,7 +3076,7 @@ class HelperTest(WebMockTestCase):
             config_mock.__contains__.side_effect = config
             config_mock.__getitem__.return_value = "pg_config"
 
-            result = helper.main_parse_config("configfile")
+            result = main.parse_config("configfile")
 
             config_mock.read.assert_called_with("configfile")
 
@@ -3023,7 +3094,7 @@ class HelperTest(WebMockTestCase):
                     ]
 
             os.path.exists.return_value = False
-            result = helper.main_parse_config("configfile")
+            result = main.parse_config("configfile")
 
             config_mock.read.assert_called_with("configfile")
             self.assertEqual(["pg_config", "report_path"], result)
@@ -3034,46 +3105,71 @@ class HelperTest(WebMockTestCase):
                 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
             config_mock = mock.MagicMock()
             configparser.ConfigParser.return_value = config_mock
-            result = helper.main_parse_config("configfile")
+            result = main.parse_config("configfile")
 
             config_mock.read.assert_called_with("configfile")
             exit.assert_called_once_with(1)
             self.assertEqual("no configuration for postgresql in config file\n", stdout_mock.getvalue())
 
+    @mock.patch.object(main.sys, "exit")
+    def test_parse_args(self, exit):
+        with self.subTest(config="config.ini"):
+            args = main.parse_args([])
+            self.assertEqual("config.ini", args.config)
+            self.assertFalse(args.before)
+            self.assertFalse(args.after)
+            self.assertFalse(args.debug)
 
-    def test_print_orders(self):
-        helper.print_orders(self.m)
+            args = main.parse_args(["--before", "--after", "--debug"])
+            self.assertTrue(args.before)
+            self.assertTrue(args.after)
+            self.assertTrue(args.debug)
 
-        self.m.report.log_stage.assert_called_with("print_orders")
-        self.m.balances.fetch_balances.assert_called_with(tag="print_orders")
-        self.m.prepare_trades.assert_called_with(base_currency="BTC",
-                compute_value="average")
-        self.m.trades.prepare_orders.assert_called_with(compute_value="average")
+            exit.assert_not_called()
+
+        with self.subTest(config="inexistant"),\
+                mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+            args = main.parse_args(["--config", "foo.bar"])
+            exit.assert_called_once_with(1)
+            self.assertEqual("no config file found, exiting\n", stdout_mock.getvalue())
+
+    @mock.patch.object(main, "psycopg2")
+    def test_fetch_markets(self, psycopg2):
+        connect_mock = mock.Mock()
+        cursor_mock = mock.MagicMock()
+        cursor_mock.__iter__.return_value = ["row_1", "row_2"]
+
+        connect_mock.cursor.return_value = cursor_mock
+        psycopg2.connect.return_value = connect_mock
+
+        with self.subTest(user=None):
+            rows = list(main.fetch_markets({"foo": "bar"}, None))
+
+            psycopg2.connect.assert_called_once_with(foo="bar")
+            cursor_mock.execute.assert_called_once_with("SELECT config,user_id FROM market_configs")
+
+            self.assertEqual(["row_1", "row_2"], rows)
+
+        psycopg2.connect.reset_mock()
+        cursor_mock.execute.reset_mock()
+        with self.subTest(user=1):
+            rows = list(main.fetch_markets({"foo": "bar"}, 1))
+
+            psycopg2.connect.assert_called_once_with(foo="bar")
+            cursor_mock.execute.assert_called_once_with("SELECT config,user_id FROM market_configs WHERE user_id = %s", 1)
+
+            self.assertEqual(["row_1", "row_2"], rows)
 
-    def test_print_balances(self):
-        self.m.balances.in_currency.return_value = {
-                "BTC": portfolio.Amount("BTC", "0.65"),
-                "ETH": portfolio.Amount("BTC", "0.3"),
-        }
-
-        helper.print_balances(self.m)
-
-        self.m.report.log_stage.assert_called_once_with("print_balances")
-        self.m.balances.fetch_balances.assert_called_with()
-        self.m.report.print_log.assert_has_calls([
-            mock.call("total:"),
-            mock.call(portfolio.Amount("BTC", "0.95")),
-            ])
 
 @unittest.skipUnless("unit" in limits, "Unit skipped")
 class ProcessorTest(WebMockTestCase):
     def test_values(self):
-        processor = helper.Processor(self.m)
+        processor = market.Processor(self.m)
 
         self.assertEqual(self.m, processor.market)
 
     def test_run_action(self):
-        processor = helper.Processor(self.m)
+        processor = market.Processor(self.m)
 
         with mock.patch.object(processor, "parse_args") as parse_args:
             method_mock = mock.Mock()
@@ -3090,7 +3186,7 @@ class ProcessorTest(WebMockTestCase):
             method_mock.assert_called_with(self.m, foo="bar")
 
     def test_select_step(self):
-        processor = helper.Processor(self.m)
+        processor = market.Processor(self.m)
 
         scenario = processor.scenarios["sell_all"]
 
@@ -3103,9 +3199,9 @@ class ProcessorTest(WebMockTestCase):
         with self.assertRaises(TypeError):
             processor.select_steps(scenario, ["wait"])
 
-    @mock.patch("helper.Processor.process_step")
+    @mock.patch("market.Processor.process_step")
     def test_process(self, process_step):
-        processor = helper.Processor(self.m)
+        processor = market.Processor(self.m)
 
         processor.process("sell_all", foo="bar")
         self.assertEqual(3, process_step.call_count)
@@ -3126,7 +3222,7 @@ class ProcessorTest(WebMockTestCase):
         ccxt = mock.Mock(spec=market.ccxt.poloniexE)
         m = market.Market(ccxt)
 
-        processor = helper.Processor(m)
+        processor = market.Processor(m)
 
         method, arguments = processor.method_arguments("wait_for_recent")
         self.assertEqual(portfolio.Portfolio.wait_for_recent, method)
@@ -3152,7 +3248,7 @@ class ProcessorTest(WebMockTestCase):
         self.assertEqual(m.trades.close_trades, method)
 
     def test_process_step(self):
-        processor = helper.Processor(self.m)
+        processor = market.Processor(self.m)
 
         with mock.patch.object(processor, "run_action") as run_action:
             step = processor.scenarios["sell_needed"][1]
@@ -3186,7 +3282,7 @@ class ProcessorTest(WebMockTestCase):
             self.m.balances.fetch_balances.assert_not_called()
 
     def test_parse_args(self):
-        processor = helper.Processor(self.m)
+        processor = market.Processor(self.m)
 
         with mock.patch.object(processor, "method_arguments") as method_arguments:
             method_mock = mock.Mock()