]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - portfolio.py
Separate store and add helper
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / portfolio.py
index 45fbef98e72fefac5104223955a1b1897f632131..efd9b84e498e20f39f0eb5a7e35d464d2b81614a 100644 (file)
@@ -1,14 +1,13 @@
-from ccxt import ExchangeError
 import time
 from decimal import Decimal as D, ROUND_DOWN
 # Put your poloniex api key in market.py
-from market import market
 from json import JSONDecodeError
 import requests
+import helper as h
+from store import *
 
 # FIXME: correctly handle web call timeouts
 
-
 class Portfolio:
     URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
     liquidities = {}
@@ -69,6 +68,24 @@ class Portfolio:
                 "high":   high_liquidity,
                 }
 
+class Computation:
+    computations = {
+            "default": lambda x, y: x[y],
+            "average": lambda x, y: x["average"],
+            "bid": lambda x, y: x["bid"],
+            "ask": lambda x, y: x["ask"],
+            }
+
+    @classmethod
+    def compute_value(cls, ticker, action, compute_value="default"):
+        if action == "buy":
+            action = "ask"
+        if action == "sell":
+            action = "bid"
+        if isinstance(compute_value, str):
+            compute_value = cls.computations[compute_value]
+        return compute_value(ticker, action)
+
 class Amount:
     def __init__(self, currency, value, linked_to=None, ticker=None, rate=None):
         self.currency = currency
@@ -86,9 +103,9 @@ class Amount:
                     self.value * rate,
                     linked_to=self,
                     rate=rate)
-        asset_ticker = Trade.get_ticker(self.currency, other_currency, market)
+        asset_ticker = h.get_ticker(self.currency, other_currency, market)
         if asset_ticker is not None:
-            rate = Trade.compute_value(asset_ticker, action, compute_value=compute_value)
+            rate = Computation.compute_value(asset_ticker, action, compute_value=compute_value)
             return Amount(
                     other_currency,
                     self.value * rate,
@@ -180,7 +197,6 @@ class Amount:
             return "Amount({:.8f} {} -> {})".format(self.value, self.currency, repr(self.linked_to))
 
 class Balance:
-    known_balances = {}
 
     def __init__(self, currency, hash_):
         self.currency = currency
@@ -201,69 +217,6 @@ class Balance:
                     ]:
                 setattr(self, key, Amount(base_currency, hash_.get(key, 0)))
 
-    @classmethod
-    def in_currency(cls, other_currency, market, compute_value="average", type="total"):
-        amounts = {}
-        for currency in cls.known_balances:
-            balance = cls.known_balances[currency]
-            other_currency_amount = getattr(balance, type)\
-                    .in_currency(other_currency, market, compute_value=compute_value)
-            amounts[currency] = other_currency_amount
-        return amounts
-
-    @classmethod
-    def currencies(cls):
-        return cls.known_balances.keys()
-
-    @classmethod
-    def fetch_balances(cls, market):
-        all_balances = market.fetch_all_balances()
-        for currency, balance in all_balances.items():
-            if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \
-                    currency in cls.known_balances:
-                cls.known_balances[currency] = cls(currency, balance)
-        return cls.known_balances
-
-    @classmethod
-    def dispatch_assets(cls, amount, repartition=None):
-        if repartition is None:
-            repartition = Portfolio.repartition()
-        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 cls.known_balances:
-                cls.known_balances[currency] = cls(currency, {})
-        return amounts
-
-    @classmethod
-    def prepare_trades(cls, market, base_currency="BTC", compute_value="average", debug=False):
-        cls.fetch_balances(market)
-        values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value)
-        total_base_value = sum(values_in_base.values())
-        new_repartition = cls.dispatch_assets(total_base_value)
-        # Recompute it in case we have new currencies
-        values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value)
-        Trade.compute_trades(values_in_base, new_repartition, market=market, debug=debug)
-
-    @classmethod
-    def update_trades(cls, market, base_currency="BTC", compute_value="average", only=None, debug=False):
-        cls.fetch_balances(market)
-        values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value)
-        total_base_value = sum(values_in_base.values())
-        new_repartition = cls.dispatch_assets(total_base_value)
-        Trade.compute_trades(values_in_base, new_repartition, only=only, market=market, debug=debug)
-
-    @classmethod
-    def prepare_trades_to_sell_all(cls, market, base_currency="BTC", compute_value="average", debug=False):
-        cls.fetch_balances(market)
-        values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value)
-        total_base_value = sum(values_in_base.values())
-        new_repartition = cls.dispatch_assets(total_base_value, repartition={ base_currency: (1, "long") })
-        Trade.compute_trades(values_in_base, new_repartition, market=market, debug=debug)
-
     def __repr__(self):
         if self.exchange_total > 0:
             if self.exchange_free > 0 and self.exchange_used > 0:
@@ -296,18 +249,7 @@ class Balance:
 
         return "Balance({}".format(self.currency) + "".join([exchange, margin, total]) + ")"
 
-class Computation:
-    computations = {
-            "default": lambda x, y: x[y],
-            "average": lambda x, y: x["average"],
-            "bid": lambda x, y: x["bid"],
-            "ask": lambda x, y: x["ask"],
-            }
-
 class Trade:
-    debug = False
-    trades = []
-
     def __init__(self, value_from, value_to, currency, market=None):
         # We have value_from of currency, and want to finish with value_to of
         # that currency. value_* may not be in currency's terms
@@ -323,105 +265,6 @@ class Trade:
             self.value_from.linked_to = Amount(self.currency, 0)
         self.base_currency = self.value_from.currency
 
-    fees_cache = {}
-    @classmethod
-    def fetch_fees(cls, market):
-        if market.__class__ not in cls.fees_cache:
-            cls.fees_cache[market.__class__] = market.fetch_fees()
-        return cls.fees_cache[market.__class__]
-
-    ticker_cache = {}
-    ticker_cache_timestamp = time.time()
-    @classmethod
-    def get_ticker(cls, c1, c2, market, refresh=False):
-        def invert(ticker):
-            return {
-                    "inverted": True,
-                    "average": (1/ticker["bid"] + 1/ticker["ask"]) / 2,
-                    "original": ticker,
-                    }
-        def augment_ticker(ticker):
-            ticker.update({
-                "inverted": False,
-                "average": (ticker["bid"] + ticker["ask"] ) / 2,
-                })
-
-        if time.time() - cls.ticker_cache_timestamp > 5:
-            cls.ticker_cache = {}
-            cls.ticker_cache_timestamp = time.time()
-        elif not refresh:
-            if (c1, c2, market.__class__) in cls.ticker_cache:
-                return cls.ticker_cache[(c1, c2, market.__class__)]
-            if (c2, c1, market.__class__) in cls.ticker_cache:
-                return invert(cls.ticker_cache[(c2, c1, market.__class__)])
-
-        try:
-            cls.ticker_cache[(c1, c2, market.__class__)] = market.fetch_ticker("{}/{}".format(c1, c2))
-            augment_ticker(cls.ticker_cache[(c1, c2, market.__class__)])
-        except ExchangeError:
-            try:
-                cls.ticker_cache[(c2, c1, market.__class__)] = market.fetch_ticker("{}/{}".format(c2, c1))
-                augment_ticker(cls.ticker_cache[(c2, c1, market.__class__)])
-            except ExchangeError:
-                cls.ticker_cache[(c1, c2, market.__class__)] = None
-        return cls.get_ticker(c1, c2, market)
-
-    @classmethod
-    def compute_trades(cls, values_in_base, new_repartition, only=None, market=None, debug=False):
-        cls.debug = cls.debug or debug
-        base_currency = sum(values_in_base.values()).currency
-        for currency in Balance.currencies():
-            if currency == base_currency:
-                continue
-            value_from = values_in_base.get(currency, Amount(base_currency, 0))
-            value_to = new_repartition.get(currency, Amount(base_currency, 0))
-            if value_from.value * value_to.value < 0:
-                trade_1 = cls(value_from, Amount(base_currency, 0), currency, market=market)
-                if only is None or trade_1.action == only:
-                    cls.trades.append(trade_1)
-                trade_2 = cls(Amount(base_currency, 0), value_to, currency, market=market)
-                if only is None or trade_2.action == only:
-                    cls.trades.append(trade_2)
-            else:
-                trade = cls(
-                    value_from,
-                    value_to,
-                    currency,
-                    market=market
-                    )
-                if only is None or trade.action == only:
-                    cls.trades.append(trade)
-        return cls.trades
-
-    @classmethod
-    def prepare_orders(cls, only=None, compute_value="default"):
-        for trade in cls.trades:
-            if only is None or trade.action == only:
-                trade.prepare_order(compute_value=compute_value)
-
-    @classmethod
-    def move_balances(cls, market):
-        needed_in_margin = {} 
-        for trade in cls.trades:
-            if trade.trade_type == "short":
-                if trade.value_to.currency not in needed_in_margin:
-                    needed_in_margin[trade.value_to.currency] = 0
-                needed_in_margin[trade.value_to.currency] += abs(trade.value_to)
-        for currency, needed in needed_in_margin.items():
-            current_balance = Balance.known_balances[currency].margin_free
-            delta = (needed - current_balance).value
-            # FIXME: don't remove too much if there are open margin position
-            if delta > 0:
-                if cls.debug:
-                    print("market.transfer_balance({}, {}, 'exchange', 'margin')".format(currency, delta))
-                else:
-                    market.transfer_balance(currency, delta, "exchange", "margin")
-            elif delta < 0:
-                if cls.debug:
-                    print("market.transfer_balance({}, {}, 'margin', 'exchange')".format(currency, -delta))
-                else:
-                    market.transfer_balance(currency, -delta, "margin", "exchange")
-
     @property
     def action(self):
         if self.value_from == self.value_to:
@@ -481,11 +324,11 @@ class Trade:
     def prepare_order(self, compute_value="default"):
         if self.action is None:
             return
-        ticker = Trade.get_ticker(self.currency, self.base_currency, self.market)
+        ticker = h.get_ticker(self.currency, self.base_currency, self.market)
         inverted = ticker["inverted"]
         if inverted:
             ticker = ticker["original"]
-        rate = Trade.compute_value(ticker, self.order_action(inverted), compute_value=compute_value)
+        rate = Computation.compute_value(ticker, self.order_action(inverted), compute_value=compute_value)
         # 0.1
 
         delta_in_base = abs(self.value_from - self.value_to)
@@ -536,51 +379,6 @@ class Trade:
             delta - self.filled_amount, rate, currency, self.trade_type,
             self.market, self, close_if_possible=close_if_possible))
 
-    @classmethod
-    def compute_value(cls, ticker, action, compute_value="default"):
-        if action == "buy":
-            action = "ask"
-        if action == "sell":
-            action = "bid"
-        if isinstance(compute_value, str):
-            compute_value = Computation.computations[compute_value]
-        return compute_value(ticker, action)
-
-    @classmethod
-    def all_orders(cls, state=None):
-        all_orders = sum(map(lambda v: v.orders, cls.trades), [])
-        if state is None:
-            return all_orders
-        else:
-            return list(filter(lambda o: o.status == state, all_orders))
-
-    @classmethod
-    def run_orders(cls):
-        for order in cls.all_orders(state="pending"):
-            order.run()
-
-    @classmethod
-    def follow_orders(cls, verbose=True, sleep=None):
-        if sleep is None:
-            sleep = 7 if cls.debug else 30
-        tick = 0
-        while len(cls.all_orders(state="open")) > 0:
-            time.sleep(sleep)
-            tick += 1
-            for order in cls.all_orders(state="open"):
-                if order.get_status() != "open":
-                    if verbose:
-                        print("finished {}".format(order))
-                else:
-                    order.trade.update_order(order, tick)
-        if verbose:
-            print("All orders finished")
-
-    @classmethod
-    def update_all_orders_status(cls):
-        for order in cls.all_orders(state="open"):
-            order.get_status()
-
     def __repr__(self):
         return "Trade({} -> {} in {}, {})".format(
                 self.value_from,
@@ -588,11 +386,6 @@ class Trade:
                 self.currency,
                 self.action)
 
-    @classmethod
-    def print_all_with_order(cls):
-        for trade in cls.trades:
-            trade.print_with_order()
-
     def print_with_order(self):
         print(self)
         for order in self.orders:
@@ -612,7 +405,6 @@ class Order:
         self.status = "pending"
         self.trade = trade
         self.close_if_possible = close_if_possible
-        self.debug = trade.debug
 
     def __repr__(self):
         return "Order({} {} {} at {} {} [{}]{})".format(
@@ -648,7 +440,7 @@ class Order:
         symbol = "{}/{}".format(self.amount.currency, self.base_currency)
         amount = round(self.amount, self.market.order_precision(symbol)).value
 
-        if self.debug:
+        if TradeStore.debug:
             print("market.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(
                 symbol, self.action, amount, self.rate, self.account))
             self.status = "open"
@@ -665,7 +457,7 @@ class Order:
                 print(self.error_message)
 
     def get_status(self):
-        if self.debug:
+        if TradeStore.debug:
             return self.status
         # other states are "closed" and "canceled"
         if self.status == "open":
@@ -675,7 +467,7 @@ class Order:
         return self.status
 
     def mark_finished_order(self):
-        if self.debug:
+        if TradeStore.debug:
             return
         if self.status == "closed":
             if self.trade_type == "short" and self.action == "buy" and self.close_if_possible:
@@ -683,7 +475,7 @@ class Order:
 
     fetch_cache_timestamp = None
     def fetch(self, force=False):
-        if self.debug or (not force and self.fetch_cache_timestamp is not None
+        if TradeStore.debug or (not force and self.fetch_cache_timestamp is not None
                 and time.time() - self.fetch_cache_timestamp < 10):
             return
         self.fetch_cache_timestamp = time.time()
@@ -725,7 +517,7 @@ class Order:
                 self.base_currency, mouvement_hash))
 
     def cancel(self):
-        if self.debug:
+        if TradeStore.debug:
             self.status = "canceled"
             return
         self.market.cancel_order(self.result['id'])
@@ -744,45 +536,6 @@ class Mouvement:
         # rate * total = total_in_base
         self.total_in_base = Amount(base_currency, hash_["total"])
 
-def print_orders(market, base_currency="BTC"):
-    Balance.prepare_trades(market, base_currency=base_currency, compute_value="average")
-    Trade.prepare_orders(compute_value="average")
-    for currency, balance in Balance.known_balances.items():
-        print(balance)
-    Trade.print_all_with_order()
-
-def make_orders(market, base_currency="BTC"):
-    Balance.prepare_trades(market, base_currency=base_currency)
-    for trade in Trade.trades:
-        print(trade)
-        for order in trade.orders:
-            print("\t", order, sep="")
-            order.run()
-
-def process_sell_all_sell(market, base_currency="BTC", debug=False):
-    Balance.prepare_trades_to_sell_all(market, debug=debug)
-    Trade.prepare_orders(compute_value="average")
-    print("------------------")
-    for currency, balance in Balance.known_balances.items():
-        print(balance)
-    print("------------------")
-    Trade.print_all_with_order()
-    print("------------------")
-    Trade.run_orders()
-    Trade.follow_orders()
-
-def process_sell_all_buy(market, base_currency="BTC", debug=False):
-    Balance.prepare_trades(market, debug=debug)
-    Trade.prepare_orders()
-    print("------------------")
-    for currency, balance in Balance.known_balances.items():
-        print(balance)
-    print("------------------")
-    Trade.print_all_with_order()
-    print("------------------")
-    Trade.move_balances(market)
-    Trade.run_orders()
-    Trade.follow_orders()
-
 if __name__ == '__main__':
-    print_orders(market)
+    from market import market
+    h.print_orders(market)