From d24bb10c3cad1f144b76022481f46b4524873f4b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Mon, 26 Feb 2018 12:07:07 +0100 Subject: [PATCH] Fix dust amount error --- main.py | 1 - market.py | 2 ++ portfolio.py | 4 ++-- test.py | 16 ++++++++++++++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 3cb7f4a..17cf58c 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,6 @@ pg_config, report_path = helper.main_parse_config(args.config) for market_config, user_id in helper.main_fetch_markets(pg_config, args.user): try: - market_config["apiKey"] = market_config.pop("key") user_market = market.Market.from_config(market_config, debug=args.debug) helper.main_process_market(user_market, args.action, before=args.before, after=args.after) except Exception as e: diff --git a/market.py b/market.py index 931de09..0cb3e67 100644 --- a/market.py +++ b/market.py @@ -20,6 +20,8 @@ class Market: @classmethod def from_config(cls, config, debug=False): + config["apiKey"] = config.pop("key") + ccxt_instance = ccxt.poloniexE(config) # For requests logging diff --git a/portfolio.py b/portfolio.py index 43a39c4..0797de0 100644 --- a/portfolio.py +++ b/portfolio.py @@ -3,7 +3,7 @@ from datetime import datetime, timedelta from decimal import Decimal as D, ROUND_DOWN from json import JSONDecodeError from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError -from ccxt import ExchangeError, ExchangeNotAvailable +from ccxt import ExchangeError, ExchangeNotAvailable, InvalidOrder import requests # FIXME: correctly handle web call timeouts @@ -532,7 +532,7 @@ class Order: else: try: self.results.append(self.market.ccxt.create_order(symbol, 'limit', self.action, amount, price=self.rate, account=self.account)) - except ExchangeNotAvailable: + except (ExchangeNotAvailable, InvalidOrder): # Impossible to honor the order (dust amount) self.status = "closed" self.mark_finished_order() diff --git a/test.py b/test.py index 4ed0477..52d737d 100644 --- a/test.py +++ b/test.py @@ -571,7 +571,7 @@ class MarketTest(WebMockTestCase): ccxt.poloniexE.return_value = self.ccxt self.ccxt.session.request.return_value = "response" - m = market.Market.from_config("config") + m = market.Market.from_config({"key": "key", "secred": "secret"}) self.assertEqual(self.ccxt, m.ccxt) @@ -580,7 +580,7 @@ class MarketTest(WebMockTestCase): m.report.log_http_request.assert_called_with('GET', 'URL', 'data', 'headers', 'response') - m = market.Market.from_config("config", debug=True) + m = market.Market.from_config({"key": "key", "secred": "secret"}, debug=True) self.assertEqual(True, m.debug) def test_get_ticker(self): @@ -2080,6 +2080,18 @@ class OrderTest(WebMockTestCase): self.assertEqual("closed", order.status) mark_finished_order.assert_called_once() + self.m.ccxt.create_order.reset_mock() + with self.subTest(dust_amount_exception=True),\ + mock.patch.object(portfolio.Order, "mark_finished_order") as mark_finished_order: + order = portfolio.Order("buy", portfolio.Amount("ETH", 0.001), + D("0.1"), "BTC", "long", self.m, "trade") + self.m.ccxt.create_order.side_effect = portfolio.InvalidOrder + order.run() + self.m.ccxt.create_order.assert_called_once() + self.assertEqual(0, len(order.results)) + self.assertEqual("closed", order.status) + mark_finished_order.assert_called_once() + @unittest.skipUnless("unit" in limits, "Unit skipped") class MouvementTest(WebMockTestCase): -- 2.41.0