diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-08 20:33:47 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-08 20:33:47 +0200 |
commit | 3a15ffc79ea84e5ec6200545bcbf11fc6c1c6564 (patch) | |
tree | 047b8069d8889e251bd359f27e72b0a6ab212660 | |
parent | af928d32483535a817b62a68f2dcb215f48ed29c (diff) | |
download | Trader-3a15ffc79ea84e5ec6200545bcbf11fc6c1c6564.tar.gz Trader-3a15ffc79ea84e5ec6200545bcbf11fc6c1c6564.tar.zst Trader-3a15ffc79ea84e5ec6200545bcbf11fc6c1c6564.zip |
Add USDT rate to balances
-rw-r--r-- | market.py | 10 | ||||
-rw-r--r-- | store.py | 4 | ||||
-rw-r--r-- | tests/test_market.py | 3 | ||||
-rw-r--r-- | tests/test_store.py | 32 |
4 files changed, 45 insertions, 4 deletions
@@ -257,7 +257,11 @@ class Processor: | |||
257 | { | 257 | { |
258 | "name": "print_balances", | 258 | "name": "print_balances", |
259 | "number": 1, | 259 | "number": 1, |
260 | "fetch_balances_begin": { "log_tickers": True, "add_portfolio": True }, | 260 | "fetch_balances_begin": { |
261 | "log_tickers": True, | ||
262 | "add_usdt": True, | ||
263 | "add_portfolio": True | ||
264 | }, | ||
261 | "print_tickers": { "base_currency": "BTC" }, | 265 | "print_tickers": { "base_currency": "BTC" }, |
262 | } | 266 | } |
263 | ], | 267 | ], |
@@ -288,6 +292,7 @@ class Processor: | |||
288 | "fetch_balances_begin": { | 292 | "fetch_balances_begin": { |
289 | "checkpoint": "end", | 293 | "checkpoint": "end", |
290 | "log_tickers": True, | 294 | "log_tickers": True, |
295 | "add_usdt": True, | ||
291 | "add_portfolio": True | 296 | "add_portfolio": True |
292 | }, | 297 | }, |
293 | }, | 298 | }, |
@@ -319,6 +324,7 @@ class Processor: | |||
319 | "fetch_balances_begin": {}, | 324 | "fetch_balances_begin": {}, |
320 | "fetch_balances_end": { | 325 | "fetch_balances_end": { |
321 | "checkpoint": "begin", | 326 | "checkpoint": "begin", |
327 | "add_usdt": True, | ||
322 | "log_tickers": True | 328 | "log_tickers": True |
323 | }, | 329 | }, |
324 | "prepare_trades": { "only": "acquire", "available_balance_only": True }, | 330 | "prepare_trades": { "only": "acquire", "available_balance_only": True }, |
@@ -338,6 +344,7 @@ class Processor: | |||
338 | "fetch_balances_begin": { | 344 | "fetch_balances_begin": { |
339 | "checkpoint": "end", | 345 | "checkpoint": "end", |
340 | "log_tickers": True, | 346 | "log_tickers": True, |
347 | "add_usdt": True, | ||
341 | "add_portfolio": True | 348 | "add_portfolio": True |
342 | }, | 349 | }, |
343 | "fetch_balances_end": {}, | 350 | "fetch_balances_end": {}, |
@@ -362,6 +369,7 @@ class Processor: | |||
362 | "fetch_balances_begin": {}, | 369 | "fetch_balances_begin": {}, |
363 | "fetch_balances_end": { | 370 | "fetch_balances_end": { |
364 | "checkpoint": "begin", | 371 | "checkpoint": "begin", |
372 | "add_usdt": True, | ||
365 | "log_tickers": True | 373 | "log_tickers": True |
366 | }, | 374 | }, |
367 | "prepare_trades": { "available_balance_only": True }, | 375 | "prepare_trades": { "available_balance_only": True }, |
@@ -305,7 +305,7 @@ class BalanceStore: | |||
305 | return amounts | 305 | return amounts |
306 | 306 | ||
307 | def fetch_balances(self, tag=None, add_portfolio=False, | 307 | def fetch_balances(self, tag=None, add_portfolio=False, |
308 | checkpoint=None, log_tickers=False, | 308 | checkpoint=None, log_tickers=False, add_usdt=False, |
309 | ticker_currency="BTC", ticker_compute_value="average", ticker_type="total"): | 309 | ticker_currency="BTC", ticker_compute_value="average", ticker_type="total"): |
310 | all_balances = self.market.ccxt.fetch_all_balances() | 310 | all_balances = self.market.ccxt.fetch_all_balances() |
311 | for currency, balance in all_balances.items(): | 311 | for currency, balance in all_balances.items(): |
@@ -315,6 +315,8 @@ class BalanceStore: | |||
315 | if add_portfolio: | 315 | if add_portfolio: |
316 | for currency in Portfolio.repartition(from_cache=True): | 316 | for currency in Portfolio.repartition(from_cache=True): |
317 | self.all.setdefault(currency, portfolio.Balance(currency, {})) | 317 | self.all.setdefault(currency, portfolio.Balance(currency, {})) |
318 | if add_usdt: | ||
319 | self.all.setdefault("USDT", portfolio.Balance("USDT", {})) | ||
318 | if log_tickers: | 320 | if log_tickers: |
319 | tickers = self.in_currency(ticker_currency, compute_value=ticker_compute_value, type=ticker_type) | 321 | tickers = self.in_currency(ticker_currency, compute_value=ticker_compute_value, type=ticker_type) |
320 | self.market.report.log_balances(tag=tag, checkpoint=checkpoint, | 322 | self.market.report.log_balances(tag=tag, checkpoint=checkpoint, |
diff --git a/tests/test_market.py b/tests/test_market.py index 6a9ea76..2c92655 100644 --- a/tests/test_market.py +++ b/tests/test_market.py | |||
@@ -1042,6 +1042,7 @@ class ProcessorTest(WebMockTestCase): | |||
1042 | self.m.balances.fetch_balances.assert_has_calls([ | 1042 | self.m.balances.fetch_balances.assert_has_calls([ |
1043 | mock.call(add_portfolio=True, checkpoint='end', | 1043 | mock.call(add_portfolio=True, checkpoint='end', |
1044 | log_tickers=True, | 1044 | log_tickers=True, |
1045 | add_usdt=True, | ||
1045 | tag='process_foo__0_print_balances_begin') | 1046 | tag='process_foo__0_print_balances_begin') |
1046 | ]) | 1047 | ]) |
1047 | 1048 | ||
@@ -1060,7 +1061,7 @@ class ProcessorTest(WebMockTestCase): | |||
1060 | 1061 | ||
1061 | processor.process_step("foo", step, {"foo":"bar"}) | 1062 | processor.process_step("foo", step, {"foo":"bar"}) |
1062 | self.m.balances.fetch_balances.assert_called_once_with( | 1063 | self.m.balances.fetch_balances.assert_called_once_with( |
1063 | add_portfolio=True, log_tickers=True, | 1064 | add_portfolio=True, add_usdt=True, log_tickers=True, |
1064 | tag='process_foo__1_print_balances_begin') | 1065 | tag='process_foo__1_print_balances_begin') |
1065 | 1066 | ||
1066 | def test_parse_args(self): | 1067 | def test_parse_args(self): |
diff --git a/tests/test_store.py b/tests/test_store.py index 58e76e0..d7620a0 100644 --- a/tests/test_store.py +++ b/tests/test_store.py | |||
@@ -392,7 +392,6 @@ class BalanceStoreTest(WebMockTestCase): | |||
392 | type='type') | 392 | type='type') |
393 | 393 | ||
394 | balance_store = market.BalanceStore(self.m) | 394 | balance_store = market.BalanceStore(self.m) |
395 | |||
396 | with self.subTest(add_portfolio=True),\ | 395 | with self.subTest(add_portfolio=True),\ |
397 | mock.patch.object(market.Portfolio, "repartition") as repartition: | 396 | mock.patch.object(market.Portfolio, "repartition") as repartition: |
398 | repartition.return_value = { | 397 | repartition.return_value = { |
@@ -402,6 +401,37 @@ class BalanceStoreTest(WebMockTestCase): | |||
402 | balance_store.fetch_balances(add_portfolio=True) | 401 | balance_store.fetch_balances(add_portfolio=True) |
403 | self.assertListEqual(["USDT", "XVG", "XMR", "DOGE"], list(balance_store.currencies())) | 402 | self.assertListEqual(["USDT", "XVG", "XMR", "DOGE"], list(balance_store.currencies())) |
404 | 403 | ||
404 | self.m.ccxt.fetch_all_balances.return_value = { | ||
405 | "ETC": { | ||
406 | "exchange_free": 0, | ||
407 | "exchange_used": 0, | ||
408 | "exchange_total": 0, | ||
409 | "margin_total": 0, | ||
410 | }, | ||
411 | "XVG": { | ||
412 | "exchange_free": 16, | ||
413 | "exchange_used": 0, | ||
414 | "exchange_total": 16, | ||
415 | "margin_total": 0, | ||
416 | }, | ||
417 | "XMR": { | ||
418 | "exchange_free": 0, | ||
419 | "exchange_used": 0, | ||
420 | "exchange_total": 0, | ||
421 | "margin_total": D("-1.0"), | ||
422 | "margin_free": 0, | ||
423 | }, | ||
424 | } | ||
425 | |||
426 | balance_store = market.BalanceStore(self.m) | ||
427 | with self.subTest(add_usdt=True),\ | ||
428 | mock.patch.object(market.Portfolio, "repartition") as repartition: | ||
429 | repartition.return_value = { | ||
430 | "DOGE": D("0.5"), | ||
431 | "ETH": D("0.5"), | ||
432 | } | ||
433 | balance_store.fetch_balances(add_usdt=True) | ||
434 | self.assertListEqual(["XVG", "XMR", "USDT"], list(balance_store.currencies())) | ||
405 | 435 | ||
406 | @mock.patch.object(market.Portfolio, "repartition") | 436 | @mock.patch.object(market.Portfolio, "repartition") |
407 | def test_dispatch_assets(self, repartition): | 437 | def test_dispatch_assets(self, repartition): |