2017-11-18 07:45:01 +00:00
|
|
|
# pragma pylint: disable=missing-docstring,W0621
|
2017-10-26 13:50:31 +00:00
|
|
|
import json
|
2017-11-24 22:58:35 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-01-10 08:09:33 +00:00
|
|
|
import freqtrade.tests.conftest as tt # test tools
|
2017-11-20 21:26:32 +00:00
|
|
|
|
2017-11-19 03:58:35 +00:00
|
|
|
import arrow
|
2018-01-10 06:40:40 +00:00
|
|
|
import datetime
|
2017-10-01 08:02:47 +00:00
|
|
|
import pytest
|
2017-09-28 21:26:28 +00:00
|
|
|
from pandas import DataFrame
|
|
|
|
|
|
|
|
from freqtrade.analyze import parse_ticker_dataframe, populate_buy_trend, populate_indicators, \
|
2017-11-17 10:08:23 +00:00
|
|
|
get_signal, SignalType, populate_sell_trend
|
2017-09-09 11:48:49 +00:00
|
|
|
|
2018-01-06 08:08:25 +00:00
|
|
|
|
2017-10-01 08:02:47 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def result():
|
2017-11-14 23:15:17 +00:00
|
|
|
with open('freqtrade/tests/testdata/BTC_ETH-1.json') as data_file:
|
2017-11-05 22:47:59 +00:00
|
|
|
return parse_ticker_dataframe(json.load(data_file))
|
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() == \
|
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-11-07 19:12:56 +00:00
|
|
|
def test_dataframe_correct_length(result):
|
2017-12-10 11:56:39 +00:00
|
|
|
assert len(result.index) == 14395
|
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):
|
2017-10-01 08:02:47 +00:00
|
|
|
dataframe = populate_buy_trend(populate_indicators(result))
|
|
|
|
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):
|
2017-11-17 10:08:23 +00:00
|
|
|
dataframe = populate_sell_trend(populate_indicators(result))
|
|
|
|
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())
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.analyze.analyze_ticker',
|
|
|
|
return_value=DataFrame([{'buy': 1, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2017-11-14 17:06:03 +00:00
|
|
|
assert get_signal('BTC-ETH', SignalType.BUY)
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2017-11-24 22:58:35 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.analyze.analyze_ticker',
|
|
|
|
return_value=DataFrame([{'buy': 0, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2017-11-14 17:06:03 +00:00
|
|
|
assert not get_signal('BTC-ETH', SignalType.BUY)
|
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())
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.analyze.analyze_ticker',
|
|
|
|
return_value=DataFrame([{'sell': 1, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2017-11-14 18:28:31 +00:00
|
|
|
assert get_signal('BTC-ETH', SignalType.SELL)
|
|
|
|
|
2017-11-24 22:58:35 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.analyze.analyze_ticker',
|
|
|
|
return_value=DataFrame([{'sell': 0, 'date': arrow.utcnow()}])
|
|
|
|
)
|
2017-11-14 18:28:31 +00:00
|
|
|
assert not get_signal('BTC-ETH', SignalType.SELL)
|
2018-01-05 11:43:56 +00:00
|
|
|
|
2018-01-06 08:08:25 +00:00
|
|
|
|
2018-01-10 06:40:40 +00:00
|
|
|
def test_get_signal_empty(mocker, caplog):
|
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=None)
|
|
|
|
assert not get_signal('foo', SignalType.BUY)
|
2018-01-10 08:09:33 +00:00
|
|
|
assert tt.log_has('Empty ticker history for pair foo',
|
|
|
|
caplog.record_tuples)
|
2018-01-10 06:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_signal_execption_valueerror(mocker, caplog):
|
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
|
|
|
|
mocker.patch('freqtrade.analyze.analyze_ticker',
|
|
|
|
side_effect=ValueError('xyz'))
|
|
|
|
assert not get_signal('foo', SignalType.BUY)
|
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
|
|
|
|
|
|
|
|
|
|
|
# This error should never occur becase analyze_ticker is run first,
|
|
|
|
# and that function can only add columns, it cant delete all rows from the dataframe
|
|
|
|
def test_get_signal_empty_dataframe(mocker, caplog):
|
|
|
|
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
|
|
|
|
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=DataFrame([]))
|
|
|
|
assert not get_signal('xyz', SignalType.BUY)
|
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
|
|
|
|
|
|
|
|
|
|
|
def test_get_signal_old_dataframe(mocker, caplog):
|
|
|
|
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}])
|
|
|
|
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=DataFrame(ticks))
|
|
|
|
assert not get_signal('xyz', SignalType.BUY)
|
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())
|
|
|
|
mocker.patch('freqtrade.analyze.analyze_ticker',
|
|
|
|
side_effect=Exception('invalid ticker history '))
|
|
|
|
|
|
|
|
assert not get_signal('BTC-ETH', SignalType.BUY)
|