X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=portfolio.py;h=4991dced7d437b63c99ee3b70c6d02d93fbaf98e;hb=f0e4f1828138f86cee54d9f8c4414228840207aa;hp=b0f9256e5222ee75a3e43c09ad5b7f5d8fe2c720;hpb=089d5d9df3d9d93e3ce789be16ee6cdd99dc2b52;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/portfolio.py b/portfolio.py index b0f9256..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: @@ -266,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): @@ -416,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: