From bbe3bc4423610d27090b378f69738aaab85a0303 Mon Sep 17 00:00:00 2001 From: gcarq Date: Sat, 21 Apr 2018 22:37:27 +0200 Subject: [PATCH 01/10] catch ccxt.ExchangeError and retry --- freqtrade/__init__.py | 4 +- freqtrade/exchange/__init__.py | 113 +++++++++++----------- freqtrade/tests/exchange/test_exchange.py | 14 +-- 3 files changed, 67 insertions(+), 64 deletions(-) diff --git a/freqtrade/__init__.py b/freqtrade/__init__.py index 16f8b3b4a..c630d06b2 100644 --- a/freqtrade/__init__.py +++ b/freqtrade/__init__.py @@ -16,9 +16,9 @@ class OperationalException(BaseException): """ -class NetworkException(BaseException): +class TemporaryError(BaseException): """ - Network related error. + Temporary network or exchange related error. This could happen when an exchange is congested, unavailable, or the user has networking problems. Usually resolves itself after a time. """ diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index dffe0e1b8..e840387a9 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -7,7 +7,7 @@ from typing import List, Dict, Any, Optional import ccxt import arrow -from freqtrade import OperationalException, DependencyException, NetworkException +from freqtrade import OperationalException, DependencyException, TemporaryError logger = logging.getLogger(__name__) @@ -15,7 +15,7 @@ logger = logging.getLogger(__name__) # Current selected exchange _API: ccxt.Exchange = None -_CONF: dict = {} +_CONF: Dict = {} API_RETRY_COUNT = 4 # Holds all open sell orders for dry_run @@ -33,15 +33,15 @@ def retrier(f): count = kwargs.pop('count', API_RETRY_COUNT) try: return f(*args, **kwargs) - except (NetworkException, DependencyException) as ex: - logger.warning('%s returned exception: "%s"', f, ex) + except (TemporaryError, DependencyException) as ex: + logger.warning('%s() returned exception: "%s"', f.__name__, ex) if count > 0: count -= 1 kwargs.update({'count': count}) - logger.warning('retrying %s still for %s times', f, count) + logger.warning('retrying %s() still for %s times', f.__name__, count) return wrapper(*args, **kwargs) else: - raise OperationalException('Giving up retrying: %s', f) + raise OperationalException('Giving up retrying: %s()', f.__name__) return wrapper @@ -151,10 +151,10 @@ def buy(pair: str, rate: float, amount: float) -> Dict: 'Tried to buy amount {} at rate {} (total {}).' 'Message: {}'.format(pair, amount, rate, rate*amount, e) ) - except ccxt.NetworkError as e: - raise NetworkException( - 'Could not place buy order due to networking error. Message: {}'.format(e) - ) + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not place buy order due to {}. Message: {}'.format( + e.__class__.__name__, e)) except ccxt.BaseError as e: raise OperationalException(e) @@ -189,10 +189,10 @@ def sell(pair: str, rate: float, amount: float) -> Dict: 'Tried to sell amount {} at rate {} (total {}).' 'Message: {}'.format(pair, amount, rate, rate*amount, e) ) - except ccxt.NetworkError as e: - raise NetworkException( - 'Could not place sell order due to networking error. Message: {}'.format(e) - ) + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not place sell order due to {}. Message: {}'.format( + e.__class__.__name__, e)) except ccxt.BaseError as e: raise OperationalException(e) @@ -219,10 +219,10 @@ def get_balances() -> dict: balances.pop("used", None) return balances - except ccxt.NetworkError as e: - raise NetworkException( - 'Could not get balance due to networking error. Message: {}'.format(e) - ) + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not get balance due to {}. Message: {}'.format( + e.__class__.__name__, e)) except ccxt.BaseError as e: raise OperationalException(e) @@ -231,17 +231,17 @@ def get_balances() -> dict: def get_tickers() -> Dict: try: return _API.fetch_tickers() - except ccxt.NetworkError as e: - raise NetworkException( - 'Could not load tickers due to networking error. Message: {}'.format(e) - ) - except ccxt.BaseError as e: - raise OperationalException(e) except ccxt.NotSupported as e: raise OperationalException( 'Exchange {} does not support fetching tickers in batch.' 'Message: {}'.format(_API.name, e) ) + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not load tickers due to {}. Message: {}'.format( + e.__class__.__name__, e)) + except ccxt.BaseError as e: + raise OperationalException(e) # TODO: remove refresh argument, keeping it to keep track of where it was intended to be used @@ -249,10 +249,10 @@ def get_tickers() -> Dict: def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict: try: return _API.fetch_ticker(pair) - except ccxt.NetworkError as e: - raise NetworkException( - 'Could not load tickers due to networking error. Message: {}'.format(e) - ) + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not load ticker history due to {}. Message: {}'.format( + e.__class__.__name__, e)) except ccxt.BaseError as e: raise OperationalException(e) @@ -261,17 +261,17 @@ def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict: def get_ticker_history(pair: str, tick_interval: str) -> List[Dict]: try: return _API.fetch_ohlcv(pair, timeframe=tick_interval) - except ccxt.NetworkError as e: - raise NetworkException( - 'Could not load ticker history due to networking error. Message: {}'.format(e) - ) - except ccxt.BaseError as e: - raise OperationalException('Could not fetch ticker data. Msg: {}'.format(e)) except ccxt.NotSupported as e: raise OperationalException( 'Exchange {} does not support fetching historical candlestick data.' 'Message: {}'.format(_API.name, e) ) + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not load ticker history due to {}. Message: {}'.format( + e.__class__.__name__, e)) + except ccxt.BaseError as e: + raise OperationalException('Could not fetch ticker data. Msg: {}'.format(e)) def cancel_order(order_id: str, pair: str) -> None: @@ -280,14 +280,10 @@ def cancel_order(order_id: str, pair: str) -> None: try: return _API.cancel_order(order_id, pair) - except ccxt.NetworkError as e: - raise NetworkException( - 'Could not get order due to networking error. Message: {}'.format(e) - ) - except ccxt.InvalidOrder as e: - raise DependencyException( - 'Could not cancel order. Message: {}'.format(e) - ) + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not cancel order due to {}. Message: {}'.format( + e.__class__.__name__, e)) except ccxt.BaseError as e: raise OperationalException(e) @@ -301,14 +297,14 @@ def get_order(order_id: str, pair: str) -> Dict: return order try: return _API.fetch_order(order_id, pair) - except ccxt.NetworkError as e: - raise NetworkException( - 'Could not get order due to networking error. Message: {}'.format(e) - ) except ccxt.InvalidOrder as e: raise DependencyException( 'Could not get order. Message: {}'.format(e) ) + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not get order due to {}. Message: {}'.format( + e.__class__.__name__, e)) except ccxt.BaseError as e: raise OperationalException(e) @@ -327,10 +323,10 @@ def get_pair_detail_url(pair: str) -> str: def get_markets() -> List[dict]: try: return _API.fetch_markets() - except ccxt.NetworkError as e: - raise NetworkException( - 'Could not load markets due to networking error. Message: {}'.format(e) - ) + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not load markets due to {}. Message: {}'.format( + e.__class__.__name__, e)) except ccxt.BaseError as e: raise OperationalException(e) @@ -345,9 +341,16 @@ def get_id() -> str: def get_fee(symbol='ETH/BTC', type='', side='', amount=1, price=1, taker_or_maker='maker') -> float: - # validate that markets are loaded before trying to get fee - if _API.markets is None or len(_API.markets) == 0: - _API.load_markets() + try: + # validate that markets are loaded before trying to get fee + if _API.markets is None or len(_API.markets) == 0: + _API.load_markets() - return _API.calculate_fee(symbol=symbol, type=type, side=side, amount=amount, - price=price, takerOrMaker=taker_or_maker)['rate'] + return _API.calculate_fee(symbol=symbol, type=type, side=side, amount=amount, + price=price, takerOrMaker=taker_or_maker)['rate'] + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + 'Could not get fee info due to {}. Message: {}'.format( + e.__class__.__name__, e)) + except ccxt.BaseError as e: + raise OperationalException(e) diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index c2a553d3b..0f74b161a 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -8,7 +8,7 @@ import ccxt import pytest -from freqtrade import OperationalException, DependencyException, NetworkException +from freqtrade import OperationalException, DependencyException, TemporaryError from freqtrade.exchange import init, validate_pairs, buy, sell, get_balance, get_balances, \ get_ticker, get_ticker_history, cancel_order, get_name, get_fee, get_id, get_pair_detail_url import freqtrade.exchange as exchange @@ -148,7 +148,7 @@ def test_buy_prod(default_conf, mocker): mocker.patch('freqtrade.exchange._API', api_mock) buy(pair='ETH/BTC', rate=200, amount=1) - with pytest.raises(NetworkException): + with pytest.raises(TemporaryError): api_mock.create_limit_buy_order = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) buy(pair='ETH/BTC', rate=200, amount=1) @@ -198,7 +198,7 @@ def test_sell_prod(default_conf, mocker): mocker.patch('freqtrade.exchange._API', api_mock) sell(pair='ETH/BTC', rate=200, amount=1) - with pytest.raises(NetworkException): + with pytest.raises(TemporaryError): api_mock.create_limit_sell_order = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) sell(pair='ETH/BTC', rate=200, amount=1) @@ -262,7 +262,7 @@ def test_get_balances_prod(default_conf, mocker): assert get_balances()['1ST']['total'] == 10.0 assert get_balances()['1ST']['used'] == 0.0 - with pytest.raises(NetworkException): + with pytest.raises(TemporaryError): api_mock.fetch_balance = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) get_balances() @@ -397,12 +397,12 @@ def test_cancel_order(default_conf, mocker): mocker.patch('freqtrade.exchange._API', api_mock) assert cancel_order(order_id='_', pair='TKN/BTC') == 123 - with pytest.raises(NetworkException): + with pytest.raises(TemporaryError): api_mock.cancel_order = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) cancel_order(order_id='_', pair='TKN/BTC') - with pytest.raises(DependencyException): + with pytest.raises(TemporaryError): api_mock.cancel_order = MagicMock(side_effect=ccxt.InvalidOrder) mocker.patch('freqtrade.exchange._API', api_mock) cancel_order(order_id='_', pair='TKN/BTC') @@ -429,7 +429,7 @@ def test_get_order(default_conf, mocker): mocker.patch('freqtrade.exchange._API', api_mock) assert exchange.get_order('X', 'TKN/BTC') == 456 - with pytest.raises(NetworkException): + with pytest.raises(TemporaryError): api_mock.fetch_order = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) exchange.get_order(order_id='_', pair='TKN/BTC') From 9ab495347279c21ab8ba9c97919e03faed9dd2b4 Mon Sep 17 00:00:00 2001 From: gcarq Date: Sun, 22 Apr 2018 00:19:11 +0200 Subject: [PATCH 02/10] fix backtesting testsuite --- freqtrade/tests/optimize/test_backtesting.py | 133 +++++++++---------- 1 file changed, 66 insertions(+), 67 deletions(-) diff --git a/freqtrade/tests/optimize/test_backtesting.py b/freqtrade/tests/optimize/test_backtesting.py index f5709154b..7b6783d92 100644 --- a/freqtrade/tests/optimize/test_backtesting.py +++ b/freqtrade/tests/optimize/test_backtesting.py @@ -6,7 +6,6 @@ import random from copy import deepcopy from typing import List from unittest.mock import MagicMock -import pytest import numpy as np import pandas as pd @@ -18,19 +17,6 @@ from freqtrade.arguments import Arguments from freqtrade.optimize.backtesting import Backtesting, start, setup_configuration from freqtrade.tests.conftest import log_has -# Avoid to reinit the same object again and again -_BACKTESTING = None -_BACKTESTING_INITIALIZED = False - - -@pytest.fixture(scope='function') -def init_backtesting(default_conf, mocker): - global _BACKTESTING_INITIALIZED, _BACKTESTING - if not _BACKTESTING_INITIALIZED: - mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) - _BACKTESTING = Backtesting(default_conf) - _BACKTESTING_INITIALIZED = True - def get_args(args) -> List[str]: return Arguments(args, '').get_parsed_arg() @@ -96,8 +82,9 @@ def load_data_test(what): return data -def simple_backtest(config, contour, num_results) -> None: - backtesting = _BACKTESTING +def simple_backtest(config, contour, num_results, mocker) -> None: + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtesting = Backtesting(config) data = load_data_test(contour) processed = backtesting.tickerdata_to_dataframe(data) @@ -128,12 +115,14 @@ def _load_pair_as_ticks(pair, tickfreq): # FIX: fixturize this? -def _make_backtest_conf(conf=None, pair='UNITTEST/BTC', record=None): +def _make_backtest_conf(mocker, conf=None, pair='UNITTEST/BTC', record=None): data = optimize.load_data(None, ticker_interval='8m', pairs=[pair]) data = trim_dictlist(data, -200) + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtesting = Backtesting(conf) return { 'stake_amount': conf['stake_amount'], - 'processed': _BACKTESTING.tickerdata_to_dataframe(data), + 'processed': backtesting.tickerdata_to_dataframe(data), 'max_open_trades': 10, 'realistic': True, 'record': record @@ -169,21 +158,6 @@ def _trend_alternate(dataframe=None): return dataframe -def _run_backtest_1(fun, backtest_conf): - # strategy is a global (hidden as a singleton), so we - # emulate strategy being pure, by override/restore here - # if we dont do this, the override in strategy will carry over - # to other tests - old_buy = _BACKTESTING.populate_buy_trend - old_sell = _BACKTESTING.populate_sell_trend - _BACKTESTING.populate_buy_trend = fun # Override - _BACKTESTING.populate_sell_trend = fun # Override - results = _BACKTESTING.backtest(backtest_conf) - _BACKTESTING.populate_buy_trend = old_buy # restore override - _BACKTESTING.populate_sell_trend = old_sell # restore override - return results - - # Unit tests def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None: """ @@ -287,12 +261,13 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non ) -def test_start(mocker, init_backtesting, fee, default_conf, caplog) -> None: +def test_start(mocker, fee, default_conf, caplog) -> None: """ Test start() function """ start_mock = MagicMock() mocker.patch('freqtrade.exchange.get_fee', fee) + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) mocker.patch('freqtrade.optimize.backtesting.Backtesting.start', start_mock) mocker.patch('freqtrade.configuration.open', mocker.mock_open( read_data=json.dumps(default_conf) @@ -342,16 +317,16 @@ def test_backtesting_init(mocker, default_conf) -> None: assert callable(backtesting.populate_sell_trend) -def test_tickerdata_to_dataframe(init_backtesting, default_conf) -> None: +def test_tickerdata_to_dataframe(default_conf, mocker) -> None: """ Test Backtesting.tickerdata_to_dataframe() method """ - + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) timerange = ((None, 'line'), None, -100) tick = optimize.load_tickerdata_file(None, 'UNITTEST/BTC', '1m', timerange=timerange) tickerlist = {'UNITTEST/BTC': tick} - backtesting = _BACKTESTING + backtesting = Backtesting(default_conf) data = backtesting.tickerdata_to_dataframe(tickerlist) assert len(data['UNITTEST/BTC']) == 100 @@ -361,11 +336,12 @@ def test_tickerdata_to_dataframe(init_backtesting, default_conf) -> None: assert data['UNITTEST/BTC'].equals(data2['UNITTEST/BTC']) -def test_get_timeframe(init_backtesting) -> None: +def test_get_timeframe(default_conf, mocker) -> None: """ Test Backtesting.get_timeframe() method """ - backtesting = _BACKTESTING + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtesting = Backtesting(default_conf) data = backtesting.tickerdata_to_dataframe( optimize.load_data( @@ -379,11 +355,12 @@ def test_get_timeframe(init_backtesting) -> None: assert max_date.isoformat() == '2017-11-14T22:59:00+00:00' -def test_generate_text_table(init_backtesting): +def test_generate_text_table(default_conf, mocker): """ Test Backtesting.generate_text_table() method """ - backtesting = _BACKTESTING + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtesting = Backtesting(default_conf) results = pd.DataFrame( { @@ -451,13 +428,13 @@ def test_backtesting_start(default_conf, mocker, caplog) -> None: assert log_has(line, caplog.record_tuples) -def test_backtest(init_backtesting, default_conf, fee, mocker) -> None: +def test_backtest(default_conf, fee, mocker) -> None: """ Test Backtesting.backtest() method """ mocker.patch('freqtrade.exchange.get_fee', fee) - - backtesting = _BACKTESTING + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtesting = Backtesting(default_conf) data = optimize.load_data(None, ticker_interval='5m', pairs=['UNITTEST/BTC']) data = trim_dictlist(data, -200) @@ -472,13 +449,13 @@ def test_backtest(init_backtesting, default_conf, fee, mocker) -> None: assert not results.empty -def test_backtest_1min_ticker_interval(init_backtesting, default_conf, fee, mocker) -> None: +def test_backtest_1min_ticker_interval(default_conf, fee, mocker) -> None: """ Test Backtesting.backtest() method with 1 min ticker """ mocker.patch('freqtrade.exchange.get_fee', fee) - - backtesting = _BACKTESTING + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtesting = Backtesting(default_conf) # Run a backtesting for an exiting 5min ticker_interval data = optimize.load_data(None, ticker_interval='1m', pairs=['UNITTEST/BTC']) @@ -494,11 +471,12 @@ def test_backtest_1min_ticker_interval(init_backtesting, default_conf, fee, mock assert not results.empty -def test_processed(init_backtesting) -> None: +def test_processed(default_conf, mocker) -> None: """ Test Backtesting.backtest() method with offline data """ - backtesting = _BACKTESTING + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtesting = Backtesting(default_conf) dict_of_tickerrows = load_data_test('raise') dataframes = backtesting.tickerdata_to_dataframe(dict_of_tickerrows) @@ -510,69 +488,90 @@ def test_processed(init_backtesting) -> None: assert col in cols -def test_backtest_pricecontours(init_backtesting, default_conf, fee, mocker) -> None: +def test_backtest_pricecontours(default_conf, fee, mocker) -> None: mocker.patch('freqtrade.optimize.backtesting.exchange.get_fee', fee) tests = [['raise', 17], ['lower', 0], ['sine', 17]] for [contour, numres] in tests: - simple_backtest(default_conf, contour, numres) + simple_backtest(default_conf, contour, numres, mocker) # Test backtest using offline data (testdata directory) -def test_backtest_ticks(init_backtesting, default_conf, fee, mocker): +def test_backtest_ticks(default_conf, fee, mocker): mocker.patch('freqtrade.exchange.get_fee', fee) + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) ticks = [1, 5] - fun = _BACKTESTING.populate_buy_trend + fun = Backtesting(default_conf).populate_buy_trend for _ in ticks: - backtest_conf = _make_backtest_conf(conf=default_conf) - results = _run_backtest_1(fun, backtest_conf) + backtest_conf = _make_backtest_conf(mocker, conf=default_conf) + backtesting = Backtesting(default_conf) + backtesting.populate_buy_trend = fun # Override + backtesting.populate_sell_trend = fun # Override + results = backtesting.backtest(backtest_conf) assert not results.empty -def test_backtest_clash_buy_sell(init_backtesting, default_conf): +def test_backtest_clash_buy_sell(mocker, default_conf): # Override the default buy trend function in our default_strategy def fun(dataframe=None): buy_value = 1 sell_value = 1 return _trend(dataframe, buy_value, sell_value) - backtest_conf = _make_backtest_conf(conf=default_conf) - results = _run_backtest_1(fun, backtest_conf) + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtest_conf = _make_backtest_conf(mocker, conf=default_conf) + backtesting = Backtesting(default_conf) + backtesting.populate_buy_trend = fun # Override + backtesting.populate_sell_trend = fun # Override + results = backtesting.backtest(backtest_conf) assert results.empty -def test_backtest_only_sell(init_backtesting, default_conf): +def test_backtest_only_sell(mocker, default_conf): # Override the default buy trend function in our default_strategy def fun(dataframe=None): buy_value = 0 sell_value = 1 return _trend(dataframe, buy_value, sell_value) - backtest_conf = _make_backtest_conf(conf=default_conf) - results = _run_backtest_1(fun, backtest_conf) + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtest_conf = _make_backtest_conf(mocker, conf=default_conf) + backtesting = Backtesting(default_conf) + backtesting.populate_buy_trend = fun # Override + backtesting.populate_sell_trend = fun # Override + results = backtesting.backtest(backtest_conf) assert results.empty -def test_backtest_alternate_buy_sell(init_backtesting, default_conf, fee, mocker): +def test_backtest_alternate_buy_sell(default_conf, fee, mocker): mocker.patch('freqtrade.optimize.backtesting.exchange.get_fee', fee) - backtest_conf = _make_backtest_conf(conf=default_conf, pair='UNITTEST/BTC') - results = _run_backtest_1(_trend_alternate, backtest_conf) + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) + backtest_conf = _make_backtest_conf(mocker, conf=default_conf, pair='UNITTEST/BTC') + backtesting = Backtesting(default_conf) + backtesting.populate_buy_trend = _trend_alternate # Override + backtesting.populate_sell_trend = _trend_alternate # Override + results = backtesting.backtest(backtest_conf) assert len(results) == 3 -def test_backtest_record(init_backtesting, default_conf, fee, mocker): +def test_backtest_record(default_conf, fee, mocker): names = [] records = [] + mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) mocker.patch('freqtrade.optimize.backtesting.exchange.get_fee', fee) mocker.patch( 'freqtrade.optimize.backtesting.file_dump_json', new=lambda n, r: (names.append(n), records.append(r)) ) backtest_conf = _make_backtest_conf( + mocker, conf=default_conf, pair='UNITTEST/BTC', record="trades" ) - results = _run_backtest_1(_trend_alternate, backtest_conf) + backtesting = Backtesting(default_conf) + backtesting.populate_buy_trend = _trend_alternate # Override + backtesting.populate_sell_trend = _trend_alternate # Override + results = backtesting.backtest(backtest_conf) assert len(results) == 3 # Assert file_dump_json was only called once assert names == ['backtest-result.json'] @@ -595,7 +594,7 @@ def test_backtest_record(init_backtesting, default_conf, fee, mocker): assert dur > 0 -def test_backtest_start_live(init_backtesting, default_conf, mocker, caplog): +def test_backtest_start_live(default_conf, mocker, caplog): conf = deepcopy(default_conf) conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC'] mocker.patch('freqtrade.exchange.get_ticker_history', From c43ceb20457aa89a59d8514eaa71ea48f5f0ccd1 Mon Sep 17 00:00:00 2001 From: gcarq Date: Sun, 22 Apr 2018 00:35:04 +0200 Subject: [PATCH 03/10] add config*.json to .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c1bb0be68..e5ac932f8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Freqtrade rules freqtrade/tests/testdata/*.json hyperopt_conf.py -config.json +config*.json *.sqlite .hyperopt logfile.txt From bc2bd7fe1e19be0418afdbdf57dd3f5d19d969da Mon Sep 17 00:00:00 2001 From: gcarq Date: Sun, 22 Apr 2018 17:28:49 +0200 Subject: [PATCH 04/10] add retrier decorator to all exchange functions except buy/sell --- freqtrade/exchange/__init__.py | 6 ++++++ freqtrade/tests/exchange/test_exchange.py | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index e840387a9..eb2d324c5 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -206,6 +206,7 @@ def get_balance(currency: str) -> float: return balances[currency]['free'] +@retrier def get_balances() -> dict: if _CONF['dry_run']: return {} @@ -274,6 +275,7 @@ def get_ticker_history(pair: str, tick_interval: str) -> List[Dict]: raise OperationalException('Could not fetch ticker data. Msg: {}'.format(e)) +@retrier def cancel_order(order_id: str, pair: str) -> None: if _CONF['dry_run']: return @@ -288,6 +290,7 @@ def cancel_order(order_id: str, pair: str) -> None: raise OperationalException(e) +@retrier def get_order(order_id: str, pair: str) -> Dict: if _CONF['dry_run']: order = _DRY_RUN_OPEN_ORDERS[order_id] @@ -309,6 +312,7 @@ def get_order(order_id: str, pair: str) -> Dict: raise OperationalException(e) +@retrier def get_pair_detail_url(pair: str) -> str: try: url_base = _API.urls.get('www') @@ -320,6 +324,7 @@ def get_pair_detail_url(pair: str) -> str: return "" +@retrier def get_markets() -> List[dict]: try: return _API.fetch_markets() @@ -339,6 +344,7 @@ def get_id() -> str: return _API.id +@retrier def get_fee(symbol='ETH/BTC', type='', side='', amount=1, price=1, taker_or_maker='maker') -> float: try: diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index 0f74b161a..8efbcc201 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -262,15 +262,17 @@ def test_get_balances_prod(default_conf, mocker): assert get_balances()['1ST']['total'] == 10.0 assert get_balances()['1ST']['used'] == 0.0 - with pytest.raises(TemporaryError): + with pytest.raises(OperationalException): api_mock.fetch_balance = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) get_balances() + assert api_mock.fetch_balance.call_count == exchange.API_RETRY_COUNT + 1 with pytest.raises(OperationalException): api_mock.fetch_balance = MagicMock(side_effect=ccxt.BaseError) mocker.patch('freqtrade.exchange._API', api_mock) get_balances() + assert api_mock.fetch_balance.call_count == 1 # This test is somewhat redundant with @@ -397,20 +399,23 @@ def test_cancel_order(default_conf, mocker): mocker.patch('freqtrade.exchange._API', api_mock) assert cancel_order(order_id='_', pair='TKN/BTC') == 123 - with pytest.raises(TemporaryError): + with pytest.raises(OperationalException): api_mock.cancel_order = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) cancel_order(order_id='_', pair='TKN/BTC') + assert api_mock.cancel_order.call_count == exchange.API_RETRY_COUNT + 1 - with pytest.raises(TemporaryError): + with pytest.raises(OperationalException): api_mock.cancel_order = MagicMock(side_effect=ccxt.InvalidOrder) mocker.patch('freqtrade.exchange._API', api_mock) cancel_order(order_id='_', pair='TKN/BTC') + assert api_mock.cancel_order.call_count == exchange.API_RETRY_COUNT + 1 with pytest.raises(OperationalException): api_mock.cancel_order = MagicMock(side_effect=ccxt.BaseError) mocker.patch('freqtrade.exchange._API', api_mock) cancel_order(order_id='_', pair='TKN/BTC') + assert api_mock.cancel_order.call_count == 1 def test_get_order(default_conf, mocker): @@ -429,20 +434,23 @@ def test_get_order(default_conf, mocker): mocker.patch('freqtrade.exchange._API', api_mock) assert exchange.get_order('X', 'TKN/BTC') == 456 - with pytest.raises(TemporaryError): + with pytest.raises(OperationalException): api_mock.fetch_order = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) exchange.get_order(order_id='_', pair='TKN/BTC') + assert api_mock.fetch_order.call_count == exchange.API_RETRY_COUNT + 1 - with pytest.raises(DependencyException): + with pytest.raises(OperationalException): api_mock.fetch_order = MagicMock(side_effect=ccxt.InvalidOrder) mocker.patch('freqtrade.exchange._API', api_mock) exchange.get_order(order_id='_', pair='TKN/BTC') + assert api_mock.fetch_order.call_count == exchange.API_RETRY_COUNT + 1 with pytest.raises(OperationalException): api_mock.fetch_order = MagicMock(side_effect=ccxt.BaseError) mocker.patch('freqtrade.exchange._API', api_mock) exchange.get_order(order_id='_', pair='TKN/BTC') + assert api_mock.fetch_order.call_count == 1 def test_get_name(default_conf, mocker): From 4c49229b7722e39c66123b71be23865fba95f95d Mon Sep 17 00:00:00 2001 From: gcarq Date: Sun, 22 Apr 2018 20:27:34 +0200 Subject: [PATCH 05/10] catch DependencyExceptions while selling --- freqtrade/freqtradebot.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 947d20df3..672090129 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -358,15 +358,18 @@ class FreqtradeBot(object): Tries to execute a sell trade :return: True if executed """ - # Get order details for actual price per unit - if trade.open_order_id: - # Update trade with order values - logger.info('Found open order for %s', trade) - trade.update(exchange.get_order(trade.open_order_id, trade.pair)) + try: + # Get order details for actual price per unit + if trade.open_order_id: + # Update trade with order values + logger.info('Found open order for %s', trade) + trade.update(exchange.get_order(trade.open_order_id, trade.pair)) - if trade.is_open and trade.open_order_id is None: - # Check if we can sell our current pair - return self.handle_trade(trade) + if trade.is_open and trade.open_order_id is None: + # Check if we can sell our current pair + return self.handle_trade(trade) + except DependencyException as exception: + logger.warning('Unable to sell trade: %s', exception) return False def handle_trade(self, trade: Trade) -> bool: From 5baab91bb569072de2e738edba19a49eb2bc8ab7 Mon Sep 17 00:00:00 2001 From: gcarq Date: Sun, 22 Apr 2018 20:28:39 +0200 Subject: [PATCH 06/10] catch TemporaryError for buy/sell in _process() --- freqtrade/freqtradebot.py | 6 +++--- freqtrade/tests/test_freqtradebot.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 672090129..e780860e3 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -3,7 +3,6 @@ Freqtrade is the main module of this bot. It contains the class Freqtrade() """ import copy -import json import logging import time import traceback @@ -15,7 +14,8 @@ import requests from cachetools import cached, TTLCache from freqtrade import ( - DependencyException, OperationalException, exchange, persistence, __version__ + DependencyException, OperationalException, TemporaryError, + exchange, persistence, __version__, ) from freqtrade.analyze import Analyze from freqtrade.constants import Constants @@ -173,7 +173,7 @@ class FreqtradeBot(object): self.check_handle_timedout(self.config['unfilledtimeout']) Trade.session.flush() - except (requests.exceptions.RequestException, json.JSONDecodeError) as error: + except TemporaryError as error: logger.warning('%s, retrying in 30 seconds...', error) time.sleep(Constants.RETRY_TIMEOUT) except OperationalException: diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index d307c68aa..217bf82e6 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -16,7 +16,7 @@ import pytest import requests from sqlalchemy import create_engine -from freqtrade import DependencyException, OperationalException +from freqtrade import DependencyException, OperationalException, TemporaryError from freqtrade.freqtradebot import FreqtradeBot from freqtrade.persistence import Trade from freqtrade.state import State @@ -451,7 +451,7 @@ def test_process_exchange_failures(default_conf, ticker, markets, mocker) -> Non validate_pairs=MagicMock(), get_ticker=ticker, get_markets=markets, - buy=MagicMock(side_effect=requests.exceptions.RequestException) + buy=MagicMock(side_effect=TemporaryError) ) sleep_mock = mocker.patch('time.sleep', side_effect=lambda _: None) From 20af4bae7cbd41e6b6cdd1d9f3aab946c4baa559 Mon Sep 17 00:00:00 2001 From: gcarq Date: Mon, 23 Apr 2018 16:56:35 +0200 Subject: [PATCH 07/10] retrier: raise initial exception instead of OperationalException --- freqtrade/exchange/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index eb2d324c5..c10dc8576 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -41,7 +41,8 @@ def retrier(f): logger.warning('retrying %s() still for %s times', f.__name__, count) return wrapper(*args, **kwargs) else: - raise OperationalException('Giving up retrying: %s()', f.__name__) + logger.warning('Giving up retrying: %s()', f.__name__) + raise ex return wrapper From baeeaa777d489e2b1687aede0dfc3e4c63d0bad2 Mon Sep 17 00:00:00 2001 From: gcarq Date: Mon, 23 Apr 2018 16:57:18 +0200 Subject: [PATCH 08/10] get_balance: handle case if currency is not in response --- freqtrade/exchange/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index c10dc8576..8f9b737dd 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -198,13 +198,19 @@ def sell(pair: str, rate: float, amount: float) -> Dict: raise OperationalException(e) +@retrier def get_balance(currency: str) -> float: if _CONF['dry_run']: return 999.9 # ccxt exception is already handled by get_balances balances = get_balances() - return balances[currency]['free'] + balance = balances.get(currency) + if balance is None: + raise TemporaryError( + 'Could not get {} balance due to malformed exchange response: {}'.format( + currency, balances)) + return balance['free'] @retrier From aa213a364032df34131aeb3f4f6bb78fd1044fdd Mon Sep 17 00:00:00 2001 From: gcarq Date: Mon, 23 Apr 2018 16:58:32 +0200 Subject: [PATCH 09/10] cancel_order: handle InvalidOrder exception --- freqtrade/exchange/__init__.py | 4 ++++ freqtrade/tests/exchange/test_exchange.py | 14 +++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 8f9b737dd..f2c08ff7d 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -289,6 +289,10 @@ def cancel_order(order_id: str, pair: str) -> None: try: return _API.cancel_order(order_id, pair) + except ccxt.InvalidOrder as e: + raise DependencyException( + 'Could not cancel order. Message: {}'.format(e) + ) except (ccxt.NetworkError, ccxt.ExchangeError) as e: raise TemporaryError( 'Could not cancel order due to {}. Message: {}'.format( diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index 8efbcc201..d626340a3 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -262,7 +262,7 @@ def test_get_balances_prod(default_conf, mocker): assert get_balances()['1ST']['total'] == 10.0 assert get_balances()['1ST']['used'] == 0.0 - with pytest.raises(OperationalException): + with pytest.raises(TemporaryError): api_mock.fetch_balance = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) get_balances() @@ -312,7 +312,7 @@ def test_get_ticker(default_conf, mocker): assert ticker['bid'] == 0.5 assert ticker['ask'] == 1 - with pytest.raises(OperationalException): # test retrier + with pytest.raises(TemporaryError): # test retrier api_mock.fetch_ticker = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) get_ticker(pair='ETH/BTC', refresh=True) @@ -370,7 +370,7 @@ def test_get_ticker_history(default_conf, mocker): assert ticks[0][4] == 9 assert ticks[0][5] == 10 - with pytest.raises(OperationalException): # test retrier + with pytest.raises(TemporaryError): # test retrier api_mock.fetch_ohlcv = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) # new symbol to get around cache @@ -399,13 +399,13 @@ def test_cancel_order(default_conf, mocker): mocker.patch('freqtrade.exchange._API', api_mock) assert cancel_order(order_id='_', pair='TKN/BTC') == 123 - with pytest.raises(OperationalException): + with pytest.raises(TemporaryError): api_mock.cancel_order = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) cancel_order(order_id='_', pair='TKN/BTC') assert api_mock.cancel_order.call_count == exchange.API_RETRY_COUNT + 1 - with pytest.raises(OperationalException): + with pytest.raises(DependencyException): api_mock.cancel_order = MagicMock(side_effect=ccxt.InvalidOrder) mocker.patch('freqtrade.exchange._API', api_mock) cancel_order(order_id='_', pair='TKN/BTC') @@ -434,13 +434,13 @@ def test_get_order(default_conf, mocker): mocker.patch('freqtrade.exchange._API', api_mock) assert exchange.get_order('X', 'TKN/BTC') == 456 - with pytest.raises(OperationalException): + with pytest.raises(TemporaryError): api_mock.fetch_order = MagicMock(side_effect=ccxt.NetworkError) mocker.patch('freqtrade.exchange._API', api_mock) exchange.get_order(order_id='_', pair='TKN/BTC') assert api_mock.fetch_order.call_count == exchange.API_RETRY_COUNT + 1 - with pytest.raises(OperationalException): + with pytest.raises(DependencyException): api_mock.fetch_order = MagicMock(side_effect=ccxt.InvalidOrder) mocker.patch('freqtrade.exchange._API', api_mock) exchange.get_order(order_id='_', pair='TKN/BTC') From 9b0fbbdc146f9cbe207fe5ce27347be91e464038 Mon Sep 17 00:00:00 2001 From: gcarq Date: Mon, 23 Apr 2018 16:58:52 +0200 Subject: [PATCH 10/10] cancel_order: pass all positional arguments --- freqtrade/freqtradebot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index e780860e3..298f03164 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -429,7 +429,7 @@ class FreqtradeBot(object): """Buy timeout - cancel order :return: True if order was fully cancelled """ - exchange.cancel_order(trade.open_order_id) + exchange.cancel_order(trade.open_order_id, trade.pair) if order['remaining'] == order['amount']: # if trade is not partially completed, just delete the trade Trade.session.delete(trade) @@ -459,7 +459,7 @@ class FreqtradeBot(object): """ if order['remaining'] == order['amount']: # if trade is not partially completed, just cancel the trade - exchange.cancel_order(trade.open_order_id) + exchange.cancel_order(trade.open_order_id, trade.pair) trade.close_rate = None trade.close_profit = None trade.close_date = None