]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - market.py
Add USDT rate to balances
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / market.py
index 6edd6050b6ba3ef6aa2ddb02ce30dbf1e4b11707..caa951330dac039fe4170d597394b50989eadb17 100644 (file)
--- a/market.py
+++ b/market.py
@@ -96,7 +96,8 @@ class Market:
                 elif after:
                     self.processor.process(action, steps="after")
         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 +201,10 @@ 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))
+                    new_order.run()
+                    self.report.log_order(order, tick, new_order=new_order)
 
         self.report.log_stage("follow_orders_end")
 
@@ -254,7 +257,11 @@ class Processor:
                 {
                     "name": "print_balances",
                     "number": 1,
-                    "fetch_balances": ["begin"],
+                    "fetch_balances_begin": {
+                        "log_tickers": True,
+                        "add_usdt": True,
+                        "add_portfolio": True
+                        },
                     "print_tickers": { "base_currency": "BTC" },
                     }
                 ],
@@ -271,25 +278,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": {},
@@ -298,10 +318,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": {},
@@ -316,7 +341,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": {},
@@ -335,7 +366,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": {},
@@ -386,15 +422,19 @@ class Processor:
     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), log_tickers=True)
+
+        if "fetch_balances_begin" in step:
+            self.market.balances.fetch_balances(tag="{}_begin".format(process_name),
+                    **step["fetch_balances_begin"])
 
         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), log_tickers=True)
+        if "fetch_balances_end" in step:
+            self.market.balances.fetch_balances(tag="{}_end".format(process_name),
+                    **step["fetch_balances_end"])
+
         self.market.report.log_stage("{}_end".format(process_name))
 
     def method_arguments(self, action):