aboutsummaryrefslogtreecommitdiff
path: root/store.py
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-05-01 17:24:40 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-05-01 17:24:40 +0200
commit2b1ee8f4d54fa1672510141a71a5817120ac031c (patch)
tree762d3a59d666bf6e1d4d1ea5901c31ab06f7e88e /store.py
parent37b64a97021220df0bee5fd927184b92b10e8d76 (diff)
downloadTrader-2b1ee8f4d54fa1672510141a71a5817120ac031c.tar.gz
Trader-2b1ee8f4d54fa1672510141a71a5817120ac031c.tar.zst
Trader-2b1ee8f4d54fa1672510141a71a5817120ac031c.zip
Store tickers in balance log
Diffstat (limited to 'store.py')
-rw-r--r--store.py31
1 files changed, 23 insertions, 8 deletions
diff --git a/store.py b/store.py
index 072d3a2..cd0bf7b 100644
--- a/store.py
+++ b/store.py
@@ -98,7 +98,8 @@ class ReportStore:
98 "args": args, 98 "args": args,
99 }) 99 })
100 100
101 def log_balances(self, tag=None): 101 def log_balances(self, tag=None, tickers=None,
102 ticker_currency=None, compute_value=None, type=None):
102 self.print_log("[Balance]") 103 self.print_log("[Balance]")
103 for currency, balance in self.market.balances.all.items(): 104 for currency, balance in self.market.balances.all.items():
104 self.print_log("\t{}".format(balance)) 105 self.print_log("\t{}".format(balance))
@@ -109,11 +110,22 @@ class ReportStore:
109 "balances": self.market.balances.as_json() 110 "balances": self.market.balances.as_json()
110 } 111 }
111 112
113 if tickers is not None:
114 log["tickers"] = self._ticker_hash(tickers, ticker_currency,
115 compute_value, type)
116
112 self.add_log(log.copy()) 117 self.add_log(log.copy())
113 self.add_redis_status(log) 118 self.add_redis_status(log)
114 119
115 def log_tickers(self, amounts, other_currency, 120 def log_tickers(self, amounts, other_currency,
116 compute_value, type): 121 compute_value, type):
122 log = self._ticker_hash(amounts, other_currency, compute_value,
123 type)
124 log["type"] = "tickers"
125
126 self.add_log(log)
127
128 def _ticker_hash(self, amounts, other_currency, compute_value, type):
117 values = {} 129 values = {}
118 rates = {} 130 rates = {}
119 if callable(compute_value): 131 if callable(compute_value):
@@ -122,8 +134,7 @@ class ReportStore:
122 for currency, amount in amounts.items(): 134 for currency, amount in amounts.items():
123 values[currency] = amount.as_json()["value"] 135 values[currency] = amount.as_json()["value"]
124 rates[currency] = amount.rate 136 rates[currency] = amount.rate
125 log = { 137 return {
126 "type": "tickers",
127 "compute_value": compute_value, 138 "compute_value": compute_value,
128 "balance_type": type, 139 "balance_type": type,
129 "currency": other_currency, 140 "currency": other_currency,
@@ -132,9 +143,6 @@ class ReportStore:
132 "total": sum(amounts.values()).as_json()["value"] 143 "total": sum(amounts.values()).as_json()["value"]
133 } 144 }
134 145
135 self.add_log(log.copy())
136 self.add_redis_status(log)
137
138 def log_dispatch(self, amount, amounts, liquidity, repartition): 146 def log_dispatch(self, amount, amounts, liquidity, repartition):
139 self.add_log({ 147 self.add_log({
140 "type": "dispatch", 148 "type": "dispatch",
@@ -294,13 +302,20 @@ class BalanceStore:
294 compute_value, type) 302 compute_value, type)
295 return amounts 303 return amounts
296 304
297 def fetch_balances(self, tag=None): 305 def fetch_balances(self, tag=None, log_tickers=False,
306 ticker_currency="BTC", ticker_compute_value="average", ticker_type="total"):
298 all_balances = self.market.ccxt.fetch_all_balances() 307 all_balances = self.market.ccxt.fetch_all_balances()
299 for currency, balance in all_balances.items(): 308 for currency, balance in all_balances.items():
300 if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \ 309 if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \
301 currency in self.all: 310 currency in self.all:
302 self.all[currency] = portfolio.Balance(currency, balance) 311 self.all[currency] = portfolio.Balance(currency, balance)
303 self.market.report.log_balances(tag=tag) 312 if log_tickers:
313 tickers = self.in_currency(ticker_currency, compute_value=ticker_compute_value, type=ticker_type)
314 self.market.report.log_balances(tag=tag,
315 tickers=tickers, ticker_currency=ticker_currency,
316 compute_value=ticker_compute_value, type=ticker_type)
317 else:
318 self.market.report.log_balances(tag=tag)
304 319
305 def dispatch_assets(self, amount, liquidity="medium", repartition=None): 320 def dispatch_assets(self, amount, liquidity="medium", repartition=None):
306 if repartition is None: 321 if repartition is None: