2018-01-28 07:38:41 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, C0103
|
2018-02-04 08:28:02 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Unit test file for analyse.py
|
|
|
|
"""
|
|
|
|
|
2018-01-26 16:52:39 +00:00
|
|
|
import datetime
|
2017-11-24 22:58:35 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-01-31 17:37:38 +00:00
|
|
|
import logging
|
2018-02-04 08:28:02 +00:00
|
|
|
import arrow
|
2017-09-28 21:26:28 +00:00
|
|
|
from pandas import DataFrame
|
|
|
|
|
2018-02-04 08:28:02 +00:00
|
|
|
from freqtrade.analyze import Analyze, SignalType
|
2018-03-02 13:46:32 +00:00
|
|
|
from freqtrade.optimize.__init__ import load_tickerdata_file
|
|
|
|
import freqtrade.tests.conftest as tt # test tools
|
2018-02-04 08:28:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Avoid to reinit the same object again and again
|
|
|
|
_ANALYZE = Analyze({'strategy': 'default_strategy'})
|
|
|
|
|
|
|
|
|
|
|
|
def test_signaltype_object() -> None:
|
|
|
|
"""
|
|
|
|
Test the SignalType object has the mandatory Constants
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
assert hasattr(SignalType, 'BUY')
|
|
|
|
assert hasattr(SignalType, 'SELL')
|
|
|
|
|
|
|
|
|
|
|
|
def test_analyze_object() -> None:
|
|
|
|
"""
|
|
|
|
Test the Analyze object has the mandatory methods
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
assert hasattr(Analyze, 'parse_ticker_dataframe')
|
|
|
|
assert hasattr(Analyze, 'populate_indicators')
|
|
|
|
assert hasattr(Analyze, 'populate_buy_trend')
|
|
|
|
assert hasattr(Analyze, 'populate_sell_trend')
|
|
|
|
assert hasattr(Analyze, 'analyze_ticker')
|
|
|
|
assert hasattr(Analyze, 'get_signal')
|
|
|
|
assert hasattr(Analyze, 'should_sell')
|
|
|
|
assert hasattr(Analyze, 'min_roi_reached')
|
2017-09-09 11:48:49 +00:00
|
|
|
|
2018-01-06 08:08:25 +00:00
|
|
|
|
2017-11-07 19:12:56 +00:00
|
|
|
def test_dataframe_correct_columns(result):
|
2017-10-01 08:02:47 +00:00
|
|
|
assert result.columns.tolist() == \
|
2017-11-06 17:01:13 +00:00
|
|
|
['close', 'high', 'low', 'open', 'date', 'volume']
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-01 08:11:20 +00:00
|
|
|
def test_populates_buy_trend(result):
|
2018-01-15 08:35:11 +00:00
|
|
|
# Load the default strategy for the unit test, because this logic is done in main.py
|
2018-02-04 08:28:02 +00:00
|
|
|
dataframe = _ANALYZE.populate_buy_trend(_ANALYZE.populate_indicators(result))
|
2017-10-01 08:02:47 +00:00
|
|
|
assert 'buy' in dataframe.columns
|
2017-11-17 10:08:23 +00:00
|
|
|
|
|
|
|
|
2017-11-18 07:19:22 +00:00
|
|
|
def test_populates_sell_trend(result):
|
2018-01-15 08:35:11 +00:00
|
|
|
# Load the default strategy for the unit test, because this logic is done in main.py
|
2018-02-04 08:28:02 +00:00
|
|
|
dataframe = _ANALYZE.populate_sell_trend(_ANALYZE.populate_indicators(result))
|
2017-11-17 10:08:23 +00:00
|
|
|
assert 'sell' in dataframe.columns
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
def test_returns_latest_buy_signal(mocker):
|
2017-11-24 22:58:35 +00:00
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
|
2018-02-04 08:28:02 +00:00
|
|
|
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
return_value=DataFrame([{'buy': 1, 'sell': 0, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2017-11-24 22:58:35 +00:00
|
|
|
)
|
2018-02-04 08:28:02 +00:00
|
|
|
assert _ANALYZE.get_signal('BTC-ETH', 5) == (True, False)
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2018-02-04 08:28:02 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
return_value=DataFrame([{'buy': 0, 'sell': 1, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2017-11-24 22:58:35 +00:00
|
|
|
)
|
2018-02-04 08:28:02 +00:00
|
|
|
assert _ANALYZE.get_signal('BTC-ETH', 5) == (False, True)
|
2017-11-14 18:28:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_returns_latest_sell_signal(mocker):
|
2017-11-24 22:58:35 +00:00
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
|
2018-02-04 08:28:02 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
return_value=DataFrame([{'sell': 1, 'buy': 0, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2017-11-24 22:58:35 +00:00
|
|
|
)
|
2017-11-14 18:28:31 +00:00
|
|
|
|
2018-02-04 08:28:02 +00:00
|
|
|
assert _ANALYZE.get_signal('BTC-ETH', 5) == (False, True)
|
|
|
|
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
return_value=DataFrame([{'sell': 0, 'buy': 1, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2017-11-24 22:58:35 +00:00
|
|
|
)
|
2018-02-04 08:28:02 +00:00
|
|
|
assert _ANALYZE.get_signal('BTC-ETH', 5) == (True, False)
|
2018-01-05 11:43:56 +00:00
|
|
|
|
2018-01-06 08:08:25 +00:00
|
|
|
|
2018-01-20 20:24:28 +00:00
|
|
|
def test_get_signal_empty(default_conf, mocker, caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2018-01-10 06:40:40 +00:00
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=None)
|
2018-02-04 08:28:02 +00:00
|
|
|
assert (False, False) == _ANALYZE.get_signal('foo', int(default_conf['ticker_interval']))
|
|
|
|
assert tt.log_has('Empty ticker history for pair foo', caplog.record_tuples)
|
2018-01-10 06:40:40 +00:00
|
|
|
|
|
|
|
|
2018-01-20 20:24:28 +00:00
|
|
|
def test_get_signal_exception_valueerror(default_conf, mocker, caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2018-01-10 06:40:40 +00:00
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
|
2018-02-04 08:28:02 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
side_effect=ValueError('xyz')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
assert (False, False) == _ANALYZE.get_signal('foo', int(default_conf['ticker_interval']))
|
2018-01-10 08:09:33 +00:00
|
|
|
assert tt.log_has('Unable to analyze ticker for pair foo: xyz',
|
|
|
|
caplog.record_tuples)
|
2018-01-10 06:40:40 +00:00
|
|
|
|
|
|
|
|
2018-01-20 20:24:28 +00:00
|
|
|
def test_get_signal_empty_dataframe(default_conf, mocker, caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2018-01-10 06:40:40 +00:00
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
|
2018-02-04 08:28:02 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
return_value=DataFrame([])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
assert (False, False) == _ANALYZE.get_signal('xyz', int(default_conf['ticker_interval']))
|
2018-01-10 08:09:33 +00:00
|
|
|
assert tt.log_has('Empty dataframe for pair xyz',
|
|
|
|
caplog.record_tuples)
|
2018-01-10 06:40:40 +00:00
|
|
|
|
|
|
|
|
2018-01-20 20:24:28 +00:00
|
|
|
def test_get_signal_old_dataframe(default_conf, mocker, caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2018-01-10 06:40:40 +00:00
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
|
|
|
|
# FIX: The get_signal function has hardcoded 10, which we must inturn hardcode
|
|
|
|
oldtime = arrow.utcnow() - datetime.timedelta(minutes=11)
|
|
|
|
ticks = DataFrame([{'buy': 1, 'date': oldtime}])
|
2018-02-04 08:28:02 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
return_value=DataFrame(ticks)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
assert (False, False) == _ANALYZE.get_signal('xyz', int(default_conf['ticker_interval']))
|
2018-01-10 08:09:33 +00:00
|
|
|
assert tt.log_has('Too old dataframe for pair xyz',
|
|
|
|
caplog.record_tuples)
|
2018-01-10 06:40:40 +00:00
|
|
|
|
|
|
|
|
2018-01-05 11:43:56 +00:00
|
|
|
def test_get_signal_handles_exceptions(mocker):
|
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
|
2018-02-04 08:28:02 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
side_effect=Exception('invalid ticker history ')
|
|
|
|
)
|
|
|
|
)
|
2018-01-05 11:43:56 +00:00
|
|
|
|
2018-02-04 08:28:02 +00:00
|
|
|
assert _ANALYZE.get_signal('BTC-ETH', 5) == (False, False)
|
2018-01-21 13:25:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_ticker_dataframe(ticker_history, ticker_history_without_bv):
|
|
|
|
columns = ['close', 'high', 'low', 'open', 'date', 'volume']
|
|
|
|
|
|
|
|
# Test file with BV data
|
2018-02-04 08:28:02 +00:00
|
|
|
dataframe = Analyze.parse_ticker_dataframe(ticker_history)
|
2018-01-21 13:25:15 +00:00
|
|
|
assert dataframe.columns.tolist() == columns
|
|
|
|
|
|
|
|
# Test file without BV data
|
2018-02-04 08:28:02 +00:00
|
|
|
dataframe = Analyze.parse_ticker_dataframe(ticker_history_without_bv)
|
2018-01-21 13:25:15 +00:00
|
|
|
assert dataframe.columns.tolist() == columns
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_tickerdata_to_dataframe(default_conf) -> None:
|
|
|
|
"""
|
|
|
|
Test Analyze.tickerdata_to_dataframe() method
|
|
|
|
"""
|
|
|
|
analyze = Analyze(default_conf)
|
|
|
|
|
|
|
|
timerange = ((None, 'line'), None, -100)
|
|
|
|
tick = load_tickerdata_file(None, 'BTC_UNITEST', 1, timerange=timerange)
|
|
|
|
tickerlist = {'BTC_UNITEST': tick}
|
|
|
|
data = analyze.tickerdata_to_dataframe(tickerlist)
|
|
|
|
assert len(data['BTC_UNITEST']) == 100
|