]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Merge branch 'night_fixes' into dev
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 12 Mar 2018 01:10:08 +0000 (02:10 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 12 Mar 2018 01:10:08 +0000 (02:10 +0100)
helper.py [deleted file]
main.py
market.py
portfolio.py
store.py
test.py
test_samples/poloniexETest.test_fetch_all_balances.1.json [new file with mode: 0644]
test_samples/poloniexETest.test_fetch_all_balances.2.json [new file with mode: 0644]
test_samples/poloniexETest.test_fetch_all_balances.3.json [new file with mode: 0644]
test_samples/test_portfolio.json [moved from test_portfolio.json with 100% similarity]

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..856d449fc16cf94263c81ad45b11bcb406a12ce0 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):
+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)")
+    parser.add_argument("--parallel", action='store_true', default=True, dest="parallel")
+    parser.add_argument("--no-parallel", action='store_false', dest="parallel")
+
+    args = parser.parse_args(argv)
+
+    if not os.path.exists(args.config):
+        print("no config file found, exiting")
+        sys.exit(1)
+
+    return args
+
+def process(market_config, user_id, report_path, args):
     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)
+        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:
-        try:
-            user_market.report.log_error("main", exception=e)
-        except:
-            print("{}: {}".format(e.__class__.__name__, e))
-    finally:
-        helper.main_store_report(report_path, user_id, user_market)
+        print("{}: {}".format(e.__class__.__name__, e))
+
+def main(argv):
+    args = parse_args(argv)
+
+    pg_config, report_path = parse_config(args.config)
+
+    if args.parallel:
+        import threading
+        market.Portfolio.start_worker()
+
+        for market_config, user_id in fetch_markets(pg_config, args.user):
+            threading.Thread(target=process, args=[market_config, user_id, report_path, args]).start()
+    else:
+        for market_config, user_id in fetch_markets(pg_config, args.user):
+            process(market_config, user_id, report_path, args)
+
+if __name__ == '__main__': # pragma: no cover
+    main(sys.argv[1:])
index 3381d1e2643e03cb35019cbd14eb29f0f9108cbf..8672c59699431846ac097173bf5cb9ab076b3910 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,35 @@ 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):
+        self.report.merge(Portfolio.report)
+        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 +177,200 @@ 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 = {
+            "wait_for_cryptoportfolio": [
+                {
+                    "name": "wait",
+                    "number": 1,
+                    "before": False,
+                    "after": True,
+                    "wait_for_recent": {},
+                    },
+                ],
+            "print_orders": [
+                {
+                    "name": "wait",
+                    "number": 1,
+                    "before": False,
+                    "after": True,
+                    "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" },
+                    "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.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)
+
+        method(**args)
index ed50b570ea2c31d310c6a3d271f37f7f413c43c0..69e37557fd9cd355fbdccabcd1fb993fbf9adef1 100644 (file)
@@ -1,87 +1,10 @@
-import time
-from datetime import datetime, timedelta
+from datetime import datetime
 from decimal import Decimal as D, ROUND_DOWN
-from json import JSONDecodeError
-from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError
 from ccxt import ExchangeError, InsufficientFunds, ExchangeNotAvailable, InvalidOrder, OrderNotCached, OrderNotFound
 from retry import retry
-import requests
 
 # FIXME: correctly handle web call timeouts
 
-class Portfolio:
-    URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
-    liquidities = {}
-    data = None
-    last_date = None
-
-    @classmethod
-    def wait_for_recent(cls, market, delta=4):
-        cls.repartition(market, refetch=True)
-        while cls.last_date is None or datetime.now() - cls.last_date > timedelta(delta):
-            time.sleep(30)
-            market.report.print_log("Attempt to fetch up-to-date cryptoportfolio")
-            cls.repartition(market, refetch=True)
-
-    @classmethod
-    def repartition(cls, market, liquidity="medium", refetch=False):
-        cls.parse_cryptoportfolio(market, refetch=refetch)
-        liquidities = cls.liquidities[liquidity]
-        return liquidities[cls.last_date]
-
-    @classmethod
-    def get_cryptoportfolio(cls, market):
-        try:
-            r = requests.get(cls.URL)
-            market.report.log_http_request(r.request.method,
-                    r.request.url, r.request.body, r.request.headers, r)
-        except Exception as e:
-            market.report.log_error("get_cryptoportfolio", exception=e)
-            return
-        try:
-            cls.data = r.json(parse_int=D, parse_float=D)
-        except (JSONDecodeError, SimpleJSONDecodeError):
-            cls.data = None
-
-    @classmethod
-    def parse_cryptoportfolio(cls, market, refetch=False):
-        if refetch or cls.data is None:
-            cls.get_cryptoportfolio(market)
-
-        def filter_weights(weight_hash):
-            if weight_hash[1][0] == 0:
-                return False
-            if weight_hash[0] == "_row":
-                return False
-            return True
-
-        def clean_weights(i):
-            def clean_weights_(h):
-                if h[0].endswith("s"):
-                    return [h[0][0:-1], (h[1][i], "short")]
-                else:
-                    return [h[0], (h[1][i], "long")]
-            return clean_weights_
-
-        def parse_weights(portfolio_hash):
-            weights_hash = portfolio_hash["weights"]
-            weights = {}
-            for i in range(len(weights_hash["_row"])):
-                date = datetime.strptime(weights_hash["_row"][i], "%Y-%m-%d")
-                weights[date] = dict(filter(
-                        filter_weights,
-                        map(clean_weights(i), weights_hash.items())))
-            return weights
-
-        high_liquidity = parse_weights(cls.data["portfolio_1"])
-        medium_liquidity = parse_weights(cls.data["portfolio_2"])
-
-        cls.liquidities = {
-                "medium": medium_liquidity,
-                "high":   high_liquidity,
-                }
-        cls.last_date = max(max(medium_liquidity.keys()), max(high_liquidity.keys()))
-
 class Computation:
     computations = {
             "default": lambda x, y: x[y],
index d25dd35d1b06efe8f73b812c90cb1d51a66448cc..f655be538b66f3d81d7fb121a0595ee4ff63ddc7 100644 (file)
--- a/store.py
+++ b/store.py
@@ -1,10 +1,14 @@
+import time
+import requests
 import portfolio
 import simplejson as json
 from decimal import Decimal as D, ROUND_DOWN
-from datetime import date, datetime
+from datetime import date, datetime, timedelta
 import inspect
+from json import JSONDecodeError
+from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError
 
-__all__ = ["BalanceStore", "ReportStore", "TradeStore"]
+__all__ = ["Portfolio", "BalanceStore", "ReportStore", "TradeStore"]
 
 class ReportStore:
     def __init__(self, market, verbose_print=True):
@@ -13,6 +17,10 @@ class ReportStore:
 
         self.logs = []
 
+    def merge(self, other_report):
+        self.logs += other_report.logs
+        self.logs.sort(key=lambda x: x["date"])
+
     def print_log(self, message):
         message = str(message)
         if self.verbose_print:
@@ -213,7 +221,7 @@ class BalanceStore:
 
     def dispatch_assets(self, amount, liquidity="medium", repartition=None):
         if repartition is None:
-            repartition = portfolio.Portfolio.repartition(self.market, liquidity=liquidity)
+            repartition = Portfolio.repartition(liquidity=liquidity)
         sum_ratio = sum([v[0] for k, v in repartition.items()])
         amounts = {}
         for currency, (ptt, trade_type) in repartition.items():
@@ -301,4 +309,165 @@ class TradeStore:
         for order in self.all_orders(state="open"):
             order.get_status()
 
+class NoopLock:
+    def __enter__(self, *args):
+        pass
+    def __exit__(self, *args):
+        pass
+
+class LockedVar:
+    def __init__(self, value):
+        self.lock = NoopLock()
+        self.val = value
+
+    def start_lock(self):
+        import threading
+        self.lock = threading.Lock()
+
+    def set(self, value):
+        with self.lock:
+            self.val = value
+
+    def get(self, key=None):
+        with self.lock:
+            if key is not None and isinstance(self.val, dict):
+                return self.val.get(key)
+            else:
+                return self.val
+
+    def __getattr__(self, key):
+        with self.lock:
+            return getattr(self.val, key)
+
+class Portfolio:
+    URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
+    data = LockedVar(None)
+    liquidities = LockedVar({})
+    last_date = LockedVar(None)
+    report = LockedVar(ReportStore(None))
+    worker = None
+    worker_started = False
+    worker_notify = None
+    callback = None
+
+    @classmethod
+    def start_worker(cls, poll=30):
+        import threading
+
+        cls.worker = threading.Thread(name="portfolio", daemon=True,
+                target=cls.wait_for_notification, kwargs={"poll": poll})
+        cls.worker_notify = threading.Event()
+        cls.callback = threading.Event()
+
+        cls.last_date.start_lock()
+        cls.liquidities.start_lock()
+        cls.report.start_lock()
+
+        cls.worker_started = True
+        cls.worker.start()
+
+    @classmethod
+    def is_worker_thread(cls):
+        if cls.worker is None:
+            return False
+        else:
+            import threading
+            return cls.worker == threading.current_thread()
+
+    @classmethod
+    def wait_for_notification(cls, poll=30):
+        if not cls.is_worker_thread():
+            raise RuntimeError("This method needs to be ran with the worker")
+        while cls.worker_started:
+            cls.worker_notify.wait()
+            cls.worker_notify.clear()
+            cls.report.print_log("Fetching cryptoportfolio")
+            cls.get_cryptoportfolio(refetch=True)
+            cls.callback.set()
+            time.sleep(poll)
+
+    @classmethod
+    def notify_and_wait(cls):
+        cls.callback.clear()
+        cls.worker_notify.set()
+        cls.callback.wait()
+
+    @classmethod
+    def wait_for_recent(cls, delta=4, poll=30):
+        cls.get_cryptoportfolio()
+        while cls.last_date.get() is None or datetime.now() - cls.last_date.get() > timedelta(delta):
+            if cls.worker is None:
+                time.sleep(poll)
+                cls.report.print_log("Attempt to fetch up-to-date cryptoportfolio")
+            cls.get_cryptoportfolio(refetch=True)
+
+    @classmethod
+    def repartition(cls, liquidity="medium"):
+        cls.get_cryptoportfolio()
+        liquidities = cls.liquidities.get(liquidity)
+        return liquidities[cls.last_date.get()]
+
+    @classmethod
+    def get_cryptoportfolio(cls, refetch=False):
+        if cls.data.get() is not None and not refetch:
+            return
+        if cls.worker is not None and not cls.is_worker_thread():
+            cls.notify_and_wait()
+            return
+        try:
+            r = requests.get(cls.URL)
+            cls.report.log_http_request(r.request.method,
+                    r.request.url, r.request.body, r.request.headers, r)
+        except Exception as e:
+            cls.report.log_error("get_cryptoportfolio", exception=e)
+            return
+        try:
+            cls.data.set(r.json(parse_int=D, parse_float=D))
+            cls.parse_cryptoportfolio()
+        except (JSONDecodeError, SimpleJSONDecodeError):
+            cls.data.set(None)
+            cls.last_date.set(None)
+            cls.liquidities.set({})
+
+    @classmethod
+    def parse_cryptoportfolio(cls):
+        def filter_weights(weight_hash):
+            if weight_hash[1][0] == 0:
+                return False
+            if weight_hash[0] == "_row":
+                return False
+            return True
+
+        def clean_weights(i):
+            def clean_weights_(h):
+                if h[0].endswith("s"):
+                    return [h[0][0:-1], (h[1][i], "short")]
+                else:
+                    return [h[0], (h[1][i], "long")]
+            return clean_weights_
+
+        def parse_weights(portfolio_hash):
+            if "weights" not in portfolio_hash:
+                return {}
+            weights_hash = portfolio_hash["weights"]
+            weights = {}
+            for i in range(len(weights_hash["_row"])):
+                date = datetime.strptime(weights_hash["_row"][i], "%Y-%m-%d")
+                weights[date] = dict(filter(
+                        filter_weights,
+                        map(clean_weights(i), weights_hash.items())))
+            return weights
+
+        high_liquidity = parse_weights(cls.data.get("portfolio_1"))
+        medium_liquidity = parse_weights(cls.data.get("portfolio_2"))
+
+        cls.liquidities.set({
+            "medium": medium_liquidity,
+            "high":   high_liquidity,
+            })
+        cls.last_date.set(max(
+            max(medium_liquidity.keys(), default=datetime(1, 1, 1)),
+            max(high_liquidity.keys(), default=datetime(1, 1, 1))
+            ))
+
 
diff --git a/test.py b/test.py
index 921af9f1b451595cfc9fb08cd9affaebb3741a47..ac9a6cd55b3ccf81b14f926939b359e14586ca70 100644 (file)
--- a/test.py
+++ b/test.py
@@ -7,7 +7,8 @@ from unittest import mock
 import requests
 import requests_mock
 from io import StringIO
-import portfolio, helper, market
+import threading
+import portfolio, market, main, store
 
 limits = ["acceptance", "unit"]
 for test_type in limits:
@@ -32,7 +33,15 @@ class WebMockTestCase(unittest.TestCase):
         self.m.debug = False
 
         self.patchers = [
-                mock.patch.multiple(portfolio.Portfolio, last_date=None, data=None, liquidities={}),
+                mock.patch.multiple(market.Portfolio,
+                    data=store.LockedVar(None),
+                    liquidities=store.LockedVar({}),
+                    last_date=store.LockedVar(None),
+                    report=mock.Mock(),
+                    worker=None,
+                    worker_notify=None,
+                    worker_started=False,
+                    callback=None),
                 mock.patch.multiple(portfolio.Computation,
                     computations=portfolio.Computation.computations),
                 ]
@@ -126,174 +135,632 @@ class poloniexETest(unittest.TestCase):
                     }
             self.assertEqual(expected, self.s.margin_summary())
 
+    def test_create_order(self):
+        with mock.patch.object(self.s, "create_exchange_order") as exchange,\
+                mock.patch.object(self.s, "create_margin_order") as margin:
+            with self.subTest(account="unspecified"):
+                self.s.create_order("symbol", "type", "side", "amount", price="price", lending_rate="lending_rate", params="params")
+                exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params")
+                margin.assert_not_called()
+                exchange.reset_mock()
+                margin.reset_mock()
+
+            with self.subTest(account="exchange"):
+                self.s.create_order("symbol", "type", "side", "amount", account="exchange", price="price", lending_rate="lending_rate", params="params")
+                exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params")
+                margin.assert_not_called()
+                exchange.reset_mock()
+                margin.reset_mock()
+
+            with self.subTest(account="margin"):
+                self.s.create_order("symbol", "type", "side", "amount", account="margin", price="price", lending_rate="lending_rate", params="params")
+                margin.assert_called_once_with("symbol", "type", "side", "amount", lending_rate="lending_rate", price="price", params="params")
+                exchange.assert_not_called()
+                exchange.reset_mock()
+                margin.reset_mock()
+
+            with self.subTest(account="unknown"), self.assertRaises(NotImplementedError):
+                self.s.create_order("symbol", "type", "side", "amount", account="unknown")
+
+    def test_parse_ticker(self):
+        ticker = {
+                "high24hr": "12",
+                "low24hr": "10",
+                "highestBid": "10.5",
+                "lowestAsk": "11.5",
+                "last": "11",
+                "percentChange": "0.1",
+                "quoteVolume": "10",
+                "baseVolume": "20"
+                }
+        market = {
+                "symbol": "BTC/ETC"
+                }
+        with mock.patch.object(self.s, "milliseconds") as ms:
+            ms.return_value = 1520292715123
+            result = self.s.parse_ticker(ticker, market)
+
+            expected = {
+                    "symbol": "BTC/ETC",
+                    "timestamp": 1520292715123,
+                    "datetime": "2018-03-05T23:31:55.123Z",
+                    "high": D("12"),
+                    "low": D("10"),
+                    "bid": D("10.5"),
+                    "ask": D("11.5"),
+                    "vwap": None,
+                    "open": None,
+                    "close": None,
+                    "first": None,
+                    "last": D("11"),
+                    "change": D("0.1"),
+                    "percentage": None,
+                    "average": None,
+                    "baseVolume": D("10"),
+                    "quoteVolume": D("20"),
+                    "info": ticker
+                    }
+            self.assertEqual(expected, result)
+
+    def test_fetch_margin_balance(self):
+        with mock.patch.object(self.s, "privatePostGetMarginPosition") as get_margin_position:
+            get_margin_position.return_value = {
+                    "BTC_DASH": {
+                        "amount": "-0.1",
+                        "basePrice": "0.06818560",
+                        "lendingFees": "0.00000001",
+                        "liquidationPrice": "0.15107132",
+                        "pl": "-0.00000371",
+                        "total": "0.00681856",
+                        "type": "short"
+                        },
+                    "BTC_ETC": {
+                        "amount": "-0.6",
+                        "basePrice": "0.1",
+                        "lendingFees": "0.00000001",
+                        "liquidationPrice": "0.6",
+                        "pl": "0.00000371",
+                        "total": "0.06",
+                        "type": "short"
+                        },
+                    "BTC_ETH": {
+                        "amount": "0",
+                        "basePrice": "0",
+                        "lendingFees": "0",
+                        "liquidationPrice": "-1",
+                        "pl": "0",
+                        "total": "0",
+                        "type": "none"
+                        }
+                    }
+            balances = self.s.fetch_margin_balance()
+            self.assertEqual(2, len(balances))
+            expected = {
+                "DASH": {
+                    "amount": D("-0.1"),
+                    "borrowedPrice": D("0.06818560"),
+                    "lendingFees": D("1E-8"),
+                    "pl": D("-0.00000371"),
+                    "liquidationPrice": D("0.15107132"),
+                    "type": "short",
+                    "total": D("0.00681856"),
+                    "baseCurrency": "BTC"
+                    },
+                "ETC": {
+                    "amount": D("-0.6"),
+                    "borrowedPrice": D("0.1"),
+                    "lendingFees": D("1E-8"),
+                    "pl": D("0.00000371"),
+                    "liquidationPrice": D("0.6"),
+                    "type": "short",
+                    "total": D("0.06"),
+                    "baseCurrency": "BTC"
+                    }
+                }
+            self.assertEqual(expected, balances)
+
+    def test_sum(self):
+        self.assertEqual(D("1.1"), self.s.sum(D("1"), D("0.1")))
+
+    def test_fetch_balance(self):
+        with mock.patch.object(self.s, "load_markets") as load_markets,\
+                mock.patch.object(self.s, "privatePostReturnCompleteBalances") as balances,\
+                mock.patch.object(self.s, "common_currency_code") as ccc:
+            ccc.side_effect = ["ETH", "BTC", "DASH"]
+            balances.return_value = {
+                    "ETH": {
+                        "available": "10",
+                        "onOrders": "1",
+                        },
+                    "BTC": {
+                        "available": "1",
+                        "onOrders": "0",
+                        },
+                    "DASH": {
+                        "available": "0",
+                        "onOrders": "3"
+                        }
+                    }
+
+            expected = {
+                    "info": {
+                        "ETH": {"available": "10", "onOrders": "1"},
+                        "BTC": {"available": "1", "onOrders": "0"},
+                        "DASH": {"available": "0", "onOrders": "3"}
+                        },
+                    "ETH": {"free": D("10"), "used": D("1"), "total": D("11")},
+                    "BTC": {"free": D("1"), "used": D("0"), "total": D("1")},
+                    "DASH": {"free": D("0"), "used": D("3"), "total": D("3")},
+                    "free": {"ETH": D("10"), "BTC": D("1"), "DASH": D("0")},
+                    "used": {"ETH": D("1"), "BTC": D("0"), "DASH": D("3")},
+                    "total": {"ETH": D("11"), "BTC": D("1"), "DASH": D("3")}
+                    }
+            result = self.s.fetch_balance()
+            load_markets.assert_called_once()
+            self.assertEqual(expected, result)
+
+    def test_fetch_balance_per_type(self):
+        with mock.patch.object(self.s, "privatePostReturnAvailableAccountBalances") as balances:
+            balances.return_value = {
+                "exchange": {
+                    "BLK": "159.83673869",
+                    "BTC": "0.00005959",
+                    "USDT": "0.00002625",
+                    "XMR": "0.18719303"
+                    },
+                "margin": {
+                    "BTC": "0.03019227"
+                    }
+                }
+            expected = {
+                    "info": {
+                        "exchange": {
+                            "BLK": "159.83673869",
+                            "BTC": "0.00005959",
+                            "USDT": "0.00002625",
+                            "XMR": "0.18719303"
+                            },
+                        "margin": {
+                            "BTC": "0.03019227"
+                            }
+                        },
+                    "exchange": {
+                        "BLK": D("159.83673869"),
+                        "BTC": D("0.00005959"),
+                        "USDT": D("0.00002625"),
+                        "XMR": D("0.18719303")
+                        },
+                    "margin": {"BTC": D("0.03019227")},
+                    "BLK": {"exchange": D("159.83673869")},
+                    "BTC": {"exchange": D("0.00005959"), "margin": D("0.03019227")},
+                    "USDT": {"exchange": D("0.00002625")},
+                    "XMR": {"exchange": D("0.18719303")}
+                    } 
+            result = self.s.fetch_balance_per_type()
+            self.assertEqual(expected, result)
+
+    def test_fetch_all_balances(self):
+        import json
+        with mock.patch.object(self.s, "load_markets") as load_markets,\
+                mock.patch.object(self.s, "privatePostGetMarginPosition") as margin_balance,\
+                mock.patch.object(self.s, "privatePostReturnCompleteBalances") as balance,\
+                mock.patch.object(self.s, "privatePostReturnAvailableAccountBalances") as balance_per_type:
+
+            with open("test_samples/poloniexETest.test_fetch_all_balances.1.json") as f:
+                balance.return_value = json.load(f)
+            with open("test_samples/poloniexETest.test_fetch_all_balances.2.json") as f:
+                margin_balance.return_value = json.load(f)
+            with open("test_samples/poloniexETest.test_fetch_all_balances.3.json") as f:
+                balance_per_type.return_value = json.load(f)
+
+            result = self.s.fetch_all_balances()
+            expected_doge = {
+                    "total": D("-12779.79821852"),
+                    "exchange_used": D("0E-8"),
+                    "exchange_total": D("0E-8"),
+                    "exchange_free": D("0E-8"),
+                    "margin_available": 0,
+                    "margin_in_position": 0,
+                    "margin_borrowed": D("12779.79821852"),
+                    "margin_total": D("-12779.79821852"),
+                    "margin_pending_gain": 0,
+                    "margin_lending_fees": D("-9E-8"),
+                    "margin_pending_base_gain": D("0.00024059"),
+                    "margin_position_type": "short",
+                    "margin_liquidation_price": D("0.00000246"),
+                    "margin_borrowed_base_price": D("0.00599149"),
+                    "margin_borrowed_base_currency": "BTC"
+                    } 
+            expected_btc = {"total": D("0.05432165"),
+                    "exchange_used": D("0E-8"),
+                    "exchange_total": D("0.00005959"),
+                    "exchange_free": D("0.00005959"),
+                    "margin_available": D("0.03019227"),
+                    "margin_in_position": D("0.02406979"),
+                    "margin_borrowed": 0,
+                    "margin_total": D("0.05426206"),
+                    "margin_pending_gain": D("0.00093955"),
+                    "margin_lending_fees": 0,
+                    "margin_pending_base_gain": 0,
+                    "margin_position_type": None,
+                    "margin_liquidation_price": 0,
+                    "margin_borrowed_base_price": 0,
+                    "margin_borrowed_base_currency": None
+                    }
+            expected_xmr = {"total": D("0.18719303"),
+                    "exchange_used": D("0E-8"),
+                    "exchange_total": D("0.18719303"),
+                    "exchange_free": D("0.18719303"),
+                    "margin_available": 0,
+                    "margin_in_position": 0,
+                    "margin_borrowed": 0,
+                    "margin_total": 0,
+                    "margin_pending_gain": 0,
+                    "margin_lending_fees": 0,
+                    "margin_pending_base_gain": 0,
+                    "margin_position_type": None,
+                    "margin_liquidation_price": 0,
+                    "margin_borrowed_base_price": 0,
+                    "margin_borrowed_base_currency": None
+                    } 
+            self.assertEqual(expected_xmr, result["XMR"])
+            self.assertEqual(expected_doge, result["DOGE"])
+            self.assertEqual(expected_btc, result["BTC"])
+
+    def test_create_margin_order(self):
+        with self.assertRaises(market.ExchangeError):
+            self.s.create_margin_order("FOO", "market", "buy", "10")
+
+        with mock.patch.object(self.s, "load_markets") as load_markets,\
+                mock.patch.object(self.s, "privatePostMarginBuy") as margin_buy,\
+                mock.patch.object(self.s, "privatePostMarginSell") as margin_sell,\
+                mock.patch.object(self.s, "market") as market_mock,\
+                mock.patch.object(self.s, "price_to_precision") as ptp,\
+                mock.patch.object(self.s, "amount_to_precision") as atp:
+
+            margin_buy.return_value = {
+                    "orderNumber": 123
+                    }
+            margin_sell.return_value = {
+                    "orderNumber": 456
+                    }
+            market_mock.return_value = { "id": "BTC_ETC", "symbol": "BTC_ETC" }
+            ptp.return_value = D("0.1")
+            atp.return_value = D("12")
+
+            order = self.s.create_margin_order("BTC_ETC", "margin", "buy", "12", price="0.1")
+            self.assertEqual(123, order["id"])
+            margin_buy.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")})
+            margin_sell.assert_not_called()
+            margin_buy.reset_mock()
+            margin_sell.reset_mock()
+
+            order = self.s.create_margin_order("BTC_ETC", "margin", "sell", "12", lending_rate="0.01", price="0.1")
+            self.assertEqual(456, order["id"])
+            margin_sell.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12"), "lendingRate": "0.01"})
+            margin_buy.assert_not_called()
+
+    def test_create_exchange_order(self):
+        with mock.patch.object(market.ccxt.poloniex, "create_order") as create_order:
+            self.s.create_order("symbol", "type", "side", "amount", price="price", params="params")
+
+            create_order.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params")
+
 @unittest.skipUnless("unit" in limits, "Unit skipped")
-class PortfolioTest(WebMockTestCase):
-    def fill_data(self):
-        if self.json_response is not None:
-            portfolio.Portfolio.data = self.json_response
+class NoopLockTest(unittest.TestCase):
+    def test_with(self):
+        noop_lock = store.NoopLock()
+        with noop_lock:
+            self.assertTrue(True)
+
+@unittest.skipUnless("unit" in limits, "Unit skipped")
+class LockedVar(unittest.TestCase):
+
+    def test_values(self):
+        locked_var = store.LockedVar("Foo")
+        self.assertIsInstance(locked_var.lock, store.NoopLock)
+        self.assertEqual("Foo", locked_var.val)
+
+    def test_get(self):
+        with self.subTest(desc="Normal case"):
+            locked_var = store.LockedVar("Foo")
+            self.assertEqual("Foo", locked_var.get())
+        with self.subTest(desc="Dict"):
+            locked_var = store.LockedVar({"foo": "bar"})
+            self.assertEqual({"foo": "bar"}, locked_var.get())
+            self.assertEqual("bar", locked_var.get("foo"))
+            self.assertIsNone(locked_var.get("other"))
+
+    def test_set(self):
+        locked_var = store.LockedVar("Foo")
+        locked_var.set("Bar")
+        self.assertEqual("Bar", locked_var.get())
+
+    def test__getattr(self):
+        dummy = type('Dummy', (object,), {})()
+        dummy.attribute = "Hey"
+
+        locked_var = store.LockedVar(dummy)
+        self.assertEqual("Hey", locked_var.attribute)
+        with self.assertRaises(AttributeError):
+            locked_var.other
+
+    def test_start_lock(self):
+        locked_var = store.LockedVar("Foo")
+        locked_var.start_lock()
+        self.assertEqual("lock", locked_var.lock.__class__.__name__)
+
+        thread1 = threading.Thread(target=locked_var.set, args=["Bar1"])
+        thread2 = threading.Thread(target=locked_var.set, args=["Bar2"])
+        thread3 = threading.Thread(target=locked_var.set, args=["Bar3"])
+
+        with locked_var.lock:
+            thread1.start()
+            thread2.start()
+            thread3.start()
+
+            self.assertEqual("Foo", locked_var.val)
+        thread1.join()
+        thread2.join()
+        thread3.join()
+        self.assertEqual("Bar", locked_var.get()[0:3])
+
+    def test_wait_for_notification(self):
+        with self.assertRaises(RuntimeError):
+            store.Portfolio.wait_for_notification()
+
+        with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\
+                mock.patch.object(store.Portfolio, "report") as report,\
+                mock.patch.object(store.time, "sleep") as sleep:
+            store.Portfolio.start_worker(poll=3)
+
+            store.Portfolio.worker_notify.set()
+
+            store.Portfolio.callback.wait()
+
+            report.print_log.assert_called_once_with("Fetching cryptoportfolio")
+            get.assert_called_once_with(refetch=True)
+            sleep.assert_called_once_with(3)
+            self.assertFalse(store.Portfolio.worker_notify.is_set())
+            self.assertTrue(store.Portfolio.worker.is_alive())
+
+            store.Portfolio.callback.clear()
+            store.Portfolio.worker_started = False
+            store.Portfolio.worker_notify.set()
+            store.Portfolio.callback.wait()
+
+            self.assertFalse(store.Portfolio.worker.is_alive())
+
+    def test_notify_and_wait(self):
+        with mock.patch.object(store.Portfolio, "callback") as callback,\
+                mock.patch.object(store.Portfolio, "worker_notify") as worker_notify:
+            store.Portfolio.notify_and_wait()
+            callback.clear.assert_called_once_with()
+            worker_notify.set.assert_called_once_with()
+            callback.wait.assert_called_once_with()
 
+@unittest.skipUnless("unit" in limits, "Unit skipped")
+class PortfolioTest(WebMockTestCase):
     def setUp(self):
         super(PortfolioTest, self).setUp()
 
-        with open("test_portfolio.json") as example:
+        with open("test_samples/test_portfolio.json") as example:
             self.json_response = example.read()
 
-        self.wm.get(portfolio.Portfolio.URL, text=self.json_response)
+        self.wm.get(market.Portfolio.URL, text=self.json_response)
 
-    def test_get_cryptoportfolio(self):
-        self.wm.get(portfolio.Portfolio.URL, [
-            {"text":'{ "foo": "bar" }', "status_code": 200},
-            {"text": "System Error", "status_code": 500},
-            {"exc": requests.exceptions.ConnectTimeout},
-            ])
-        portfolio.Portfolio.get_cryptoportfolio(self.m)
-        self.assertIn("foo", portfolio.Portfolio.data)
-        self.assertEqual("bar", portfolio.Portfolio.data["foo"])
-        self.assertTrue(self.wm.called)
-        self.assertEqual(1, self.wm.call_count)
-        self.m.report.log_error.assert_not_called()
-        self.m.report.log_http_request.assert_called_once()
-        self.m.report.log_http_request.reset_mock()
-
-        portfolio.Portfolio.get_cryptoportfolio(self.m)
-        self.assertIsNone(portfolio.Portfolio.data)
-        self.assertEqual(2, self.wm.call_count)
-        self.m.report.log_error.assert_not_called()
-        self.m.report.log_http_request.assert_called_once()
-        self.m.report.log_http_request.reset_mock()
-
-
-        portfolio.Portfolio.data = "Foo"
-        portfolio.Portfolio.get_cryptoportfolio(self.m)
-        self.assertEqual("Foo", portfolio.Portfolio.data)
-        self.assertEqual(3, self.wm.call_count)
-        self.m.report.log_error.assert_called_once_with("get_cryptoportfolio",
-                exception=mock.ANY)
-        self.m.report.log_http_request.assert_not_called()
+    @mock.patch.object(market.Portfolio, "parse_cryptoportfolio")
+    def test_get_cryptoportfolio(self, parse_cryptoportfolio):
+        with self.subTest(parallel=False):
+            self.wm.get(market.Portfolio.URL, [
+                {"text":'{ "foo": "bar" }', "status_code": 200},
+                {"text": "System Error", "status_code": 500},
+                {"exc": requests.exceptions.ConnectTimeout},
+                ])
+            market.Portfolio.get_cryptoportfolio()
+            self.assertIn("foo", market.Portfolio.data.get())
+            self.assertEqual("bar", market.Portfolio.data.get()["foo"])
+            self.assertTrue(self.wm.called)
+            self.assertEqual(1, self.wm.call_count)
+            market.Portfolio.report.log_error.assert_not_called()
+            market.Portfolio.report.log_http_request.assert_called_once()
+            parse_cryptoportfolio.assert_called_once_with()
+            market.Portfolio.report.log_http_request.reset_mock()
+            parse_cryptoportfolio.reset_mock()
+            market.Portfolio.data = store.LockedVar(None)
+
+            market.Portfolio.get_cryptoportfolio()
+            self.assertIsNone(market.Portfolio.data.get())
+            self.assertEqual(2, self.wm.call_count)
+            parse_cryptoportfolio.assert_not_called()
+            market.Portfolio.report.log_error.assert_not_called()
+            market.Portfolio.report.log_http_request.assert_called_once()
+            market.Portfolio.report.log_http_request.reset_mock()
+            parse_cryptoportfolio.reset_mock()
+
+            market.Portfolio.data = store.LockedVar("Foo")
+            market.Portfolio.get_cryptoportfolio()
+            self.assertEqual(2, self.wm.call_count)
+            parse_cryptoportfolio.assert_not_called()
+
+            market.Portfolio.get_cryptoportfolio(refetch=True)
+            self.assertEqual("Foo", market.Portfolio.data.get())
+            self.assertEqual(3, self.wm.call_count)
+            market.Portfolio.report.log_error.assert_called_once_with("get_cryptoportfolio",
+                    exception=mock.ANY)
+            market.Portfolio.report.log_http_request.assert_not_called()
+        with self.subTest(parallel=True):
+            with mock.patch.object(market.Portfolio, "is_worker_thread") as is_worker,\
+                    mock.patch.object(market.Portfolio, "notify_and_wait") as notify:
+                with self.subTest(worker=True):
+                    market.Portfolio.data = store.LockedVar(None)
+                    market.Portfolio.worker = mock.Mock()
+                    is_worker.return_value = True
+                    self.wm.get(market.Portfolio.URL, [
+                        {"text":'{ "foo": "bar" }', "status_code": 200},
+                        ])
+                    market.Portfolio.get_cryptoportfolio()
+                    self.assertIn("foo", market.Portfolio.data.get())
+                parse_cryptoportfolio.reset_mock()
+                with self.subTest(worker=False):
+                    market.Portfolio.data = store.LockedVar(None)
+                    market.Portfolio.worker = mock.Mock()
+                    is_worker.return_value = False
+                    market.Portfolio.get_cryptoportfolio()
+                    notify.assert_called_once_with()
+                    parse_cryptoportfolio.assert_not_called()
 
     def test_parse_cryptoportfolio(self):
-        portfolio.Portfolio.parse_cryptoportfolio(self.m)
-
-        self.assertListEqual(
-                ["medium", "high"],
-                list(portfolio.Portfolio.liquidities.keys()))
-
-        liquidities = portfolio.Portfolio.liquidities
-        self.assertEqual(10, len(liquidities["medium"].keys()))
-        self.assertEqual(10, len(liquidities["high"].keys()))
-
-        expected = {
-                'BTC':  (D("0.2857"), "long"),
-                'DGB':  (D("0.1015"), "long"),
-                'DOGE': (D("0.1805"), "long"),
-                'SC':   (D("0.0623"), "long"),
-                'ZEC':  (D("0.3701"), "long"),
-                }
-        date = portfolio.datetime(2018, 1, 8)
-        self.assertDictEqual(expected, liquidities["high"][date])
-
-        expected = {
-                'BTC':  (D("1.1102e-16"), "long"),
-                'ETC':  (D("0.1"), "long"),
-                'FCT':  (D("0.1"), "long"),
-                'GAS':  (D("0.1"), "long"),
-                'NAV':  (D("0.1"), "long"),
-                'OMG':  (D("0.1"), "long"),
-                'OMNI': (D("0.1"), "long"),
-                'PPC':  (D("0.1"), "long"),
-                'RIC':  (D("0.1"), "long"),
-                'VIA':  (D("0.1"), "long"),
-                'XCP':  (D("0.1"), "long"),
-                }
-        self.assertDictEqual(expected, liquidities["medium"][date])
-        self.assertEqual(portfolio.datetime(2018, 1, 15), portfolio.Portfolio.last_date)
-
-        self.m.report.log_http_request.assert_called_once_with("GET",
-                portfolio.Portfolio.URL, None, mock.ANY, mock.ANY)
-        self.m.report.log_http_request.reset_mock()
-
-        # It doesn't refetch the data when available
-        portfolio.Portfolio.parse_cryptoportfolio(self.m)
-        self.m.report.log_http_request.assert_not_called()
-
-        self.assertEqual(1, self.wm.call_count)
-
-        portfolio.Portfolio.parse_cryptoportfolio(self.m, refetch=True)
-        self.assertEqual(2, self.wm.call_count)
-        self.m.report.log_http_request.assert_called_once()
-
-    def test_repartition(self):
-        expected_medium = {
-                'BTC':   (D("1.1102e-16"), "long"),
-                'USDT':  (D("0.1"), "long"),
-                'ETC':   (D("0.1"), "long"),
-                'FCT':   (D("0.1"), "long"),
-                'OMG':   (D("0.1"), "long"),
-                'STEEM': (D("0.1"), "long"),
-                'STRAT': (D("0.1"), "long"),
-                'XEM':   (D("0.1"), "long"),
-                'XMR':   (D("0.1"), "long"),
-                'XVC':   (D("0.1"), "long"),
-                'ZRX':   (D("0.1"), "long"),
-                }
-        expected_high = {
-                'USDT': (D("0.1226"), "long"),
-                'BTC':  (D("0.1429"), "long"),
-                'ETC':  (D("0.1127"), "long"),
-                'ETH':  (D("0.1569"), "long"),
-                'FCT':  (D("0.3341"), "long"),
-                'GAS':  (D("0.1308"), "long"),
-                }
+        with self.subTest(description="Normal case"):
+            market.Portfolio.data = store.LockedVar(store.json.loads(
+                self.json_response, parse_int=D, parse_float=D))
+            market.Portfolio.parse_cryptoportfolio()
 
-        self.assertEqual(expected_medium, portfolio.Portfolio.repartition(self.m))
-        self.assertEqual(expected_medium, portfolio.Portfolio.repartition(self.m, liquidity="medium"))
-        self.assertEqual(expected_high, portfolio.Portfolio.repartition(self.m, liquidity="high"))
+            self.assertListEqual(
+                    ["medium", "high"],
+                    list(market.Portfolio.liquidities.get().keys()))
 
-        self.assertEqual(1, self.wm.call_count)
+            liquidities = market.Portfolio.liquidities.get()
+            self.assertEqual(10, len(liquidities["medium"].keys()))
+            self.assertEqual(10, len(liquidities["high"].keys()))
 
-        portfolio.Portfolio.repartition(self.m)
-        self.assertEqual(1, self.wm.call_count)
+            expected = {
+                    'BTC':  (D("0.2857"), "long"),
+                    'DGB':  (D("0.1015"), "long"),
+                    'DOGE': (D("0.1805"), "long"),
+                    'SC':   (D("0.0623"), "long"),
+                    'ZEC':  (D("0.3701"), "long"),
+                    }
+            date = portfolio.datetime(2018, 1, 8)
+            self.assertDictEqual(expected, liquidities["high"][date])
 
-        portfolio.Portfolio.repartition(self.m, refetch=True)
-        self.assertEqual(2, self.wm.call_count)
-        self.m.report.log_http_request.assert_called()
-        self.assertEqual(2, self.m.report.log_http_request.call_count)
+            expected = {
+                    'BTC':  (D("1.1102e-16"), "long"),
+                    'ETC':  (D("0.1"), "long"),
+                    'FCT':  (D("0.1"), "long"),
+                    'GAS':  (D("0.1"), "long"),
+                    'NAV':  (D("0.1"), "long"),
+                    'OMG':  (D("0.1"), "long"),
+                    'OMNI': (D("0.1"), "long"),
+                    'PPC':  (D("0.1"), "long"),
+                    'RIC':  (D("0.1"), "long"),
+                    'VIA':  (D("0.1"), "long"),
+                    'XCP':  (D("0.1"), "long"),
+                    }
+            self.assertDictEqual(expected, liquidities["medium"][date])
+            self.assertEqual(portfolio.datetime(2018, 1, 15), market.Portfolio.last_date.get())
+
+        with self.subTest(description="Missing weight"):
+            data = store.json.loads(self.json_response, parse_int=D, parse_float=D)
+            del(data["portfolio_2"]["weights"])
+            market.Portfolio.data = store.LockedVar(data)
+
+            market.Portfolio.parse_cryptoportfolio()
+            self.assertListEqual(
+                    ["medium", "high"],
+                    list(market.Portfolio.liquidities.get().keys()))
+            self.assertEqual({}, market.Portfolio.liquidities.get("medium"))
+
+        with self.subTest(description="All missing weights"):
+            data = store.json.loads(self.json_response, parse_int=D, parse_float=D)
+            del(data["portfolio_1"]["weights"])
+            del(data["portfolio_2"]["weights"])
+            market.Portfolio.data = store.LockedVar(data)
+
+            market.Portfolio.parse_cryptoportfolio()
+            self.assertEqual({}, market.Portfolio.liquidities.get("medium"))
+            self.assertEqual({}, market.Portfolio.liquidities.get("high"))
+            self.assertEqual(datetime.datetime(1,1,1), market.Portfolio.last_date.get())
+
+
+    @mock.patch.object(market.Portfolio, "get_cryptoportfolio")
+    def test_repartition(self, get_cryptoportfolio):
+        market.Portfolio.liquidities = store.LockedVar({
+                "medium": {
+                    "2018-03-01": "medium_2018-03-01",
+                    "2018-03-08": "medium_2018-03-08",
+                    },
+                "high": {
+                    "2018-03-01": "high_2018-03-01",
+                    "2018-03-08": "high_2018-03-08",
+                    }
+                })
+        market.Portfolio.last_date = store.LockedVar("2018-03-08")
 
-    @mock.patch.object(portfolio.time, "sleep")
-    @mock.patch.object(portfolio.Portfolio, "repartition")
-    def test_wait_for_recent(self, repartition, sleep):
+        self.assertEqual("medium_2018-03-08", market.Portfolio.repartition())
+        get_cryptoportfolio.assert_called_once_with()
+        self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium"))
+        self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high"))
+
+    @mock.patch.object(market.time, "sleep")
+    @mock.patch.object(market.Portfolio, "get_cryptoportfolio")
+    def test_wait_for_recent(self, get_cryptoportfolio, sleep):
         self.call_count = 0
-        def _repartition(market, refetch):
-            self.assertEqual(self.m, market)
-            self.assertTrue(refetch)
+        def _get(refetch=False):
+            if self.call_count != 0:
+                self.assertTrue(refetch)
+            else:
+                self.assertFalse(refetch)
             self.call_count += 1
-            portfolio.Portfolio.last_date = portfolio.datetime.now()\
-                - portfolio.timedelta(10)\
-                + portfolio.timedelta(self.call_count)
-        repartition.side_effect = _repartition
+            market.Portfolio.last_date = store.LockedVar(store.datetime.now()\
+                - store.timedelta(10)\
+                + store.timedelta(self.call_count))
+        get_cryptoportfolio.side_effect = _get
 
-        portfolio.Portfolio.wait_for_recent(self.m)
+        market.Portfolio.wait_for_recent()
         sleep.assert_called_with(30)
         self.assertEqual(6, sleep.call_count)
-        self.assertEqual(7, repartition.call_count)
-        self.m.report.print_log.assert_called_with("Attempt to fetch up-to-date cryptoportfolio")
+        self.assertEqual(7, get_cryptoportfolio.call_count)
+        market.Portfolio.report.print_log.assert_called_with("Attempt to fetch up-to-date cryptoportfolio")
 
         sleep.reset_mock()
-        repartition.reset_mock()
-        portfolio.Portfolio.last_date = None
+        get_cryptoportfolio.reset_mock()
+        market.Portfolio.last_date = store.LockedVar(None)
         self.call_count = 0
-        portfolio.Portfolio.wait_for_recent(self.m, delta=15)
+        market.Portfolio.wait_for_recent(delta=15)
         sleep.assert_not_called()
-        self.assertEqual(1, repartition.call_count)
+        self.assertEqual(1, get_cryptoportfolio.call_count)
 
         sleep.reset_mock()
-        repartition.reset_mock()
-        portfolio.Portfolio.last_date = None
+        get_cryptoportfolio.reset_mock()
+        market.Portfolio.last_date = store.LockedVar(None)
         self.call_count = 0
-        portfolio.Portfolio.wait_for_recent(self.m, delta=1)
+        market.Portfolio.wait_for_recent(delta=1)
         sleep.assert_called_with(30)
         self.assertEqual(9, sleep.call_count)
-        self.assertEqual(10, repartition.call_count)
+        self.assertEqual(10, get_cryptoportfolio.call_count)
+
+    def test_is_worker_thread(self):
+        with self.subTest(worker=None):
+            self.assertFalse(store.Portfolio.is_worker_thread())
+
+        with self.subTest(worker="not self"),\
+                mock.patch("threading.current_thread") as current_thread:
+            current = mock.Mock()
+            current_thread.return_value = current
+            store.Portfolio.worker = mock.Mock()
+            self.assertFalse(store.Portfolio.is_worker_thread())
+
+        with self.subTest(worker="self"),\
+                mock.patch("threading.current_thread") as current_thread:
+            current = mock.Mock()
+            current_thread.return_value = current
+            store.Portfolio.worker = current
+            self.assertTrue(store.Portfolio.is_worker_thread())
+
+    def test_start_worker(self):
+        with mock.patch.object(store.Portfolio, "wait_for_notification") as notification:
+            store.Portfolio.start_worker()
+            notification.assert_called_once_with(poll=30)
+
+            self.assertEqual("lock", store.Portfolio.last_date.lock.__class__.__name__)
+            self.assertEqual("lock", store.Portfolio.liquidities.lock.__class__.__name__)
+            store.Portfolio.report.start_lock.assert_called_once_with()
+
+            self.assertIsNotNone(store.Portfolio.worker)
+            self.assertIsNotNone(store.Portfolio.worker_notify)
+            self.assertIsNotNone(store.Portfolio.callback)
+            self.assertTrue(store.Portfolio.worker_started)
 
 @unittest.skipUnless("unit" in limits, "Unit skipped")
 class AmountTest(WebMockTestCase):
@@ -736,7 +1203,7 @@ class MarketTest(WebMockTestCase):
         self.assertEqual("Foo", m.fetch_fees())
         self.ccxt.fetch_fees.assert_not_called()
 
-    @mock.patch.object(portfolio.Portfolio, "repartition")
+    @mock.patch.object(market.Portfolio, "repartition")
     @mock.patch.object(market.Market, "get_ticker")
     @mock.patch.object(market.TradeStore, "compute_trades")
     def test_prepare_trades(self, compute_trades, get_ticker, repartition):
@@ -787,7 +1254,7 @@ class MarketTest(WebMockTestCase):
             m.report.log_balances.assert_called_once_with(tag="tag")
 
 
-    @mock.patch.object(portfolio.time, "sleep")
+    @mock.patch.object(market.time, "sleep")
     @mock.patch.object(market.TradeStore, "all_orders")
     def test_follow_orders(self, all_orders, time_mock):
         for debug, sleep in [
@@ -907,7 +1374,172 @@ 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()
+        m = market.Market(self.ccxt, user_id=1)
+        with self.subTest(file=None),\
+                mock.patch.object(m, "report") as report,\
+                mock.patch("market.open", file_open):
+            m.store_report()
+            report.merge.assert_called_with(store.Portfolio.report)
+            file_open.assert_not_called()
+
+        report.reset_mock()
+        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()
+            report.merge.assert_called_with(store.Portfolio.report)
+
+        report.reset_mock()
+
+        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.object(m, "report") as report,\
+                mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+            file_open.side_effect = FileNotFoundError
+
+            m.store_report()
+
+            report.merge.assert_called_with(store.Portfolio.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):
@@ -1226,7 +1858,7 @@ class BalanceStoreTest(WebMockTestCase):
         self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies()))
         self.m.report.log_balances.assert_called_with(tag="foo")
 
-    @mock.patch.object(portfolio.Portfolio, "repartition")
+    @mock.patch.object(market.Portfolio, "repartition")
     def test_dispatch_assets(self, repartition):
         self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance
 
@@ -1243,7 +1875,7 @@ class BalanceStoreTest(WebMockTestCase):
         repartition.return_value = repartition_hash
 
         amounts = balance_store.dispatch_assets(portfolio.Amount("BTC", "11.1"))
-        repartition.assert_called_with(self.m, liquidity="medium")
+        repartition.assert_called_with(liquidity="medium")
         self.assertIn("XEM", balance_store.currencies())
         self.assertEqual(D("2.6"), amounts["BTC"].value)
         self.assertEqual(D("7.5"), amounts["XEM"].value)
@@ -2372,6 +3004,19 @@ class ReportStoreTest(WebMockTestCase):
             report_store.set_verbose(False)
             self.assertFalse(report_store.verbose_print)
 
+    def test_merge(self):
+        report_store1 = market.ReportStore(self.m, verbose_print=False)
+        report_store2 = market.ReportStore(None, verbose_print=False)
+
+        report_store2.log_stage("1")
+        report_store1.log_stage("2")
+        report_store2.log_stage("3")
+
+        report_store1.merge(report_store2)
+
+        self.assertEqual(3, len(report_store1.logs))
+        self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs)))
+
     def test_print_log(self):
         report_store = market.ReportStore(self.m)
         with self.subTest(verbose=True),\
@@ -2790,7 +3435,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,
@@ -2800,7 +3445,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"),
@@ -2825,7 +3470,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]
@@ -2834,7 +3479,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"),
@@ -2854,7 +3499,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)
@@ -2862,14 +3507,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)
@@ -2879,19 +3524,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)
@@ -2899,141 +3544,100 @@ 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_process(self):
+        with 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)
-
-            self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;")
-
-    @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)
-
-            process.assert_not_called()
-
-        process.reset_mock()
-        with self.subTest(before=True, after=False):
-            helper.main_process_market(m, None, before=True)
-
-            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")
+            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"
+            self.assertEqual("", stdout_mock.getvalue())
 
-        process.reset_mock()
-        with self.subTest(before=True, after=True):
-            helper.main_process_market(m, None, before=True, after=True)
+            main.process("config", 1, "report_path", args_mock)
 
-            process.assert_has_calls([
-                mock.call("sell_all", steps="before"),
-                mock.call("sell_all", steps="after"),
+            market_mock.from_config.assert_has_calls([
+                mock.call("config", debug="debug", user_id=1, 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"])
+            with self.subTest(exception=True):
+                market_mock.from_config.side_effect = Exception("boo")
+                main.process("config", 1, "report_path", args_mock)
+                self.assertEqual("Exception: boo\n", stdout_mock.getvalue())
 
-    @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))
+    def test_main(self):
+        with self.subTest(parallel=False):
+            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("main.process") as process:
 
-            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)
+                args_mock = mock.Mock()
+                args_mock.parallel = False
+                args_mock.config = "config"
+                args_mock.user = "user"
+                parse_args.return_value = args_mock
 
-            self.assertEqual(["row_1", "row_2"], rows)
+                parse_config.return_value = ["pg_config", "report_path"]
 
-    @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)
+                fetch_markets.return_value = [["config1", 1], ["config2", 2]]
 
-            args = helper.main_parse_args(["--before", "--after", "--debug"])
-            self.assertTrue(args.before)
-            self.assertTrue(args.after)
-            self.assertTrue(args.debug)
+                main.main(["Foo", "Bar"])
 
-            exit.assert_not_called()
+                parse_args.assert_called_with(["Foo", "Bar"])
+                parse_config.assert_called_with("config")
+                fetch_markets.assert_called_with("pg_config", "user")
 
-        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())
+                self.assertEqual(2, process.call_count)
+                process.assert_has_calls([
+                    mock.call("config1", 1, "report_path", args_mock),
+                    mock.call("config2", 2, "report_path", args_mock),
+                    ])
+        with self.subTest(parallel=True):
+            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("main.process") as process,\
+                    mock.patch("store.Portfolio.start_worker") as start:
+
+                args_mock = mock.Mock()
+                args_mock.parallel = True
+                args_mock.config = "config"
+                args_mock.user = "user"
+                parse_args.return_value = args_mock
+
+                parse_config.return_value = ["pg_config", "report_path"]
+
+                fetch_markets.return_value = [["config1", 1], ["config2", 2]]
+
+                main.main(["Foo", "Bar"])
+
+                parse_args.assert_called_with(["Foo", "Bar"])
+                parse_config.assert_called_with("config")
+                fetch_markets.assert_called_with("pg_config", "user")
+
+                start.assert_called_once_with()
+                self.assertEqual(2, process.call_count)
+                process.assert_has_calls([
+                    mock.call.__bool__(),
+                    mock.call("config1", 1, "report_path", args_mock),
+                    mock.call.__bool__(),
+                    mock.call("config2", 2, "report_path", args_mock),
+                    ])
 
-    @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
@@ -3043,7 +3647,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")
 
@@ -3061,7 +3665,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)
@@ -3072,46 +3676,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()
@@ -3125,10 +3754,10 @@ class ProcessorTest(WebMockTestCase):
 
             processor.run_action("wait_for_recent", "bar", "baz")
 
-            method_mock.assert_called_with(self.m, foo="bar")
+            method_mock.assert_called_with(foo="bar")
 
     def test_select_step(self):
-        processor = helper.Processor(self.m)
+        processor = market.Processor(self.m)
 
         scenario = processor.scenarios["sell_all"]
 
@@ -3141,9 +3770,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)
@@ -3164,11 +3793,11 @@ 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)
-        self.assertEqual(["delta"], arguments)
+        self.assertEqual(market.Portfolio.wait_for_recent, method)
+        self.assertEqual(["delta", "poll"], arguments)
 
         method, arguments = processor.method_arguments("prepare_trades")
         self.assertEqual(m.prepare_trades, method)
@@ -3190,7 +3819,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]
@@ -3224,7 +3853,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()
@@ -3350,7 +3979,7 @@ class AcceptanceTest(WebMockTestCase):
         market = mock.Mock()
         market.fetch_all_balances.return_value = fetch_balance
         market.fetch_ticker.side_effect = fetch_ticker
-        with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition):
+        with mock.patch.object(market.Portfolio, "repartition", return_value=repartition):
             # Action 1
             helper.prepare_trades(market)
 
@@ -3429,7 +4058,7 @@ class AcceptanceTest(WebMockTestCase):
                     "amount": "10", "total": "1"
                     }
                 ]
-        with mock.patch.object(portfolio.time, "sleep") as sleep:
+        with mock.patch.object(market.time, "sleep") as sleep:
             # Action 4
             helper.follow_orders(verbose=False)
 
@@ -3470,7 +4099,7 @@ class AcceptanceTest(WebMockTestCase):
                 }
         market.fetch_all_balances.return_value = fetch_balance
 
-        with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition):
+        with mock.patch.object(market.Portfolio, "repartition", return_value=repartition):
             # Action 5
             helper.prepare_trades(market, only="acquire", compute_value="average")
 
@@ -3542,7 +4171,7 @@ class AcceptanceTest(WebMockTestCase):
         # TODO
         # portfolio.TradeStore.run_orders()
 
-        with mock.patch.object(portfolio.time, "sleep") as sleep:
+        with mock.patch.object(market.time, "sleep") as sleep:
             # Action 8
             helper.follow_orders(verbose=False)
 
diff --git a/test_samples/poloniexETest.test_fetch_all_balances.1.json b/test_samples/poloniexETest.test_fetch_all_balances.1.json
new file mode 100644 (file)
index 0000000..b04648c
--- /dev/null
@@ -0,0 +1,1459 @@
+{
+    "1CR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ABY": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "AC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ACH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ADN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "AEON": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "AERO": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "AIR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "AMP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "APH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ARCH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ARDR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "AUR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "AXIS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BALLS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BANK": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BBL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BBR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BCC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BCH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BCN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BCY": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BDC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BDG": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BELA": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BITCNY": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BITS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BITUSD": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BLK": {
+        "available": "159.83673869",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00562305"
+    },
+    "BLOCK": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BLU": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BNS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BONES": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BOST": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BTC": {
+        "available": "0.03025186",
+        "onOrders": "0.00000000",
+        "btcValue": "0.03025186"
+    },
+    "BTCD": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BTCS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BTM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BTS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BURN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "BURST": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "C2": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CACH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CAI": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CCN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CGA": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CHA": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CINNI": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CLAM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CNL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CNMT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CNOTE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "COMM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CON": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CORG": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CRYPT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CURE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CVC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "CYC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DAO": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DASH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DCR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DGB": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DICE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DIEM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DIME": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DIS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DNS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DOGE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DRKC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DRM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DSH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "DVK": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "EAC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "EBT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ECC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "EFL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "EMC2": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "EMO": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ENC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ETC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ETH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "eTOK": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "EXE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "EXP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FAC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FCN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FCT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FIBRE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FLAP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FLDC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FLO": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FLT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FOX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FRAC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FRK": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FRQ": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FVZ": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FZ": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "FZN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GAME": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GAP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GAS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GDN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GEMZ": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GEO": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GIAR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GLB": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GML": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GNO": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GNS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GNT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GOLD": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GPC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GPUC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GRC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GRCX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GRS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "GUE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "H2O": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "HIRO": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "HOT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "HUC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "HUGE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "HVC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "HYP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "HZ": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "IFC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "INDEX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "IOC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ITC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "IXC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "JLH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "JPC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "JUG": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "KDC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "KEY": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LBC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LCL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LEAF": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LGC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LOL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LOVE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LQD": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LSK": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LTBC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LTC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "LTCX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MAID": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MAST": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MAX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MCN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MEC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "METH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MIL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MIN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MINT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MMC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MMNXT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MMXIV": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MNTA": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MON": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MRC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MRS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MTS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MUN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MYR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "MZC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "N5X": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NAS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NAUT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NAV": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NBT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NEOS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NMC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NOBL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NOTE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NOXT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NRS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NSR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NTX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NXC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NXT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "NXTI": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "OMG": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "OMNI": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "OPAL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PAND": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PASC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PAWN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PIGGY": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PINK": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PLX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PMC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "POT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PPC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PRC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PRT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "PTS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "Q2C": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "QBK": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "QCN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "QORA": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "QTL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "RADS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "RBY": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "RDD": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "REP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "RIC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "RZR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SBD": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SDC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SHIBE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SHOPX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SILK": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SJCX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SLR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SMC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SOC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SPA": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SQL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SRCC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SRG": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SSD": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "STEEM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "STORJ": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "STR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "STRAT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SUM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SUN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SWARM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SXC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SYNC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "SYS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "TAC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "TOR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "TRUST": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "TWE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "UIS": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ULTC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "UNITY": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "URO": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "USDE": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "USDT": {
+        "available": "0.00002625",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "UTC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "UTIL": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "UVC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "VIA": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "VOOT": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "VOX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "VRC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "VTC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "WC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "WDC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "WIKI": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "WOLF": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "X13": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XAI": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XAP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XBC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XCH": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XCN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XCP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XCR": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XDN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XDP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XEM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XHC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XLB": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XMG": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XMR": {
+        "available": "0.18719303",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00598102"
+    },
+    "XPB": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XPM": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XRP": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XSI": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XST": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XSV": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XUSD": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XVC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "XXC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "YACC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "YANG": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "YC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "YIN": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ZEC": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    },
+    "ZRX": {
+        "available": "0.00000000",
+        "onOrders": "0.00000000",
+        "btcValue": "0.00000000"
+    }
+}
+
+
diff --git a/test_samples/poloniexETest.test_fetch_all_balances.2.json b/test_samples/poloniexETest.test_fetch_all_balances.2.json
new file mode 100644 (file)
index 0000000..3d2f741
--- /dev/null
@@ -0,0 +1,101 @@
+{
+    "BTC_BTS": {
+        "amount": "-309.05320945",
+        "total": "0.00602465",
+        "basePrice": "0.00001949",
+        "liquidationPrice": "0.00010211",
+        "pl": "0.00024394",
+        "lendingFees": "0.00000000",
+        "type": "short"
+    },
+    "BTC_CLAM": {
+        "type": "none",
+        "amount": "0.00000000",
+        "total": "0.00000000",
+        "basePrice": "0.00000000",
+        "liquidationPrice": -1,
+        "pl": "0.00000000",
+        "lendingFees": "0.00000000"
+    },
+    "BTC_DASH": {
+        "amount": "-0.11204647",
+        "total": "0.00602391",
+        "basePrice": "0.05376260",
+        "liquidationPrice": "0.28321001",
+        "pl": "0.00006304",
+        "lendingFees": "0.00000000",
+        "type": "short"
+    },
+    "BTC_DOGE": {
+        "amount": "-12779.79821852",
+        "total": "0.00599149",
+        "basePrice": "0.00000046",
+        "liquidationPrice": "0.00000246",
+        "pl": "0.00024059",
+        "lendingFees": "-0.00000009",
+        "type": "short"
+    },
+    "BTC_LTC": {
+        "type": "none",
+        "amount": "0.00000000",
+        "total": "0.00000000",
+        "basePrice": "0.00000000",
+        "liquidationPrice": -1,
+        "pl": "0.00000000",
+        "lendingFees": "0.00000000"
+    },
+    "BTC_MAID": {
+        "type": "none",
+        "amount": "0.00000000",
+        "total": "0.00000000",
+        "basePrice": "0.00000000",
+        "liquidationPrice": -1,
+        "pl": "0.00000000",
+        "lendingFees": "0.00000000"
+    },
+    "BTC_STR": {
+        "type": "none",
+        "amount": "0.00000000",
+        "total": "0.00000000",
+        "basePrice": "0.00000000",
+        "liquidationPrice": -1,
+        "pl": "0.00000000",
+        "lendingFees": "0.00000000"
+    },
+    "BTC_XMR": {
+        "type": "none",
+        "amount": "0.00000000",
+        "total": "0.00000000",
+        "basePrice": "0.00000000",
+        "liquidationPrice": -1,
+        "pl": "0.00000000",
+        "lendingFees": "0.00000000"
+    },
+    "BTC_XRP": {
+        "amount": "-68.66952474",
+        "total": "0.00602974",
+        "basePrice": "0.00008780",
+        "liquidationPrice": "0.00045756",
+        "pl": "0.00039198",
+        "lendingFees": "0.00000000",
+        "type": "short"
+    },
+    "BTC_ETH": {
+        "type": "none",
+        "amount": "0.00000000",
+        "total": "0.00000000",
+        "basePrice": "0.00000000",
+        "liquidationPrice": -1,
+        "pl": "0.00000000",
+        "lendingFees": "0.00000000"
+    },
+    "BTC_FCT": {
+        "type": "none",
+        "amount": "0.00000000",
+        "total": "0.00000000",
+        "basePrice": "0.00000000",
+        "liquidationPrice": -1,
+        "pl": "0.00000000",
+        "lendingFees": "0.00000000"
+    }
+}
diff --git a/test_samples/poloniexETest.test_fetch_all_balances.3.json b/test_samples/poloniexETest.test_fetch_all_balances.3.json
new file mode 100644 (file)
index 0000000..e805f6f
--- /dev/null
@@ -0,0 +1,11 @@
+{
+    "exchange": {
+        "BLK": "159.83673869",
+        "BTC": "0.00005959",
+        "USDT": "0.00002625",
+        "XMR": "0.18719303"
+    },
+    "margin": {
+        "BTC": "0.03019227"
+    }
+}