aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-01-22 01:04:30 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-01-22 01:04:30 +0100
commit7ab23e291602b6ffd54622cffdc76ba0b4437a05 (patch)
tree2c866b6e3259aea6979426fca649e5dcf33e5083
parent643767f141505c5e62dbaa402ac84a7b0c5511f2 (diff)
downloadTrader-7ab23e291602b6ffd54622cffdc76ba0b4437a05.tar.gz
Trader-7ab23e291602b6ffd54622cffdc76ba0b4437a05.tar.zst
Trader-7ab23e291602b6ffd54622cffdc76ba0b4437a05.zip
Allow dispatch asset to have custom repartition
-rw-r--r--portfolio.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/portfolio.py b/portfolio.py
index 946a96a..20f83e4 100644
--- a/portfolio.py
+++ b/portfolio.py
@@ -205,11 +205,12 @@ class Balance:
205 return cls.known_balances 205 return cls.known_balances
206 206
207 @classmethod 207 @classmethod
208 def dispatch_assets(cls, amount): 208 def dispatch_assets(cls, amount, repartition=None):
209 repartition_pertenthousand = Portfolio.repartition_pertenthousand() 209 if repartition is None:
210 sum_pertenthousand = sum([v for k, v in repartition_pertenthousand.items()]) 210 repartition = Portfolio.repartition_pertenthousand()
211 sum_pertenthousand = sum([v for k, v in repartition.items()])
211 amounts = {} 212 amounts = {}
212 for currency, ptt in repartition_pertenthousand.items(): 213 for currency, ptt in repartition.items():
213 amounts[currency] = ptt * amount / sum_pertenthousand 214 amounts[currency] = ptt * amount / sum_pertenthousand
214 if currency not in cls.known_balances: 215 if currency not in cls.known_balances:
215 cls.known_balances[currency] = cls(currency, 0, 0, 0) 216 cls.known_balances[currency] = cls(currency, 0, 0, 0)
@@ -233,6 +234,14 @@ class Balance:
233 new_repartition = cls.dispatch_assets(total_base_value) 234 new_repartition = cls.dispatch_assets(total_base_value)
234 Trade.compute_trades(values_in_base, new_repartition, only=only, market=market) 235 Trade.compute_trades(values_in_base, new_repartition, only=only, market=market)
235 236
237 @classmethod
238 def prepare_trades_to_sell_all(cls, market, base_currency="BTC", compute_value="average"):
239 cls.fetch_balances(market)
240 values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value)
241 total_base_value = sum(values_in_base.values())
242 new_repartition = cls.dispatch_assets(total_base_value, repartition={ base_currency: 1 })
243 Trade.compute_trades(values_in_base, new_repartition, market=market)
244
236 def __repr__(self): 245 def __repr__(self):
237 return "Balance({} [{}/{}/{}])".format(self.currency, str(self.free), str(self.used), str(self.total)) 246 return "Balance({} [{}/{}/{}])".format(self.currency, str(self.free), str(self.used), str(self.total))
238 247