]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - market.py
Merge branch 'dev'
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / market.py
index 5876071875750df01a1d658698216b2c30670bb0..9612b17406de5b45bec1e6dabb59e3e3c870d16d 100644 (file)
--- a/market.py
+++ b/market.py
@@ -1,4 +1,4 @@
-from ccxt import ExchangeError, NotSupported, RequestTimeout, InvalidNonce
+from ccxt import AuthenticationError, ExchangeError, NotSupported, RequestTimeout, InvalidNonce
 import ccxt_wrapper as ccxt
 import time
 import dbs
@@ -15,6 +15,7 @@ class Market:
     report = None
     trades = None
     balances = None
+    options = None
 
     def __init__(self, ccxt_instance, args, **kwargs):
         self.args = args
@@ -26,6 +27,7 @@ class Market:
         self.balances = BalanceStore(self)
         self.processor = Processor(self)
 
+        self.options = kwargs.get("options", {})
         for key in ["user_id", "market_id"]:
             setattr(self, key, kwargs.get(key, None))
 
@@ -88,15 +90,19 @@ class Market:
 
     def process(self, actions, before=False, after=False):
         try:
+            self.ccxt.check_required_credentials()
             for action in actions:
                 if bool(before) is bool(after):
-                    self.processor.process(action, steps="all")
+                    self.processor.process(action, steps="all", options=self.options)
                 elif before:
-                    self.processor.process(action, steps="before")
+                    self.processor.process(action, steps="before", options=self.options)
                 elif after:
-                    self.processor.process(action, steps="after")
+                    self.processor.process(action, steps="after", options=self.options)
+        except AuthenticationError:
+            self.report.log_error("market_authentication", message="Impossible to authenticate to market")
         except Exception as e:
-            self.report.log_error("market_process", exception=e)
+            import traceback
+            self.report.log_error("market_process", exception=e, message=traceback.format_exc())
         finally:
             self.store_report()
 
@@ -200,8 +206,11 @@ class Market:
                 if status == "error_disappeared":
                     self.report.log_error("follow_orders",
                             message="{} disappeared, recreating it".format(order))
-                    order.trade.prepare_order(
+                    new_order = order.trade.prepare_order(
                             compute_value=order.trade.tick_actions_recreate(tick))
+                    if new_order is not None:
+                        new_order.run()
+                        self.report.log_order(order, tick, new_order=new_order)
 
         self.report.log_stage("follow_orders_end")
 
@@ -214,23 +223,20 @@ class Market:
                 compute_value=compute_value, only=only,
                 repartition=repartition, available_balance_only=available_balance_only)
 
-        values_in_base = self.balances.in_currency(base_currency,
-                compute_value=compute_value)
         if available_balance_only:
-            balance = self.balances.all.get(base_currency)
-            if balance is None:
-                total_base_value = portfolio.Amount(base_currency, 0)
-            else:
-                total_base_value = balance.exchange_free + balance.margin_available
+            repartition, total_base_value, values_in_base = self.balances.available_balances_for_repartition(
+                    base_currency=base_currency, liquidity=liquidity,
+                    repartition=repartition, compute_value=compute_value)
         else:
+            values_in_base = self.balances.in_currency(base_currency,
+                    compute_value=compute_value)
             total_base_value = sum(values_in_base.values())
         new_repartition = self.balances.dispatch_assets(total_base_value,
                 liquidity=liquidity, repartition=repartition)
         if available_balance_only:
             for currency, amount in values_in_base.items():
-                if currency != base_currency:
-                    new_repartition.setdefault(currency, portfolio.Amount(base_currency, 0))
-                    new_repartition[currency] += amount
+                if currency != base_currency and currency not in new_repartition:
+                    new_repartition[currency] = amount
 
         self.trades.compute_trades(values_in_base, new_repartition, only=only)
 
@@ -254,8 +260,11 @@ class Processor:
                 {
                     "name": "print_balances",
                     "number": 1,
-                    "fetch_balances": ["begin"],
-                    "fetch_balances_args": { "add_portfolio": True },
+                    "fetch_balances_begin": {
+                        "log_tickers": True,
+                        "add_usdt": True,
+                        "add_portfolio": True
+                        },
                     "print_tickers": { "base_currency": "BTC" },
                     }
                 ],
@@ -272,25 +281,38 @@ class Processor:
                     "number": 2,
                     "before": False,
                     "after": True,
-                    "fetch_balances": ["begin"],
+                    "fetch_balances_begin": {},
                     "prepare_trades": { "compute_value": "average" },
                     "prepare_orders": { "compute_value": "average" },
                     },
                 ],
             "sell_needed": [
                 {
-                    "name": "wait",
+                    "name": "print_balances",
                     "number": 0,
+                    "before": True,
+                    "after": False,
+                    "fetch_balances_begin": {
+                        "checkpoint": "end",
+                        "log_tickers": True,
+                        "add_usdt": True,
+                        "add_portfolio": True
+                        },
+                    },
+                {
+                    "name": "wait",
+                    "number": 1,
                     "before": False,
                     "after": True,
                     "wait_for_recent": {},
                     },
                 {
                     "name": "sell",
-                    "number": 1,
+                    "number": 2,
                     "before": False,
                     "after": True,
-                    "fetch_balances": ["begin", "end"],
+                    "fetch_balances_begin": {},
+                    "fetch_balances_end": {},
                     "prepare_trades": {},
                     "prepare_orders": { "only": "dispose", "compute_value": "average" },
                     "run_orders": {},
@@ -299,10 +321,15 @@ class Processor:
                     },
                 {
                     "name": "buy",
-                    "number": 2,
+                    "number": 3,
                     "before": False,
                     "after": True,
-                    "fetch_balances": ["begin", "end"],
+                    "fetch_balances_begin": {},
+                    "fetch_balances_end": {
+                        "checkpoint": "begin",
+                        "add_usdt": True,
+                        "log_tickers": True
+                        },
                     "prepare_trades": { "only": "acquire", "available_balance_only": True },
                     "prepare_orders": { "only": "acquire", "compute_value": "average" },
                     "move_balances": {},
@@ -317,7 +344,13 @@ class Processor:
                     "number": 1,
                     "before": True,
                     "after": False,
-                    "fetch_balances": ["begin", "end"],
+                    "fetch_balances_begin": {
+                        "checkpoint": "end",
+                        "log_tickers": True,
+                        "add_usdt": True,
+                        "add_portfolio": True
+                        },
+                    "fetch_balances_end": {},
                     "prepare_trades": { "repartition": { "base_currency": (1, "long") } },
                     "prepare_orders": { "compute_value": "average" },
                     "run_orders": {},
@@ -336,7 +369,12 @@ class Processor:
                     "number": 3,
                     "before": False,
                     "after": True,
-                    "fetch_balances": ["begin", "end"],
+                    "fetch_balances_begin": {},
+                    "fetch_balances_end": {
+                        "checkpoint": "begin",
+                        "add_usdt": True,
+                        "log_tickers": True
+                        },
                     "prepare_trades": { "available_balance_only": True },
                     "prepare_orders": { "compute_value": "average" },
                     "move_balances": {},
@@ -370,7 +408,7 @@ class Processor:
     def can_process(self, scenario_name):
         return scenario_name in self.scenarios
 
-    def process(self, scenario_name, steps="all", **kwargs):
+    def process(self, scenario_name, steps="all", options={}):
         if not self.can_process(scenario_name):
             raise TypeError("Unknown scenario {}".format(scenario_name))
         scenario = self.scenarios[scenario_name]
@@ -382,24 +420,24 @@ class Processor:
             for step in steps:
                 selected_steps += self.select_steps(scenario, step)
         for step in selected_steps:
-            self.process_step(scenario_name, step, kwargs)
+            self.process_step(scenario_name, step, options)
 
-    def process_step(self, scenario_name, step, kwargs):
+    def process_step(self, scenario_name, step, options):
         process_name = "process_{}__{}_{}".format(scenario_name, step["number"], step["name"])
         self.market.report.log_stage("{}_begin".format(process_name))
 
-        fetch_args = step.get("fetch_balances_args", {})
-        if "begin" in step.get("fetch_balances", []):
-            self.market.balances.fetch_balances(tag="{}_begin".format(process_name),
-                    log_tickers=True, **fetch_args)
+        if "fetch_balances_begin" in step:
+            self.run_action("fetch_balances", step["fetch_balances_begin"],
+                    dict(options, tag="{}_begin".format(process_name)))
 
         for action in self.ordered_actions:
             if action in step:
-                self.run_action(action, step[action], kwargs)
+                self.run_action(action, step[action], options)
+
+        if "fetch_balances_end" in step:
+            self.run_action("fetch_balances", step["fetch_balances_end"],
+                    dict(options, tag="{}_end".format(process_name)))
 
-        if "end" in step.get("fetch_balances", []):
-            self.market.balances.fetch_balances(tag="{}_end".format(process_name),
-                    log_tickers=True, **fetch_args)
         self.market.report.log_stage("{}_end".format(process_name))
 
     def method_arguments(self, action):
@@ -421,6 +459,8 @@ class Processor:
             method = self.market.trades.close_trades
         elif action == "print_tickers":
             method = self.market.print_tickers
+        elif action == "fetch_balances":
+            method = self.market.balances.fetch_balances
 
         signature = inspect.getfullargspec(method)
         defaults = signature.defaults or []
@@ -428,9 +468,9 @@ class Processor:
 
         return [method, kwargs]
 
-    def parse_args(self, action, default_args, kwargs):
+    def parse_args(self, action, default_args, options):
         method, allowed_arguments = self.method_arguments(action)
-        args = {k: v for k, v in {**default_args, **kwargs}.items() if k in allowed_arguments }
+        args = {k: v for k, v in {**default_args, **options}.items() if k in allowed_arguments }
 
         if "repartition" in args and "base_currency" in args["repartition"]:
             r = args["repartition"]
@@ -438,7 +478,7 @@ class Processor:
 
         return method, args
 
-    def run_action(self, action, default_args, kwargs):
-        method, args = self.parse_args(action, default_args, kwargs)
+    def run_action(self, action, default_args, options):
+        method, args = self.parse_args(action, default_args, options)
 
         method(**args)