]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - portfolio.py
Change integer to decimals
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / portfolio.py
index b0f9256e5222ee75a3e43c09ad5b7f5d8fe2c720..e1ee1f8671c4bc9d479b15e01f5e2f14572d7804 100644 (file)
@@ -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
 
@@ -37,7 +38,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 +86,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 +102,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 +123,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 +146,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 +258,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 +408,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: