2018-07-17 07:21:53 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, C0103
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
import arrow
|
|
|
|
from pandas import DataFrame
|
|
|
|
|
2019-07-11 18:23:23 +00:00
|
|
|
from freqtrade.configuration import TimeRange
|
2019-12-27 09:44:08 +00:00
|
|
|
from freqtrade.data.history import load_data
|
2018-08-17 04:50:36 +00:00
|
|
|
from freqtrade.persistence import Trade
|
2020-02-11 22:39:15 +00:00
|
|
|
from freqtrade.resolvers import StrategyResolver
|
2020-02-18 19:12:10 +00:00
|
|
|
from .strats.default_strategy import DefaultStrategy
|
2020-02-11 22:39:15 +00:00
|
|
|
from tests.conftest import get_patched_exchange, log_has
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
# Avoid to reinit the same object again and again
|
|
|
|
_STRATEGY = DefaultStrategy(config={})
|
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_returns_latest_buy_signal(mocker, default_conf, ohlcv_history):
|
2018-07-17 07:21:53 +00:00
|
|
|
mocker.patch.object(
|
2019-08-04 10:55:03 +00:00
|
|
|
_STRATEGY, '_analyze_ticker_internal',
|
2018-07-17 07:21:53 +00:00
|
|
|
return_value=DataFrame([{'buy': 1, 'sell': 0, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2020-03-08 10:35:31 +00:00
|
|
|
assert _STRATEGY.get_signal('ETH/BTC', '5m', ohlcv_history) == (True, False)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
mocker.patch.object(
|
2019-08-04 10:55:03 +00:00
|
|
|
_STRATEGY, '_analyze_ticker_internal',
|
2018-07-17 07:21:53 +00:00
|
|
|
return_value=DataFrame([{'buy': 0, 'sell': 1, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2020-03-08 10:35:31 +00:00
|
|
|
assert _STRATEGY.get_signal('ETH/BTC', '5m', ohlcv_history) == (False, True)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_returns_latest_sell_signal(mocker, default_conf, ohlcv_history):
|
2018-07-17 07:21:53 +00:00
|
|
|
mocker.patch.object(
|
2019-08-04 10:55:03 +00:00
|
|
|
_STRATEGY, '_analyze_ticker_internal',
|
2018-07-17 07:21:53 +00:00
|
|
|
return_value=DataFrame([{'sell': 1, 'buy': 0, 'date': arrow.utcnow()}])
|
|
|
|
)
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
assert _STRATEGY.get_signal('ETH/BTC', '5m', ohlcv_history) == (False, True)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
mocker.patch.object(
|
2019-08-04 10:55:03 +00:00
|
|
|
_STRATEGY, '_analyze_ticker_internal',
|
2018-07-17 07:21:53 +00:00
|
|
|
return_value=DataFrame([{'sell': 0, 'buy': 1, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2020-03-08 10:35:31 +00:00
|
|
|
assert _STRATEGY.get_signal('ETH/BTC', '5m', ohlcv_history) == (True, False)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_signal_empty(default_conf, mocker, caplog):
|
2018-07-26 18:06:25 +00:00
|
|
|
assert (False, False) == _STRATEGY.get_signal('foo', default_conf['ticker_interval'],
|
2018-12-11 18:48:36 +00:00
|
|
|
DataFrame())
|
2020-03-08 10:35:31 +00:00
|
|
|
assert log_has('Empty candle (OHLCV) data for pair foo', caplog)
|
2018-12-12 18:35:51 +00:00
|
|
|
caplog.clear()
|
|
|
|
|
|
|
|
assert (False, False) == _STRATEGY.get_signal('bar', default_conf['ticker_interval'],
|
|
|
|
[])
|
2020-03-08 10:35:31 +00:00
|
|
|
assert log_has('Empty candle (OHLCV) data for pair bar', caplog)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_get_signal_exception_valueerror(default_conf, mocker, caplog, ohlcv_history):
|
2018-07-17 07:21:53 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
mocker.patch.object(
|
2019-08-04 10:55:03 +00:00
|
|
|
_STRATEGY, '_analyze_ticker_internal',
|
2018-07-17 07:21:53 +00:00
|
|
|
side_effect=ValueError('xyz')
|
|
|
|
)
|
2018-12-11 18:48:36 +00:00
|
|
|
assert (False, False) == _STRATEGY.get_signal('foo', default_conf['ticker_interval'],
|
2020-03-08 10:35:31 +00:00
|
|
|
ohlcv_history)
|
|
|
|
assert log_has('Unable to analyze candle (OHLCV) data for pair foo: xyz', caplog)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_get_signal_empty_dataframe(default_conf, mocker, caplog, ohlcv_history):
|
2018-07-17 07:21:53 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
mocker.patch.object(
|
2019-08-04 10:55:03 +00:00
|
|
|
_STRATEGY, '_analyze_ticker_internal',
|
2018-07-17 07:21:53 +00:00
|
|
|
return_value=DataFrame([])
|
|
|
|
)
|
2018-12-11 18:48:36 +00:00
|
|
|
assert (False, False) == _STRATEGY.get_signal('xyz', default_conf['ticker_interval'],
|
2020-03-08 10:35:31 +00:00
|
|
|
ohlcv_history)
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Empty dataframe for pair xyz', caplog)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history):
|
2018-07-17 07:21:53 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
# default_conf defines a 5m interval. we check interval * 2 + 5m
|
|
|
|
# this is necessary as the last candle is removed (partial candles) by default
|
|
|
|
oldtime = arrow.utcnow().shift(minutes=-16)
|
|
|
|
ticks = DataFrame([{'buy': 1, 'date': oldtime}])
|
|
|
|
mocker.patch.object(
|
2019-08-04 10:55:03 +00:00
|
|
|
_STRATEGY, '_analyze_ticker_internal',
|
2018-07-17 07:21:53 +00:00
|
|
|
return_value=DataFrame(ticks)
|
|
|
|
)
|
2018-12-11 18:48:36 +00:00
|
|
|
assert (False, False) == _STRATEGY.get_signal('xyz', default_conf['ticker_interval'],
|
2020-03-08 10:35:31 +00:00
|
|
|
ohlcv_history)
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Outdated history for pair xyz. Last tick is 16 minutes old', caplog)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_signal_handles_exceptions(mocker, default_conf):
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
mocker.patch.object(
|
|
|
|
_STRATEGY, 'analyze_ticker',
|
|
|
|
side_effect=Exception('invalid ticker history ')
|
|
|
|
)
|
|
|
|
assert _STRATEGY.get_signal(exchange, 'ETH/BTC', '5m') == (False, False)
|
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_ohlcvdata_to_dataframe(default_conf, testdatadir) -> None:
|
2020-02-11 22:39:15 +00:00
|
|
|
default_conf.update({'strategy': 'DefaultStrategy'})
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2018-07-17 07:21:53 +00:00
|
|
|
|
2019-10-19 13:21:47 +00:00
|
|
|
timerange = TimeRange.parse_timerange('1510694220-1510700340')
|
2020-03-08 10:35:31 +00:00
|
|
|
data = load_data(testdatadir, '1m', ['UNITTEST/BTC'], timerange=timerange,
|
|
|
|
fill_up_missing=True)
|
|
|
|
processed = strategy.ohlcvdata_to_dataframe(data)
|
|
|
|
assert len(processed['UNITTEST/BTC']) == 102 # partial candle was removed
|
2018-08-17 04:50:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_min_roi_reached(default_conf, fee) -> None:
|
|
|
|
|
2019-01-12 12:38:49 +00:00
|
|
|
# Use list to confirm sequence does not matter
|
2019-01-01 15:32:45 +00:00
|
|
|
min_roi_list = [{20: 0.05, 55: 0.01, 0: 0.1},
|
|
|
|
{0: 0.1, 20: 0.05, 55: 0.01}]
|
|
|
|
for roi in min_roi_list:
|
2020-02-11 22:39:15 +00:00
|
|
|
default_conf.update({'strategy': 'DefaultStrategy'})
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-01-01 15:32:45 +00:00
|
|
|
strategy.minimal_roi = roi
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
2019-01-01 15:32:45 +00:00
|
|
|
open_date=arrow.utcnow().shift(hours=-1).datetime,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='bittrex',
|
|
|
|
open_rate=1,
|
|
|
|
)
|
|
|
|
|
2019-01-01 15:54:44 +00:00
|
|
|
assert not strategy.min_roi_reached(trade, 0.02, arrow.utcnow().shift(minutes=-56).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.12, arrow.utcnow().shift(minutes=-56).datetime)
|
2019-01-01 15:32:45 +00:00
|
|
|
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, arrow.utcnow().shift(minutes=-39).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.06, arrow.utcnow().shift(minutes=-39).datetime)
|
|
|
|
|
|
|
|
assert not strategy.min_roi_reached(trade, -0.01, arrow.utcnow().shift(minutes=-1).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.02, arrow.utcnow().shift(minutes=-1).datetime)
|
2018-09-01 18:01:18 +00:00
|
|
|
|
|
|
|
|
2019-01-12 12:38:49 +00:00
|
|
|
def test_min_roi_reached2(default_conf, fee) -> None:
|
|
|
|
|
|
|
|
# test with ROI raising after last interval
|
|
|
|
min_roi_list = [{20: 0.07,
|
|
|
|
30: 0.05,
|
|
|
|
55: 0.30,
|
|
|
|
0: 0.1
|
|
|
|
},
|
|
|
|
{0: 0.1,
|
|
|
|
20: 0.07,
|
|
|
|
30: 0.05,
|
|
|
|
55: 0.30
|
|
|
|
},
|
|
|
|
]
|
|
|
|
for roi in min_roi_list:
|
2020-02-11 22:39:15 +00:00
|
|
|
default_conf.update({'strategy': 'DefaultStrategy'})
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-01-12 12:38:49 +00:00
|
|
|
strategy.minimal_roi = roi
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
2019-01-12 12:38:49 +00:00
|
|
|
open_date=arrow.utcnow().shift(hours=-1).datetime,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='bittrex',
|
|
|
|
open_rate=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.02, arrow.utcnow().shift(minutes=-56).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.12, arrow.utcnow().shift(minutes=-56).datetime)
|
|
|
|
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, arrow.utcnow().shift(minutes=-39).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.071, arrow.utcnow().shift(minutes=-39).datetime)
|
|
|
|
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, arrow.utcnow().shift(minutes=-26).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.06, arrow.utcnow().shift(minutes=-26).datetime)
|
|
|
|
|
|
|
|
# Should not trigger with 20% profit since after 55 minutes only 30% is active.
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.20, arrow.utcnow().shift(minutes=-2).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.31, arrow.utcnow().shift(minutes=-2).datetime)
|
2019-06-20 00:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_min_roi_reached3(default_conf, fee) -> None:
|
|
|
|
|
|
|
|
# test for issue #1948
|
|
|
|
min_roi = {20: 0.07,
|
|
|
|
30: 0.05,
|
|
|
|
55: 0.30,
|
|
|
|
}
|
2020-02-11 22:39:15 +00:00
|
|
|
default_conf.update({'strategy': 'DefaultStrategy'})
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-06-20 00:26:02 +00:00
|
|
|
strategy.minimal_roi = min_roi
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
2019-06-20 00:26:02 +00:00
|
|
|
open_date=arrow.utcnow().shift(hours=-1).datetime,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='bittrex',
|
|
|
|
open_rate=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.02, arrow.utcnow().shift(minutes=-56).datetime)
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.12, arrow.utcnow().shift(minutes=-56).datetime)
|
|
|
|
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, arrow.utcnow().shift(minutes=-39).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.071, arrow.utcnow().shift(minutes=-39).datetime)
|
|
|
|
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.04, arrow.utcnow().shift(minutes=-26).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.06, arrow.utcnow().shift(minutes=-26).datetime)
|
|
|
|
|
|
|
|
# Should not trigger with 20% profit since after 55 minutes only 30% is active.
|
|
|
|
assert not strategy.min_roi_reached(trade, 0.20, arrow.utcnow().shift(minutes=-2).datetime)
|
|
|
|
assert strategy.min_roi_reached(trade, 0.31, arrow.utcnow().shift(minutes=-2).datetime)
|
2019-01-12 12:38:49 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_analyze_ticker_default(ohlcv_history, mocker, caplog) -> None:
|
2018-08-09 18:07:01 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-08-09 18:02:24 +00:00
|
|
|
ind_mock = MagicMock(side_effect=lambda x, meta: x)
|
|
|
|
buy_mock = MagicMock(side_effect=lambda x, meta: x)
|
|
|
|
sell_mock = MagicMock(side_effect=lambda x, meta: x)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.strategy.interface.IStrategy',
|
|
|
|
advise_indicators=ind_mock,
|
|
|
|
advise_buy=buy_mock,
|
|
|
|
advise_sell=sell_mock,
|
|
|
|
|
|
|
|
)
|
|
|
|
strategy = DefaultStrategy({})
|
2020-03-08 10:35:31 +00:00
|
|
|
strategy.analyze_ticker(ohlcv_history, {'pair': 'ETH/BTC'})
|
2018-08-09 18:02:24 +00:00
|
|
|
assert ind_mock.call_count == 1
|
|
|
|
assert buy_mock.call_count == 1
|
|
|
|
assert buy_mock.call_count == 1
|
|
|
|
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('TA Analysis Launched', caplog)
|
|
|
|
assert not log_has('Skipping TA Analysis for already analyzed candle', caplog)
|
2018-08-09 18:07:01 +00:00
|
|
|
caplog.clear()
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
strategy.analyze_ticker(ohlcv_history, {'pair': 'ETH/BTC'})
|
2018-09-01 17:52:40 +00:00
|
|
|
# No analysis happens as process_only_new_candles is true
|
2018-08-09 18:02:24 +00:00
|
|
|
assert ind_mock.call_count == 2
|
|
|
|
assert buy_mock.call_count == 2
|
|
|
|
assert buy_mock.call_count == 2
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('TA Analysis Launched', caplog)
|
|
|
|
assert not log_has('Skipping TA Analysis for already analyzed candle', caplog)
|
2018-08-09 18:02:24 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test__analyze_ticker_internal_skip_analyze(ohlcv_history, mocker, caplog) -> None:
|
2018-08-09 18:07:01 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-08-09 18:02:24 +00:00
|
|
|
ind_mock = MagicMock(side_effect=lambda x, meta: x)
|
|
|
|
buy_mock = MagicMock(side_effect=lambda x, meta: x)
|
|
|
|
sell_mock = MagicMock(side_effect=lambda x, meta: x)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.strategy.interface.IStrategy',
|
|
|
|
advise_indicators=ind_mock,
|
|
|
|
advise_buy=buy_mock,
|
|
|
|
advise_sell=sell_mock,
|
|
|
|
|
|
|
|
)
|
|
|
|
strategy = DefaultStrategy({})
|
2018-09-01 17:52:40 +00:00
|
|
|
strategy.process_only_new_candles = True
|
2018-08-09 18:02:24 +00:00
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
ret = strategy._analyze_ticker_internal(ohlcv_history, {'pair': 'ETH/BTC'})
|
2018-12-02 15:03:34 +00:00
|
|
|
assert 'high' in ret.columns
|
|
|
|
assert 'low' in ret.columns
|
|
|
|
assert 'close' in ret.columns
|
|
|
|
assert isinstance(ret, DataFrame)
|
2018-08-09 18:02:24 +00:00
|
|
|
assert ind_mock.call_count == 1
|
|
|
|
assert buy_mock.call_count == 1
|
|
|
|
assert buy_mock.call_count == 1
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('TA Analysis Launched', caplog)
|
|
|
|
assert not log_has('Skipping TA Analysis for already analyzed candle', caplog)
|
2018-08-09 18:07:01 +00:00
|
|
|
caplog.clear()
|
2018-08-09 18:02:24 +00:00
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
ret = strategy._analyze_ticker_internal(ohlcv_history, {'pair': 'ETH/BTC'})
|
2018-09-01 17:52:40 +00:00
|
|
|
# No analysis happens as process_only_new_candles is true
|
2018-08-09 18:02:24 +00:00
|
|
|
assert ind_mock.call_count == 1
|
|
|
|
assert buy_mock.call_count == 1
|
|
|
|
assert buy_mock.call_count == 1
|
|
|
|
# only skipped analyze adds buy and sell columns, otherwise it's all mocked
|
2018-12-02 15:03:34 +00:00
|
|
|
assert 'buy' in ret.columns
|
|
|
|
assert 'sell' in ret.columns
|
2018-08-09 18:02:24 +00:00
|
|
|
assert ret['buy'].sum() == 0
|
|
|
|
assert ret['sell'].sum() == 0
|
2019-08-11 18:16:52 +00:00
|
|
|
assert not log_has('TA Analysis Launched', caplog)
|
|
|
|
assert log_has('Skipping TA Analysis for already analyzed candle', caplog)
|
2019-08-12 17:50:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_is_pair_locked(default_conf):
|
2020-02-11 22:39:15 +00:00
|
|
|
default_conf.update({'strategy': 'DefaultStrategy'})
|
|
|
|
strategy = StrategyResolver.load_strategy(default_conf)
|
2019-08-12 17:50:22 +00:00
|
|
|
# dict should be empty
|
|
|
|
assert not strategy._pair_locked_until
|
|
|
|
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
assert not strategy.is_pair_locked(pair)
|
|
|
|
strategy.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime)
|
|
|
|
# ETH/BTC locked for 4 minutes
|
|
|
|
assert strategy.is_pair_locked(pair)
|
|
|
|
|
2019-12-22 08:46:00 +00:00
|
|
|
# Test lock does not change
|
|
|
|
lock = strategy._pair_locked_until[pair]
|
|
|
|
strategy.lock_pair(pair, arrow.utcnow().shift(minutes=2).datetime)
|
|
|
|
assert lock == strategy._pair_locked_until[pair]
|
|
|
|
|
2019-08-12 17:50:22 +00:00
|
|
|
# XRP/BTC should not be locked now
|
|
|
|
pair = 'XRP/BTC'
|
|
|
|
assert not strategy.is_pair_locked(pair)
|
2019-12-22 08:46:00 +00:00
|
|
|
|
|
|
|
# Unlocking a pair that's not locked should not raise an error
|
|
|
|
strategy.unlock_pair(pair)
|
|
|
|
|
|
|
|
# Unlock original pair
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
strategy.unlock_pair(pair)
|
|
|
|
assert not strategy.is_pair_locked(pair)
|