X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=portfolio.py;h=c3809f0533db32cc3b62ccbd6c64a5ff5c66e651;hb=c31df868c655612b8387a25111e69882f0fe6344;hp=21a98342f15fa03ba7753be2a69579bda16cd7c9;hpb=f320eb8aafbceafbbfca02617db4846b3571e598;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/portfolio.py b/portfolio.py index 21a9834..c3809f0 100644 --- a/portfolio.py +++ b/portfolio.py @@ -1,5 +1,5 @@ import time -from datetime import datetime +from datetime import datetime, timedelta from decimal import Decimal as D, ROUND_DOWN # Put your poloniex api key in market.py from json import JSONDecodeError @@ -14,12 +14,19 @@ class Portfolio: URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json" liquidities = {} data = None + last_date = None @classmethod - def repartition(cls, liquidity="medium"): - cls.parse_cryptoportfolio() + def wait_for_recent(cls, delta=4): + cls.repartition(refetch=True) + while cls.last_date is None or datetime.now() - cls.last_date > timedelta(delta): + time.sleep(30) + cls.repartition(refetch=True) + + @classmethod + def repartition(cls, liquidity="medium", refetch=False): + cls.parse_cryptoportfolio(refetch=refetch) liquidities = cls.liquidities[liquidity] - cls.last_date = sorted(liquidities.keys())[-1] return liquidities[cls.last_date] @classmethod @@ -34,8 +41,8 @@ class Portfolio: cls.data = None @classmethod - def parse_cryptoportfolio(cls): - if cls.data is None: + def parse_cryptoportfolio(cls, refetch=False): + if refetch or cls.data is None: cls.get_cryptoportfolio() def filter_weights(weight_hash): @@ -57,7 +64,8 @@ class Portfolio: weights_hash = portfolio_hash["weights"] weights = {} for i in range(len(weights_hash["_row"])): - weights[weights_hash["_row"][i]] = dict(filter( + date = datetime.strptime(weights_hash["_row"][i], "%Y-%m-%d") + weights[date] = dict(filter( filter_weights, map(clean_weights(i), weights_hash.items()))) return weights @@ -69,6 +77,7 @@ class Portfolio: "medium": medium_liquidity, "high": high_liquidity, } + cls.last_date = max(max(medium_liquidity.keys()), max(high_liquidity.keys())) class Computation: computations = { @@ -336,6 +345,9 @@ class Trade: ticker = ticker["original"] rate = Computation.compute_value(ticker, self.order_action(inverted), compute_value=compute_value) + #TODO: store when the order is considered filled + # FIXME: Dust amount should be removed from there if they werent + # honored in other sales delta_in_base = abs(self.value_from - self.value_to) # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case) @@ -413,6 +425,8 @@ class Trade: print(self) for order in self.orders: print("\t", order, sep="") + for mouvement in order.mouvements: + print("\t\t", mouvement, sep="") class Order: def __init__(self, action, amount, rate, base_currency, trade_type, market, @@ -573,6 +587,19 @@ class Mouvement: # rate * total = total_in_base self.total_in_base = Amount(base_currency, hash_.get("total", 0)) + def __repr__(self): + if self.fee_rate > 0: + fee_rate = " fee: {}%".format(self.fee_rate * 100) + else: + fee_rate = "" + if self.date is None: + date = "No date" + else: + date = self.date + return "Mouvement({} ; {} {} ({}){})".format( + date, self.action, self.total, self.total_in_base, + fee_rate) + if __name__ == '__main__': # pragma: no cover from market import market h.print_orders(market)