import ccxt
import time
+from decimal import Decimal as D
# Put your poloniex api key in market.py
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 = {}
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
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
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:
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)
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:
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):
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: