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-31 17:37:38 +00:00
|
|
|
import logging
|
2018-03-17 21:44:47 +00:00
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
2018-02-04 08:28:02 +00:00
|
|
|
import arrow
|
2017-09-28 21:26:28 +00:00
|
|
|
from pandas import DataFrame
|
|
|
|
|
2018-07-10 10:04:37 +00:00
|
|
|
from freqtrade.analyze import Analyze, parse_ticker_dataframe
|
2018-06-05 21:53:49 +00:00
|
|
|
from freqtrade.arguments import TimeRange
|
2018-07-04 07:31:35 +00:00
|
|
|
from freqtrade.optimize.__init__ import load_tickerdata_file
|
|
|
|
from freqtrade.tests.conftest import get_patched_exchange, log_has
|
2018-07-09 16:27:36 +00:00
|
|
|
from freqtrade.strategy.default_strategy import DefaultStrategy
|
2018-02-04 08:28:02 +00:00
|
|
|
|
|
|
|
# Avoid to reinit the same object again and again
|
2018-07-09 16:27:36 +00:00
|
|
|
_ANALYZE = Analyze({}, DefaultStrategy())
|
2018-02-04 08:28:02 +00:00
|
|
|
|
|
|
|
|
2017-11-07 19:12:56 +00:00
|
|
|
def test_dataframe_correct_length(result):
|
2018-07-10 10:04:37 +00:00
|
|
|
dataframe = parse_ticker_dataframe(result)
|
2018-06-07 09:56:39 +00:00
|
|
|
assert len(result.index) - 1 == len(dataframe.index) # last partial candle removed
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-30 23:36:35 +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() == \
|
2018-04-09 17:15:04 +00:00
|
|
|
['date', 'open', 'high', 'low', 'close', 'volume']
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2018-06-17 18:48:12 +00:00
|
|
|
def test_returns_latest_buy_signal(mocker, default_conf):
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=MagicMock())
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
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-06-17 18:48:12 +00:00
|
|
|
assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (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-06-17 18:48:12 +00:00
|
|
|
assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (False, True)
|
2017-11-14 18:28:31 +00:00
|
|
|
|
|
|
|
|
2018-06-17 18:48:12 +00:00
|
|
|
def test_returns_latest_sell_signal(mocker, default_conf):
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=MagicMock())
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
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-06-17 18:48:12 +00:00
|
|
|
assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (False, True)
|
2018-02-04 08:28:02 +00:00
|
|
|
|
|
|
|
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-06-17 18:48:12 +00:00
|
|
|
assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (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-06-17 18:48:12 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=None)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
assert (False, False) == _ANALYZE.get_signal(exchange, 'foo', default_conf['ticker_interval'])
|
2018-02-24 19:18:53 +00:00
|
|
|
assert 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-06-17 18:48:12 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=1)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2018-02-04 08:28:02 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
side_effect=ValueError('xyz')
|
|
|
|
)
|
|
|
|
)
|
2018-06-17 18:48:12 +00:00
|
|
|
assert (False, False) == _ANALYZE.get_signal(exchange, 'foo', default_conf['ticker_interval'])
|
2018-02-24 19:18:53 +00:00
|
|
|
assert 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-06-17 18:48:12 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=1)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2018-02-04 08:28:02 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.analyze.Analyze',
|
|
|
|
analyze_ticker=MagicMock(
|
|
|
|
return_value=DataFrame([])
|
|
|
|
)
|
|
|
|
)
|
2018-06-17 18:48:12 +00:00
|
|
|
assert (False, False) == _ANALYZE.get_signal(exchange, 'xyz', default_conf['ticker_interval'])
|
2018-02-24 19:18:53 +00:00
|
|
|
assert 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-06-17 18:48:12 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=1)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2018-07-08 11:34:47 +00:00
|
|
|
# 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)
|
2018-01-10 06:40:40 +00:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
)
|
2018-06-17 18:48:12 +00:00
|
|
|
assert (False, False) == _ANALYZE.get_signal(exchange, 'xyz', default_conf['ticker_interval'])
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-07-08 11:34:47 +00:00
|
|
|
'Outdated history for pair xyz. Last tick is 16 minutes old',
|
2018-03-04 10:06:40 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
2018-01-10 06:40:40 +00:00
|
|
|
|
|
|
|
|
2018-06-17 18:48:12 +00:00
|
|
|
def test_get_signal_handles_exceptions(mocker, default_conf):
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=MagicMock())
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
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-06-17 18:48:12 +00:00
|
|
|
assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (False, False)
|
2018-01-21 13:25:15 +00:00
|
|
|
|
|
|
|
|
2018-04-09 18:48:53 +00:00
|
|
|
def test_parse_ticker_dataframe(ticker_history):
|
2018-04-09 17:15:04 +00:00
|
|
|
columns = ['date', 'open', 'high', 'low', 'close', 'volume']
|
2018-01-21 13:25:15 +00:00
|
|
|
|
|
|
|
# Test file with BV data
|
2018-07-10 10:04:37 +00:00
|
|
|
dataframe = parse_ticker_dataframe(ticker_history)
|
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
|
|
|
|
"""
|
2018-07-09 16:27:36 +00:00
|
|
|
analyze = Analyze(default_conf, DefaultStrategy())
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange(None, 'line', 0, -100)
|
2018-03-24 09:21:59 +00:00
|
|
|
tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m', timerange=timerange)
|
2018-02-03 16:15:40 +00:00
|
|
|
tickerlist = {'UNITTEST/BTC': tick}
|
2018-03-02 13:46:32 +00:00
|
|
|
data = analyze.tickerdata_to_dataframe(tickerlist)
|
2018-06-07 10:12:44 +00:00
|
|
|
assert len(data['UNITTEST/BTC']) == 99 # partial candle was removed
|