stable/freqtrade/tests/test_analyze.py

75 lines
2.3 KiB
Python
Raw Normal View History

# pragma pylint: disable=missing-docstring,W0621
import json
from unittest.mock import MagicMock
2017-11-20 21:26:32 +00:00
import arrow
import pytest
2017-09-28 21:26:28 +00:00
from pandas import DataFrame
2018-01-16 20:21:43 +00:00
from freqtrade.analyze import (get_signal, parse_ticker_dataframe,
2018-01-10 07:51:36 +00:00
populate_buy_trend, populate_indicators,
populate_sell_trend)
2017-09-09 11:48:49 +00:00
2018-01-06 08:08:25 +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:
return parse_ticker_dataframe(json.load(data_file))
2017-10-30 23:36:35 +00:00
2017-11-07 19:12:56 +00:00
def test_dataframe_correct_columns(result):
assert result.columns.tolist() == \
2017-11-06 17:01:13 +00:00
['close', 'high', 'low', 'open', 'date', 'volume']
2017-10-30 23:36:35 +00:00
2017-11-07 19:12:56 +00:00
def test_dataframe_correct_length(result):
assert len(result.index) == 14395
2017-10-30 23:36:35 +00:00
2017-10-01 08:11:20 +00:00
def test_populates_buy_trend(result):
dataframe = populate_buy_trend(populate_indicators(result))
assert 'buy' in dataframe.columns
2017-11-18 07:19:22 +00:00
def test_populates_sell_trend(result):
dataframe = populate_sell_trend(populate_indicators(result))
assert 'sell' in dataframe.columns
2017-10-30 23:36:35 +00:00
def test_returns_latest_buy_signal(mocker):
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
mocker.patch(
'freqtrade.analyze.analyze_ticker',
2018-01-16 20:18:43 +00:00
return_value=DataFrame([{'buy': 1, 'sell': 0, 'date': arrow.utcnow()}])
)
2018-01-16 20:18:43 +00:00
assert get_signal('BTC-ETH') == (True, False)
mocker.patch(
'freqtrade.analyze.analyze_ticker',
2018-01-16 20:21:43 +00:00
return_value=DataFrame([{'buy': 0, 'sell': 1, 'date': arrow.utcnow()}])
)
2018-01-16 20:18:43 +00:00
assert get_signal('BTC-ETH') == (False, True)
2017-11-14 18:28:31 +00:00
def test_returns_latest_sell_signal(mocker):
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock())
mocker.patch(
'freqtrade.analyze.analyze_ticker',
2018-01-16 20:18:43 +00:00
return_value=DataFrame([{'sell': 1, 'buy': 0, 'date': arrow.utcnow()}])
)
2018-01-16 20:18:43 +00:00
assert get_signal('BTC-ETH') == (False, True)
2017-11-14 18:28:31 +00:00
mocker.patch(
'freqtrade.analyze.analyze_ticker',
2018-01-16 20:18:43 +00:00
return_value=DataFrame([{'sell': 0, 'buy': 1, 'date': arrow.utcnow()}])
)
2018-01-16 20:21:43 +00:00
assert get_signal('BTC-ETH') == (True, False)
2018-01-06 08:08:25 +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 '))
2018-01-16 20:21:43 +00:00
assert get_signal('BTC-ETH') == (False, False)