X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=portfolio.py;h=4991dced7d437b63c99ee3b70c6d02d93fbaf98e;hb=f0e4f1828138f86cee54d9f8c4414228840207aa;hp=576a2286847abacabe7525161bf9e11be4170e91;hpb=cfab619d9223fc824649a6fe16863931f5e43891;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/portfolio.py b/portfolio.py index 576a228..4991dce 100644 --- a/portfolio.py +++ b/portfolio.py @@ -1,5 +1,6 @@ import ccxt import time +from decimal import Decimal as D # Put your poloniex api key in market.py from market import market @@ -7,12 +8,6 @@ from market import market # FIXME: J'essayais d'utiliser plus de bitcoins que j'en avais à disposition # FIXME: better compute moves to avoid rounding errors -def static_var(varname, value): - def decorate(func): - setattr(func, varname, value) - return func - return decorate - class Portfolio: URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json" liquidities = {} @@ -37,7 +32,9 @@ class Portfolio: except Exception: return try: - cls.data = json.loads(r.data) + cls.data = json.loads(r.data, + parse_int=D, + parse_float=D) except json.JSONDecodeError: cls.data = None @@ -83,22 +80,15 @@ class Portfolio: class Amount: MAX_DIGITS = 18 - def __init__(self, currency, value, int_val=None, linked_to=None, ticker=None): + def __init__(self, currency, value, linked_to=None, ticker=None): self.currency = currency - if int_val is None: - self._value = int(value * 10**self.MAX_DIGITS) - else: - self._value = int_val + self.value = D(value) self.linked_to = linked_to self.ticker = ticker self.ticker_cache = {} self.ticker_cache_timestamp = time.time() - @property - def value(self): - return self._value / 10 ** self.MAX_DIGITS - def in_currency(self, other_currency, market, action="average"): if other_currency == self.currency: return self @@ -106,20 +96,19 @@ class Amount: if asset_ticker is not None: return Amount( other_currency, - 0, - int_val=int(self._value * asset_ticker[action]), + self.value * asset_ticker[action], linked_to=self, ticker=asset_ticker) else: raise Exception("This asset is not available in the chosen market") def __abs__(self): - return Amount(self.currency, 0, int_val=abs(self._value)) + return Amount(self.currency, abs(self.value)) def __add__(self, other): - if other.currency != self.currency and other._value * self._value != 0: + if other.currency != self.currency and other.value * self.value != 0: raise Exception("Summing amounts must be done with same currencies") - return Amount(self.currency, 0, int_val=self._value + other._value) + return Amount(self.currency, self.value + other.value) def __radd__(self, other): if other == 0: @@ -128,25 +117,22 @@ class Amount: return self.__add__(other) def __sub__(self, other): - if other.currency != self.currency and other._value * self._value != 0: + if other.currency != self.currency and other.value * self.value != 0: raise Exception("Summing amounts must be done with same currencies") - return Amount(self.currency, 0, int_val=self._value - other._value) - - def __int__(self): - return self._value + return Amount(self.currency, self.value - other.value) def __mul__(self, value): - if type(value) != int and type(value) != float: + if type(value) != int and type(value) != float and type(value) != D: raise TypeError("Amount may only be multiplied by numbers") - return Amount(self.currency, 0, int_val=(self._value * value)) + return Amount(self.currency, self.value * value) def __rmul__(self, value): return self.__mul__(value) def __floordiv__(self, value): - if type(value) != int: + if type(value) != int and type(value) != float and type(value) != D: raise TypeError("Amount may only be multiplied by integers") - return Amount(self.currency, 0, int_val=(self._value // value)) + return Amount(self.currency, self.value / value) def __truediv__(self, value): return self.__floordiv__(value) @@ -154,14 +140,14 @@ class Amount: def __lt__(self, other): if self.currency != other.currency: raise Exception("Comparing amounts must be done with same currencies") - return self._value < other._value + return self.value < other.value def __eq__(self, other): if other == 0: - return self._value == 0 + return self.value == 0 if self.currency != other.currency: raise Exception("Comparing amounts must be done with same currencies") - return self._value == other._value + return self.value == other.value def __str__(self): if self.linked_to is None: @@ -247,14 +233,11 @@ class Trade: self.value_from = value_from self.value_to = value_to self.orders = [] + self.market = market assert self.value_from.currency == self.value_to.currency assert self.value_from.linked_to is not None and self.value_from.linked_to.currency == self.currency self.base_currency = self.value_from.currency - if market is not None: - self.market = market - self.prepare_order(market) - fees_cache = {} @classmethod def fetch_fees(cls, market): @@ -269,7 +252,7 @@ class Trade: def invert(ticker): return { "inverted": True, - "average": (float(1/ticker["bid"]) + float(1/ticker["ask"]) ) / 2, + "average": (1/ticker["bid"] + 1/ticker["ask"]) / 2, "original": ticker, } def augment_ticker(ticker): @@ -310,6 +293,7 @@ class Trade: currency, market=market ) + cls.trades[currency].prepare_order() return cls.trades @property @@ -330,7 +314,7 @@ class Trade: else: return "bid" if not inverted else "ask" - def prepare_order(self, market): + def prepare_order(self): if self.action is None: return ticker = self.value_from.ticker @@ -338,7 +322,7 @@ class Trade: if not inverted: value_from = self.value_from.linked_to - value_to = self.value_to.in_currency(self.currency, market) + value_to = self.value_to.in_currency(self.currency, self.market) delta = abs(value_to - value_from) currency = self.base_currency else: @@ -418,6 +402,8 @@ class Order: def print_orders(market, base_currency="BTC"): Balance.prepare_trades(market, base_currency=base_currency) + for currency, balance in Balance.known_balances.items(): + print(balance) for currency, trade in Trade.trades.items(): print(trade) for order in trade.orders: