]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - portfolio.py
Fix bid/ask to sell/buy in orders
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / portfolio.py
index 946a96abdafd8e23be5b799acbfbc2fc3a55d504..00beab0ddd573474cc2f87fa44fd77e870e26018 100644 (file)
@@ -205,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)
@@ -233,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))
 
@@ -339,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: