X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=portfolio.py;h=1041df13a76ebad551646fa8e8209b822b89b26d;hb=272b3cfb11aea4160f0e40588e18dea44c6b028a;hp=acb61b24623dec2e07019a2e1ab102a77dec11f5;hpb=a9950fd073198f3c9dc938fd731d97c9821a3845;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/portfolio.py b/portfolio.py index acb61b2..1041df1 100644 --- a/portfolio.py +++ b/portfolio.py @@ -4,8 +4,6 @@ from decimal import Decimal as D # Put your poloniex api key in market.py from market import market -debug = False - class Portfolio: URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json" liquidities = {} @@ -207,11 +205,12 @@ class Balance: return cls.known_balances @classmethod - def dispatch_assets(cls, amount): - repartition_pertenthousand = Portfolio.repartition_pertenthousand() - sum_pertenthousand = sum([v for k, v in repartition_pertenthousand.items()]) + def dispatch_assets(cls, amount, repartition=None): + if repartition is None: + repartition = Portfolio.repartition_pertenthousand() + sum_pertenthousand = sum([v for k, v in repartition.items()]) amounts = {} - for currency, ptt in repartition_pertenthousand.items(): + for currency, ptt in repartition.items(): amounts[currency] = ptt * amount / sum_pertenthousand if currency not in cls.known_balances: cls.known_balances[currency] = cls(currency, 0, 0, 0) @@ -235,6 +234,14 @@ class Balance: new_repartition = cls.dispatch_assets(total_base_value) Trade.compute_trades(values_in_base, new_repartition, only=only, market=market) + @classmethod + def prepare_trades_to_sell_all(cls, market, base_currency="BTC", compute_value="average"): + cls.fetch_balances(market) + values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value) + total_base_value = sum(values_in_base.values()) + new_repartition = cls.dispatch_assets(total_base_value, repartition={ base_currency: 1 }) + Trade.compute_trades(values_in_base, new_repartition, market=market) + def __repr__(self): return "Balance({} [{}/{}/{}])".format(self.currency, str(self.free), str(self.used), str(self.total)) @@ -341,9 +348,9 @@ class Trade: def order_action(self, inverted): if self.value_from < self.value_to: - return "ask" if not inverted else "bid" + return "buy" if not inverted else "sell" else: - return "bid" if not inverted else "ask" + return "sell" if not inverted else "buy" def prepare_order(self, compute_value="default"): if self.action is None: @@ -400,6 +407,11 @@ class Trade: if verbose: print("All orders finished") + @classmethod + def update_all_orders_status(cls): + for order in cls.all_orders(state="open"): + order.get_status() + def __repr__(self): return "Trade({} -> {} in {}, {})".format( self.value_from, @@ -407,9 +419,17 @@ class Trade: self.currency, self.action) -class Order: - DEBUG = debug + @classmethod + def print_all_with_order(cls): + for trade in cls.trades.values(): + trade.print_with_order() + + def print_with_order(self): + print(self) + for order in self.orders: + print("\t", order, sep="") +class Order: def __init__(self, action, amount, rate, base_currency, market): self.action = action self.amount = amount @@ -436,11 +456,11 @@ class Order: def finished(self): return self.status == "closed" or self.status == "canceled" - def run(self): + def run(self, debug=False): symbol = "{}/{}".format(self.amount.currency, self.base_currency) amount = self.amount.value - if self.DEBUG: + if debug: print("market.create_order('{}', 'limit', '{}', {}, price={})".format( symbol, self.action, amount, self.rate)) else: @@ -457,15 +477,15 @@ class Order: self.status = result["status"] return self.status + def cancel(self): + self.market.cancel_order(self.result['id']) + def print_orders(market, base_currency="BTC"): Balance.prepare_trades(market, base_currency=base_currency, compute_value="average") Trade.prepare_orders(compute_value="average") for currency, balance in Balance.known_balances.items(): print(balance) - for currency, trade in Trade.trades.items(): - print(trade) - for order in trade.orders: - print("\t", order, sep="") + portfolio.Trade.print_all_with_order() def make_orders(market, base_currency="BTC"): Balance.prepare_trades(market, base_currency=base_currency)