]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - market.py
Add quiet flag for running
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / market.py
index 6c14ae208af3add2eb1bf65ab6627696945e8b22..fc5832c089fc6eb63a97ec1621dab65eca406a2e 100644 (file)
--- a/market.py
+++ b/market.py
@@ -13,11 +13,12 @@ class Market:
     trades = None
     balances = None
 
-    def __init__(self, ccxt_instance, debug=False, user_id=None, report_path=None):
-        self.debug = debug
+    def __init__(self, ccxt_instance, args, user_id=None, report_path=None):
+        self.args = args
+        self.debug = args.debug
         self.ccxt = ccxt_instance
         self.ccxt._market = self
-        self.report = ReportStore(self)
+        self.report = ReportStore(self, verbose_print=(not args.quiet))
         self.trades = TradeStore(self)
         self.balances = BalanceStore(self)
         self.processor = Processor(self)
@@ -26,7 +27,7 @@ class Market:
         self.report_path = report_path
 
     @classmethod
-    def from_config(cls, config, debug=False, user_id=None, report_path=None):
+    def from_config(cls, config, args, user_id=None, report_path=None):
         config["apiKey"] = config.pop("key", None)
 
         ccxt_instance = ccxt.poloniexE(config)
@@ -43,14 +44,17 @@ class Market:
         ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session,
                 ccxt_instance.session.__class__)
 
-        return cls(ccxt_instance, debug=debug, user_id=user_id, report_path=report_path)
+        return cls(ccxt_instance, args, 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:
+                report_file = "{}/{}_{}".format(self.report_path, datetime.now().isoformat(), self.user_id)
+                with open(report_file + ".json", "w") as f:
                     f.write(self.report.to_json())
+                with open(report_file + ".log", "w") as f:
+                    f.write("\n".join(map(lambda x: x[1], self.report.print_logs)))
         except Exception as e:
             print("impossible to store report file: {}; {}".format(e.__class__.__name__, e))
 
@@ -192,6 +196,33 @@ class Market:
 
 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",
@@ -312,7 +343,7 @@ class Processor:
         import inspect
 
         if action == "wait_for_recent":
-            method = portfolio.Portfolio.wait_for_recent
+            method = Portfolio.wait_for_recent
         elif action == "prepare_trades":
             method = self.market.prepare_trades
         elif action == "prepare_orders":
@@ -345,8 +376,4 @@ class Processor:
     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)
-
+        method(**args)