]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - store.py
Retry running order when available balance is insufficient
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / store.py
index 4e46878928d940522beceae46c9d08f37f40c636..bebd127f14f482010f52eb73407484b214f96cce 100644 (file)
--- a/store.py
+++ b/store.py
 import portfolio
+import simplejson as json
+from decimal import Decimal as D, ROUND_DOWN
+from datetime import date, datetime
 
-__all__ = ["BalanceStore", "TradeStore"]
+__all__ = ["BalanceStore", "ReportStore", "TradeStore"]
+
+class ReportStore:
+    def __init__(self, market, verbose_print=True):
+        self.market = market
+        self.verbose_print = verbose_print
+
+        self.logs = []
+
+    def print_log(self, message):
+        message = str(message)
+        if self.verbose_print:
+            print(message)
+
+    def add_log(self, hash_):
+        hash_["date"] = datetime.now()
+        self.logs.append(hash_)
+
+    def to_json(self):
+        def default_json_serial(obj):
+            if isinstance(obj, (datetime, date)):
+                return obj.isoformat()
+            return str(obj)
+        return json.dumps(self.logs, default=default_json_serial)
+
+    def set_verbose(self, verbose_print):
+        self.verbose_print = verbose_print
+
+    def log_stage(self, stage):
+        self.print_log("-" * (len(stage) + 8))
+        self.print_log("[Stage] {}".format(stage))
+
+        self.add_log({
+            "type": "stage",
+            "stage": stage,
+            })
+
+    def log_balances(self, tag=None):
+        self.print_log("[Balance]")
+        for currency, balance in self.market.balances.all.items():
+            self.print_log("\t{}".format(balance))
+
+        self.add_log({
+            "type": "balance",
+            "tag": tag,
+            "balances": self.market.balances.as_json()
+            })
+
+    def log_tickers(self, amounts, other_currency,
+            compute_value, type):
+        values = {}
+        rates = {}
+        for currency, amount in amounts.items():
+            values[currency] = amount.as_json()["value"]
+            rates[currency] = amount.rate
+        self.add_log({
+            "type": "tickers",
+            "compute_value": compute_value,
+            "balance_type": type,
+            "currency": other_currency,
+            "balances": values,
+            "rates": rates,
+            "total": sum(amounts.values()).as_json()["value"]
+            })
+
+    def log_dispatch(self, amount, amounts, liquidity, repartition):
+        self.add_log({
+            "type": "dispatch",
+            "liquidity": liquidity,
+            "repartition_ratio": repartition,
+            "total_amount": amount.as_json(),
+            "repartition": { k: v.as_json()["value"] for k, v in amounts.items() }
+            })
+
+    def log_trades(self, matching_and_trades, only):
+        trades = []
+        for matching, trade in matching_and_trades:
+            trade_json = trade.as_json()
+            trade_json["skipped"] = not matching
+            trades.append(trade_json)
+
+        self.add_log({
+            "type": "trades",
+            "only": only,
+            "debug": self.market.debug,
+            "trades": trades
+            })
+
+    def log_orders(self, orders, tick=None, only=None, compute_value=None):
+        self.print_log("[Orders]")
+        self.market.trades.print_all_with_order(ind="\t")
+        self.add_log({
+            "type": "orders",
+            "only": only,
+            "compute_value": compute_value,
+            "tick": tick,
+            "orders": [order.as_json() for order in orders if order is not None]
+            })
+
+    def log_order(self, order, tick, finished=False, update=None,
+            new_order=None, compute_value=None):
+        if finished:
+            self.print_log("[Order] Finished {}".format(order))
+        elif update == "waiting":
+            self.print_log("[Order] {}, tick {}, waiting".format(order, tick))
+        elif update == "adjusting":
+            self.print_log("[Order] {}, tick {}, cancelling and adjusting to {}".format(order, tick, new_order))
+        elif update == "market_fallback":
+            self.print_log("[Order] {}, tick {}, fallbacking to market value".format(order, tick))
+        elif update == "market_adjust":
+            self.print_log("[Order] {}, tick {}, market value, cancelling and adjusting to {}".format(order, tick, new_order))
+
+        self.add_log({
+            "type": "order",
+            "tick": tick,
+            "update": update,
+            "order": order.as_json(),
+            "compute_value": compute_value,
+            "new_order": new_order.as_json() if new_order is not None else None
+            })
+
+    def log_move_balances(self, needed, moving):
+        self.add_log({
+            "type": "move_balances",
+            "debug": self.market.debug,
+            "needed": { k: v.as_json()["value"] if isinstance(v, portfolio.Amount) else v for k, v in needed.items() },
+            "moving": { k: v.as_json()["value"] if isinstance(v, portfolio.Amount) else v for k, v in moving.items() },
+            })
+
+    def log_http_request(self, method, url, body, headers, response):
+        self.add_log({
+            "type": "http_request",
+            "method": method,
+            "url": url,
+            "body": body,
+            "headers": headers,
+            "status": response.status_code,
+            "response": response.text
+            })
+
+    def log_error(self, action, message=None, exception=None):
+        self.print_log("[Error] {}".format(action))
+        if exception is not None:
+            self.print_log(str("\t{}: {}".format(exception.__class__.__name__, exception)))
+        if message is not None:
+            self.print_log("\t{}".format(message))
+
+        self.add_log({
+            "type": "error",
+            "action": action,
+            "exception_class": exception.__class__.__name__ if exception is not None else None,
+            "exception_message": str(exception) if exception is not None else None,
+            "message": message,
+            })
+
+    def log_debug_action(self, action):
+        self.print_log("[Debug] {}".format(action))
+
+        self.add_log({
+            "type": "debug_action",
+            "action": action,
+            })
 
 class BalanceStore:
-    all = {}
+    def __init__(self, market):
+        self.market = market
+        self.all = {}
 
-    @classmethod
-    def currencies(cls):
-        return cls.all.keys()
+    def currencies(self):
+        return self.all.keys()
 
-    @classmethod
-    def in_currency(cls, other_currency, market, compute_value="average", type="total"):
+    def in_currency(self, other_currency, compute_value="average", type="total"):
         amounts = {}
-        for currency, balance in cls.all.items():
+        for currency, balance in self.all.items():
             other_currency_amount = getattr(balance, type)\
-                    .in_currency(other_currency, market, compute_value=compute_value)
+                    .in_currency(other_currency, self.market, compute_value=compute_value)
             amounts[currency] = other_currency_amount
+        self.market.report.log_tickers(amounts, other_currency,
+                compute_value, type)
         return amounts
 
-    @classmethod
-    def fetch_balances(cls, market):
-        all_balances = market.fetch_all_balances()
+    def fetch_balances(self, tag=None):
+        all_balances = self.market.ccxt.fetch_all_balances()
         for currency, balance in all_balances.items():
             if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \
-                    currency in cls.all:
-                cls.all[currency] = portfolio.Balance(currency, balance)
+                    currency in self.all:
+                self.all[currency] = portfolio.Balance(currency, balance)
+        self.market.report.log_balances(tag=tag)
 
-    @classmethod
-    def dispatch_assets(cls, amount, repartition=None):
+    def dispatch_assets(self, amount, liquidity="medium", repartition=None):
         if repartition is None:
-            repartition = portfolio.Portfolio.repartition()
+            repartition = portfolio.Portfolio.repartition(self.market, liquidity=liquidity)
         sum_ratio = sum([v[0] for k, v in repartition.items()])
         amounts = {}
         for currency, (ptt, trade_type) in repartition.items():
             amounts[currency] = ptt * amount / sum_ratio
             if trade_type == "short":
                 amounts[currency] = - amounts[currency]
-            if currency not in BalanceStore.all:
-                cls.all[currency] = portfolio.Balance(currency, {})
+            if currency not in self.all:
+                self.all[currency] = portfolio.Balance(currency, {})
+        self.market.report.log_dispatch(amount, amounts, liquidity, repartition)
         return amounts
 
+    def as_json(self):
+        return { k: v.as_json() for k, v in self.all.items() }
+
 class TradeStore:
-    all = []
-    debug = False
+    def __init__(self, market):
+        self.market = market
+        self.all = []
 
-    @classmethod
-    def compute_trades(cls, values_in_base, new_repartition, only=None, market=None, debug=False):
-        cls.debug = cls.debug or debug
+    def compute_trades(self, values_in_base, new_repartition, only=None):
+        computed_trades = []
         base_currency = sum(values_in_base.values()).currency
-        for currency in BalanceStore.currencies():
+        for currency in self.market.balances.currencies():
             if currency == base_currency:
                 continue
             value_from = values_in_base.get(currency, portfolio.Amount(base_currency, 0))
             value_to = new_repartition.get(currency, portfolio.Amount(base_currency, 0))
+
             if value_from.value * value_to.value < 0:
-                trade_1 = portfolio.Trade(value_from, portfolio.Amount(base_currency, 0), currency, market=market)
-                if only is None or trade_1.action == only:
-                    cls.all.append(trade_1)
-                trade_2 = portfolio.Trade(portfolio.Amount(base_currency, 0), value_to, currency, market=market)
-                if only is None or trade_2.action == only:
-                    cls.all.append(trade_2)
+                computed_trades.append(self.trade_if_matching(
+                        value_from, portfolio.Amount(base_currency, 0),
+                        currency, only=only))
+                computed_trades.append(self.trade_if_matching(
+                        portfolio.Amount(base_currency, 0), value_to,
+                        currency, only=only))
             else:
-                trade = portfolio.Trade(
-                    value_from,
-                    value_to,
-                    currency,
-                    market=market
-                    )
-                if only is None or trade.action == only:
-                    cls.all.append(trade)
-
-    @classmethod
-    def prepare_orders(cls, only=None, compute_value="default"):
-        for trade in cls.all:
+                computed_trades.append(self.trade_if_matching(
+                        value_from, value_to,
+                        currency, only=only))
+        for matching, trade in computed_trades:
+            if matching:
+                self.all.append(trade)
+        self.market.report.log_trades(computed_trades, only)
+
+    def trade_if_matching(self, value_from, value_to, currency,
+            only=None):
+        trade = portfolio.Trade(value_from, value_to, currency,
+                self.market)
+        matching = only is None or trade.action == only
+        return [matching, trade]
+
+    def prepare_orders(self, only=None, compute_value="default"):
+        orders = []
+        for trade in self.all:
             if only is None or trade.action == only:
-                trade.prepare_order(compute_value=compute_value)
+                orders.append(trade.prepare_order(compute_value=compute_value))
+        self.market.report.log_orders(orders, only, compute_value)
 
-    @classmethod
-    def print_all_with_order(cls):
-        for trade in cls.all:
-            trade.print_with_order()
+    def print_all_with_order(self, ind=""):
+        for trade in self.all:
+            trade.print_with_order(ind=ind)
 
-    @classmethod
-    def run_orders(cls):
-        for order in cls.all_orders(state="pending"):
+    def run_orders(self):
+        orders = self.all_orders(state="pending")
+        for order in orders:
             order.run()
+        self.market.report.log_stage("run_orders")
+        self.market.report.log_orders(orders)
 
-    @classmethod
-    def all_orders(cls, state=None):
-        all_orders = sum(map(lambda v: v.orders, cls.all), [])
+    def all_orders(self, state=None):
+        all_orders = sum(map(lambda v: v.orders, self.all), [])
         if state is None:
             return all_orders
         else:
             return list(filter(lambda o: o.status == state, all_orders))
 
-    @classmethod
-    def update_all_orders_status(cls):
-        for order in cls.all_orders(state="open"):
+    def update_all_orders_status(self):
+        for order in self.all_orders(state="open"):
             order.get_status()