]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - store.py
Move Portfolio to store and cleanup methods
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / store.py
index c581608ca9ad67338cea98a2ae86457011d375b2..78dfe2dcba77390d3fffa42df937c1704b393949 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):
@@ -27,18 +31,29 @@ class ReportStore:
             if isinstance(obj, (datetime, date)):
                 return obj.isoformat()
             return str(obj)
-        return json.dumps(self.logs, default=default_json_serial)
+        return json.dumps(self.logs, default=default_json_serial, indent="  ")
 
     def set_verbose(self, verbose_print):
         self.verbose_print = verbose_print
 
-    def log_stage(self, stage):
+    def log_stage(self, stage, **kwargs):
+        def as_json(element):
+            if callable(element):
+                return inspect.getsource(element).strip()
+            elif hasattr(element, "as_json"):
+                return element.as_json()
+            else:
+                return element
+
+        args = { k: as_json(v) for k, v in kwargs.items() }
+        args_str = ["{}={}".format(k, v) for k, v in args.items()]
         self.print_log("-" * (len(stage) + 8))
-        self.print_log("[Stage] {}".format(stage))
+        self.print_log("[Stage] {} {}".format(stage, ", ".join(args_str)))
 
         self.add_log({
             "type": "stage",
             "stage": stage,
+            "args": args,
             })
 
     def log_balances(self, tag=None):
@@ -202,7 +217,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():
@@ -223,7 +238,7 @@ class TradeStore:
 
     @property
     def pending(self):
-        return list(filter(lambda t: not t.is_fullfiled, self.all))
+        return list(filter(lambda t: t.pending, self.all))
 
     def compute_trades(self, values_in_base, new_repartition, only=None):
         computed_trades = []
@@ -264,6 +279,10 @@ class TradeStore:
                 orders.append(trade.prepare_order(compute_value=compute_value))
         self.market.report.log_orders(orders, only, compute_value)
 
+    def close_trades(self):
+        for trade in self.all:
+            trade.close()
+
     def print_all_with_order(self, ind=""):
         for trade in self.all:
             trade.print_with_order(ind=ind)
@@ -286,4 +305,79 @@ class TradeStore:
         for order in self.all_orders(state="open"):
             order.get_status()
 
+class Portfolio:
+    URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
+    liquidities = {}
+    data = None
+    last_date = None
+    report = ReportStore(None)
+
+    @classmethod
+    def wait_for_recent(cls, delta=4):
+        cls.get_cryptoportfolio()
+        while cls.last_date is None or datetime.now() - cls.last_date > timedelta(delta):
+            time.sleep(30)
+            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[liquidity]
+        return liquidities[cls.last_date]
+
+    @classmethod
+    def get_cryptoportfolio(cls, refetch=False):
+        if cls.data is not None and not refetch:
+            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 = r.json(parse_int=D, parse_float=D)
+            cls.parse_cryptoportfolio()
+        except (JSONDecodeError, SimpleJSONDecodeError):
+            cls.data = None
+            cls.liquidities = {}
+
+    @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):
+            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()))
+