2018-01-28 07:38:41 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, protected-access, C0103
|
2017-12-28 08:11:52 +00:00
|
|
|
|
2018-01-28 22:20:20 +00:00
|
|
|
import json
|
2018-03-17 21:44:47 +00:00
|
|
|
import os
|
2018-01-28 22:38:17 +00:00
|
|
|
import uuid
|
2017-12-28 08:11:52 +00:00
|
|
|
from shutil import copyfile
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-07-04 07:31:35 +00:00
|
|
|
import arrow
|
|
|
|
|
2018-10-18 17:42:54 +00:00
|
|
|
from freqtrade import optimize, constants
|
2018-06-05 21:53:49 +00:00
|
|
|
from freqtrade.arguments import TimeRange
|
2018-07-04 07:31:35 +00:00
|
|
|
from freqtrade.misc import file_dump_json
|
|
|
|
from freqtrade.optimize.__init__ import (download_backtesting_testdata,
|
|
|
|
download_pairs,
|
|
|
|
load_cached_data_for_updating,
|
|
|
|
load_tickerdata_file,
|
|
|
|
make_testdata_path, trim_tickerlist)
|
2018-10-17 17:59:33 +00:00
|
|
|
from freqtrade.strategy.default_strategy import DefaultStrategy
|
|
|
|
from freqtrade.tests.conftest import get_patched_exchange, log_has, patch_exchange
|
2018-01-05 09:20:48 +00:00
|
|
|
|
2018-02-03 16:15:40 +00:00
|
|
|
# Change this if modifying UNITTEST/BTC testdatafile
|
2018-01-26 16:54:15 +00:00
|
|
|
_BTC_UNITTEST_LENGTH = 13681
|
2017-12-28 08:11:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _backup_file(file: str, copy_file: bool = False) -> None:
|
|
|
|
"""
|
|
|
|
Backup existing file to avoid deleting the user file
|
|
|
|
:param file: complete path to the file
|
|
|
|
:param touch_file: create an empty file in replacement
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
file_swp = file + '.swp'
|
|
|
|
if os.path.isfile(file):
|
|
|
|
os.rename(file, file_swp)
|
|
|
|
|
|
|
|
if copy_file:
|
|
|
|
copyfile(file_swp, file)
|
|
|
|
|
|
|
|
|
|
|
|
def _clean_test_file(file: str) -> None:
|
|
|
|
"""
|
|
|
|
Backup existing file to avoid deleting the user file
|
|
|
|
:param file: complete path to the file
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
file_swp = file + '.swp'
|
|
|
|
# 1. Delete file from the test
|
|
|
|
if os.path.isfile(file):
|
|
|
|
os.remove(file)
|
|
|
|
|
|
|
|
# 2. Rollback to the initial file
|
|
|
|
if os.path.isfile(file_swp):
|
|
|
|
os.rename(file_swp, file)
|
|
|
|
|
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
def test_load_data_30min_ticker(ticker_history, mocker, caplog, default_conf) -> None:
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history)
|
2018-04-12 17:21:40 +00:00
|
|
|
file = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'UNITTEST_BTC-30m.json')
|
2018-01-12 16:02:35 +00:00
|
|
|
_backup_file(file, copy_file=True)
|
2018-04-12 17:21:40 +00:00
|
|
|
optimize.load_data(None, pairs=['UNITTEST/BTC'], ticker_interval='30m')
|
2018-01-12 16:02:35 +00:00
|
|
|
assert os.path.isfile(file) is True
|
2018-04-12 17:21:40 +00:00
|
|
|
assert not log_has('Download the pair: "UNITTEST/BTC", Interval: 30m', caplog.record_tuples)
|
2018-01-12 16:02:35 +00:00
|
|
|
_clean_test_file(file)
|
|
|
|
|
2017-12-28 08:11:52 +00:00
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
def test_load_data_5min_ticker(ticker_history, mocker, caplog, default_conf) -> None:
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history)
|
2017-12-28 08:11:52 +00:00
|
|
|
|
2018-04-12 17:21:40 +00:00
|
|
|
file = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'UNITTEST_BTC-5m.json')
|
2017-12-28 08:11:52 +00:00
|
|
|
_backup_file(file, copy_file=True)
|
2018-04-12 17:21:40 +00:00
|
|
|
optimize.load_data(None, pairs=['UNITTEST/BTC'], ticker_interval='5m')
|
2017-12-28 08:11:52 +00:00
|
|
|
assert os.path.isfile(file) is True
|
2018-04-12 17:21:40 +00:00
|
|
|
assert not log_has('Download the pair: "UNITTEST/BTC", Interval: 5m', caplog.record_tuples)
|
2017-12-28 08:11:52 +00:00
|
|
|
_clean_test_file(file)
|
|
|
|
|
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
def test_load_data_1min_ticker(ticker_history, mocker, caplog) -> None:
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history)
|
2018-04-12 17:21:40 +00:00
|
|
|
file = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'UNITTEST_BTC-1m.json')
|
2017-12-28 08:11:52 +00:00
|
|
|
_backup_file(file, copy_file=True)
|
2018-04-12 17:21:40 +00:00
|
|
|
optimize.load_data(None, ticker_interval='1m', pairs=['UNITTEST/BTC'])
|
2017-12-28 08:11:52 +00:00
|
|
|
assert os.path.isfile(file) is True
|
2018-04-12 17:21:40 +00:00
|
|
|
assert not log_has('Download the pair: "UNITTEST/BTC", Interval: 1m', caplog.record_tuples)
|
2017-12-28 08:11:52 +00:00
|
|
|
_clean_test_file(file)
|
|
|
|
|
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
def test_load_data_with_new_pair_1min(ticker_history, mocker, caplog, default_conf) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
|
|
|
Test load_data() with 1 min ticker
|
|
|
|
"""
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history)
|
2018-06-17 20:12:22 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2018-03-26 11:14:36 +00:00
|
|
|
file = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-1m.json')
|
|
|
|
|
2017-12-28 08:11:52 +00:00
|
|
|
_backup_file(file)
|
2018-05-21 20:15:01 +00:00
|
|
|
# do not download a new pair if refresh_pairs isn't set
|
|
|
|
optimize.load_data(None,
|
|
|
|
ticker_interval='1m',
|
|
|
|
refresh_pairs=False,
|
|
|
|
pairs=['MEME/BTC'])
|
|
|
|
assert os.path.isfile(file) is False
|
2018-06-02 20:55:05 +00:00
|
|
|
assert log_has('No data for pair: "MEME/BTC", Interval: 1m. '
|
|
|
|
'Use --refresh-pairs-cached to download the data',
|
2018-05-21 20:15:01 +00:00
|
|
|
caplog.record_tuples)
|
|
|
|
|
|
|
|
# download a new pair if refresh_pairs is set
|
|
|
|
optimize.load_data(None,
|
|
|
|
ticker_interval='1m',
|
|
|
|
refresh_pairs=True,
|
2018-06-17 20:12:22 +00:00
|
|
|
exchange=exchange,
|
2018-05-21 20:15:01 +00:00
|
|
|
pairs=['MEME/BTC'])
|
2017-12-28 08:11:52 +00:00
|
|
|
assert os.path.isfile(file) is True
|
2018-03-26 11:14:36 +00:00
|
|
|
assert log_has('Download the pair: "MEME/BTC", Interval: 1m', caplog.record_tuples)
|
2017-12-28 08:11:52 +00:00
|
|
|
_clean_test_file(file)
|
|
|
|
|
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
def test_testdata_path() -> None:
|
2018-01-06 22:24:35 +00:00
|
|
|
assert os.path.join('freqtrade', 'tests', 'testdata') in make_testdata_path(None)
|
2017-12-28 08:11:52 +00:00
|
|
|
|
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
def test_download_pairs(ticker_history, mocker, default_conf) -> None:
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history)
|
2018-06-17 20:12:22 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2018-03-26 11:14:36 +00:00
|
|
|
file1_1 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-1m.json')
|
|
|
|
file1_5 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-5m.json')
|
|
|
|
file2_1 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'CFI_BTC-1m.json')
|
|
|
|
file2_5 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'CFI_BTC-5m.json')
|
2017-12-28 08:11:52 +00:00
|
|
|
|
|
|
|
_backup_file(file1_1)
|
|
|
|
_backup_file(file1_5)
|
|
|
|
_backup_file(file2_1)
|
|
|
|
_backup_file(file2_5)
|
|
|
|
|
2018-01-20 18:25:47 +00:00
|
|
|
assert os.path.isfile(file1_1) is False
|
|
|
|
assert os.path.isfile(file2_1) is False
|
2017-12-28 08:11:52 +00:00
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
assert download_pairs(None, exchange,
|
|
|
|
pairs=['MEME/BTC', 'CFI/BTC'], ticker_interval='1m') is True
|
2017-12-28 08:11:52 +00:00
|
|
|
|
|
|
|
assert os.path.isfile(file1_1) is True
|
|
|
|
assert os.path.isfile(file2_1) is True
|
|
|
|
|
|
|
|
# clean files freshly downloaded
|
|
|
|
_clean_test_file(file1_1)
|
|
|
|
_clean_test_file(file2_1)
|
2018-01-20 18:30:47 +00:00
|
|
|
|
2018-01-20 18:25:47 +00:00
|
|
|
assert os.path.isfile(file1_5) is False
|
|
|
|
assert os.path.isfile(file2_5) is False
|
2018-01-13 07:32:44 +00:00
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
assert download_pairs(None, exchange,
|
|
|
|
pairs=['MEME/BTC', 'CFI/BTC'], ticker_interval='5m') is True
|
2018-01-20 18:25:47 +00:00
|
|
|
|
2018-01-13 07:32:44 +00:00
|
|
|
assert os.path.isfile(file1_5) is True
|
2017-12-28 08:11:52 +00:00
|
|
|
assert os.path.isfile(file2_5) is True
|
|
|
|
|
|
|
|
# clean files freshly downloaded
|
|
|
|
_clean_test_file(file1_5)
|
|
|
|
_clean_test_file(file2_5)
|
|
|
|
|
|
|
|
|
2018-04-30 21:27:05 +00:00
|
|
|
def test_load_cached_data_for_updating(mocker) -> None:
|
|
|
|
datadir = os.path.join(os.path.dirname(__file__), '..', 'testdata')
|
|
|
|
|
|
|
|
test_data = None
|
|
|
|
test_filename = os.path.join(datadir, 'UNITTEST_BTC-1m.json')
|
|
|
|
with open(test_filename, "rt") as file:
|
|
|
|
test_data = json.load(file)
|
|
|
|
|
|
|
|
# change now time to test 'line' cases
|
|
|
|
# now = last cached item + 1 hour
|
|
|
|
now_ts = test_data[-1][0] / 1000 + 60 * 60
|
|
|
|
mocker.patch('arrow.utcnow', return_value=arrow.get(now_ts))
|
|
|
|
|
|
|
|
# timeframe starts earlier than the cached data
|
|
|
|
# should fully update data
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange('date', None, test_data[0][0] / 1000 - 1, 0)
|
2018-04-30 21:27:05 +00:00
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename,
|
|
|
|
'1m',
|
|
|
|
timerange)
|
|
|
|
assert data == []
|
|
|
|
assert start_ts == test_data[0][0] - 1000
|
|
|
|
|
|
|
|
# same with 'line' timeframe
|
|
|
|
num_lines = (test_data[-1][0] - test_data[1][0]) / 1000 / 60 + 120
|
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename,
|
|
|
|
'1m',
|
2018-06-05 21:53:49 +00:00
|
|
|
TimeRange(None, 'line', 0, -num_lines))
|
2018-04-30 21:27:05 +00:00
|
|
|
assert data == []
|
|
|
|
assert start_ts < test_data[0][0] - 1
|
|
|
|
|
|
|
|
# timeframe starts in the center of the cached data
|
|
|
|
# should return the chached data w/o the last item
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange('date', None, test_data[0][0] / 1000 + 1, 0)
|
2018-04-30 21:27:05 +00:00
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename,
|
|
|
|
'1m',
|
|
|
|
timerange)
|
|
|
|
assert data == test_data[:-1]
|
|
|
|
assert test_data[-2][0] < start_ts < test_data[-1][0]
|
|
|
|
|
|
|
|
# same with 'line' timeframe
|
|
|
|
num_lines = (test_data[-1][0] - test_data[1][0]) / 1000 / 60 + 30
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange(None, 'line', 0, -num_lines)
|
2018-04-30 21:27:05 +00:00
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename,
|
|
|
|
'1m',
|
|
|
|
timerange)
|
|
|
|
assert data == test_data[:-1]
|
|
|
|
assert test_data[-2][0] < start_ts < test_data[-1][0]
|
|
|
|
|
|
|
|
# timeframe starts after the chached data
|
|
|
|
# should return the chached data w/o the last item
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange('date', None, test_data[-1][0] / 1000 + 1, 0)
|
2018-04-30 21:27:05 +00:00
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename,
|
|
|
|
'1m',
|
|
|
|
timerange)
|
|
|
|
assert data == test_data[:-1]
|
|
|
|
assert test_data[-2][0] < start_ts < test_data[-1][0]
|
|
|
|
|
|
|
|
# same with 'line' timeframe
|
|
|
|
num_lines = 30
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange(None, 'line', 0, -num_lines)
|
2018-04-30 21:27:05 +00:00
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename,
|
|
|
|
'1m',
|
|
|
|
timerange)
|
|
|
|
assert data == test_data[:-1]
|
|
|
|
assert test_data[-2][0] < start_ts < test_data[-1][0]
|
|
|
|
|
|
|
|
# no timeframe is set
|
|
|
|
# should return the chached data w/o the last item
|
|
|
|
num_lines = 30
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange(None, 'line', 0, -num_lines)
|
2018-04-30 21:27:05 +00:00
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename,
|
|
|
|
'1m',
|
|
|
|
timerange)
|
|
|
|
assert data == test_data[:-1]
|
|
|
|
assert test_data[-2][0] < start_ts < test_data[-1][0]
|
|
|
|
|
|
|
|
# no datafile exist
|
|
|
|
# should return timestamp start time
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange('date', None, now_ts - 10000, 0)
|
2018-04-30 21:27:05 +00:00
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename + 'unexist',
|
|
|
|
'1m',
|
|
|
|
timerange)
|
|
|
|
assert data == []
|
|
|
|
assert start_ts == (now_ts - 10000) * 1000
|
|
|
|
|
|
|
|
# same with 'line' timeframe
|
|
|
|
num_lines = 30
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange(None, 'line', 0, -num_lines)
|
2018-04-30 21:27:05 +00:00
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename + 'unexist',
|
|
|
|
'1m',
|
|
|
|
timerange)
|
|
|
|
assert data == []
|
|
|
|
assert start_ts == (now_ts - num_lines * 60) * 1000
|
|
|
|
|
|
|
|
# no datafile exist, no timeframe is set
|
|
|
|
# should return an empty array and None
|
|
|
|
data, start_ts = load_cached_data_for_updating(test_filename + 'unexist',
|
|
|
|
'1m',
|
|
|
|
None)
|
|
|
|
assert data == []
|
|
|
|
assert start_ts is None
|
2018-04-27 21:30:42 +00:00
|
|
|
|
2018-04-27 21:16:34 +00:00
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
def test_download_pairs_exception(ticker_history, mocker, caplog, default_conf) -> None:
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history)
|
2017-12-28 08:11:52 +00:00
|
|
|
mocker.patch('freqtrade.optimize.__init__.download_backtesting_testdata',
|
|
|
|
side_effect=BaseException('File Error'))
|
2018-06-17 20:12:22 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2017-12-28 08:11:52 +00:00
|
|
|
|
2018-03-26 11:14:36 +00:00
|
|
|
file1_1 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-1m.json')
|
|
|
|
file1_5 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-5m.json')
|
2017-12-28 08:11:52 +00:00
|
|
|
_backup_file(file1_1)
|
|
|
|
_backup_file(file1_5)
|
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
download_pairs(None, exchange, pairs=['MEME/BTC'], ticker_interval='1m')
|
2017-12-28 08:11:52 +00:00
|
|
|
# clean files freshly downloaded
|
|
|
|
_clean_test_file(file1_1)
|
|
|
|
_clean_test_file(file1_5)
|
2018-03-26 11:14:36 +00:00
|
|
|
assert log_has('Failed to download the pair: "MEME/BTC", Interval: 1m', caplog.record_tuples)
|
2017-12-28 08:11:52 +00:00
|
|
|
|
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
def test_download_backtesting_testdata(ticker_history, mocker, default_conf) -> None:
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history)
|
2018-06-17 20:12:22 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2017-12-28 08:11:52 +00:00
|
|
|
|
|
|
|
# Download a 1 min ticker file
|
2018-03-26 11:14:36 +00:00
|
|
|
file1 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'XEL_BTC-1m.json')
|
2017-12-28 08:11:52 +00:00
|
|
|
_backup_file(file1)
|
2018-06-17 20:12:22 +00:00
|
|
|
download_backtesting_testdata(None, exchange, pair="XEL/BTC", tick_interval='1m')
|
2017-12-28 08:11:52 +00:00
|
|
|
assert os.path.isfile(file1) is True
|
|
|
|
_clean_test_file(file1)
|
|
|
|
|
|
|
|
# Download a 5 min ticker file
|
2018-03-26 11:14:36 +00:00
|
|
|
file2 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'STORJ_BTC-5m.json')
|
2017-12-28 08:11:52 +00:00
|
|
|
_backup_file(file2)
|
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
download_backtesting_testdata(None, exchange, pair="STORJ/BTC", tick_interval='5m')
|
2017-12-28 08:11:52 +00:00
|
|
|
assert os.path.isfile(file2) is True
|
|
|
|
_clean_test_file(file2)
|
2018-01-05 09:20:48 +00:00
|
|
|
|
|
|
|
|
2018-06-17 20:12:22 +00:00
|
|
|
def test_download_backtesting_testdata2(mocker, default_conf) -> None:
|
2018-04-09 18:21:54 +00:00
|
|
|
tick = [
|
|
|
|
[1509836520000, 0.00162008, 0.00162008, 0.00162008, 0.00162008, 108.14853839],
|
|
|
|
[1509836580000, 0.00161, 0.00161, 0.00161, 0.00161, 82.390199]
|
|
|
|
]
|
2018-03-29 18:17:11 +00:00
|
|
|
json_dump_mock = mocker.patch('freqtrade.misc.file_dump_json', return_value=None)
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=tick)
|
2018-06-17 20:12:22 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
download_backtesting_testdata(None, exchange, pair="UNITTEST/BTC", tick_interval='1m')
|
|
|
|
download_backtesting_testdata(None, exchange, pair="UNITTEST/BTC", tick_interval='3m')
|
2018-03-29 18:17:11 +00:00
|
|
|
assert json_dump_mock.call_count == 2
|
2018-01-10 11:00:00 +00:00
|
|
|
|
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
def test_load_tickerdata_file() -> None:
|
2018-01-29 03:21:01 +00:00
|
|
|
# 7 does not exist in either format.
|
2018-02-03 16:15:40 +00:00
|
|
|
assert not load_tickerdata_file(None, 'UNITTEST/BTC', '7m')
|
2018-01-29 03:21:01 +00:00
|
|
|
# 1 exists only as a .json
|
2018-02-03 16:15:40 +00:00
|
|
|
tickerdata = load_tickerdata_file(None, 'UNITTEST/BTC', '1m')
|
2018-01-26 16:54:15 +00:00
|
|
|
assert _BTC_UNITTEST_LENGTH == len(tickerdata)
|
2018-01-29 03:21:01 +00:00
|
|
|
# 8 .json is empty and will fail if it's loaded. .json.gz is a copy of 1.json
|
2018-02-03 16:15:40 +00:00
|
|
|
tickerdata = load_tickerdata_file(None, 'UNITTEST/BTC', '8m')
|
2018-01-29 02:22:55 +00:00
|
|
|
assert _BTC_UNITTEST_LENGTH == len(tickerdata)
|
2018-01-10 11:00:00 +00:00
|
|
|
|
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
def test_init(default_conf, mocker) -> None:
|
2018-06-17 20:12:22 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2018-02-09 07:35:38 +00:00
|
|
|
assert {} == optimize.load_data(
|
|
|
|
'',
|
2018-06-17 20:12:22 +00:00
|
|
|
exchange=exchange,
|
2018-02-09 07:35:38 +00:00
|
|
|
pairs=[],
|
|
|
|
refresh_pairs=True,
|
2018-02-03 16:15:40 +00:00
|
|
|
ticker_interval=default_conf['ticker_interval']
|
2018-02-09 07:35:38 +00:00
|
|
|
)
|
|
|
|
|
2018-01-19 07:05:52 +00:00
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
def test_trim_tickerlist() -> None:
|
2018-03-26 11:14:36 +00:00
|
|
|
file = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'UNITTEST_BTC-1m.json')
|
|
|
|
with open(file) as data_file:
|
2018-01-28 22:20:20 +00:00
|
|
|
ticker_list = json.load(data_file)
|
|
|
|
ticker_list_len = len(ticker_list)
|
|
|
|
|
|
|
|
# Test the pattern ^(-\d+)$
|
2018-04-27 21:16:34 +00:00
|
|
|
# This pattern uses the latest N elements
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange(None, 'line', 0, -5)
|
2018-01-28 22:20:20 +00:00
|
|
|
ticker = trim_tickerlist(ticker_list, timerange)
|
|
|
|
ticker_len = len(ticker)
|
|
|
|
|
2018-04-27 21:16:34 +00:00
|
|
|
assert ticker_len == 5
|
2018-01-28 22:20:20 +00:00
|
|
|
assert ticker_list[0] is not ticker[0] # The first element should be different
|
|
|
|
assert ticker_list[-1] is ticker[-1] # The last element must be the same
|
|
|
|
|
|
|
|
# Test the pattern ^(\d+)-$
|
|
|
|
# This pattern keep X element from the end
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange('line', None, 5, 0)
|
2018-01-28 22:20:20 +00:00
|
|
|
ticker = trim_tickerlist(ticker_list, timerange)
|
|
|
|
ticker_len = len(ticker)
|
|
|
|
|
|
|
|
assert ticker_len == 5
|
|
|
|
assert ticker_list[0] is ticker[0] # The first element must be the same
|
|
|
|
assert ticker_list[-1] is not ticker[-1] # The last element should be different
|
|
|
|
|
|
|
|
# Test the pattern ^(\d+)-(\d+)$
|
|
|
|
# This pattern extract a window
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange('index', 'index', 5, 10)
|
2018-01-28 22:20:20 +00:00
|
|
|
ticker = trim_tickerlist(ticker_list, timerange)
|
|
|
|
ticker_len = len(ticker)
|
|
|
|
|
|
|
|
assert ticker_len == 5
|
|
|
|
assert ticker_list[0] is not ticker[0] # The first element should be different
|
|
|
|
assert ticker_list[5] is ticker[0] # The list starts at the index 5
|
|
|
|
assert ticker_list[9] is ticker[-1] # The list ends at the index 9 (5 elements)
|
2018-01-28 22:38:17 +00:00
|
|
|
|
2018-04-27 21:16:34 +00:00
|
|
|
# Test the pattern ^(\d{8})-(\d{8})$
|
|
|
|
# This pattern extract a window between the dates
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange('date', 'date', ticker_list[5][0] / 1000, ticker_list[10][0] / 1000 - 1)
|
2018-04-27 21:16:34 +00:00
|
|
|
ticker = trim_tickerlist(ticker_list, timerange)
|
|
|
|
ticker_len = len(ticker)
|
|
|
|
|
|
|
|
assert ticker_len == 5
|
|
|
|
assert ticker_list[0] is not ticker[0] # The first element should be different
|
|
|
|
assert ticker_list[5] is ticker[0] # The list starts at the index 5
|
|
|
|
assert ticker_list[9] is ticker[-1] # The list ends at the index 9 (5 elements)
|
|
|
|
|
|
|
|
# Test the pattern ^-(\d{8})$
|
|
|
|
# This pattern extracts elements from the start to the date
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange(None, 'date', 0, ticker_list[10][0] / 1000 - 1)
|
2018-04-27 21:16:34 +00:00
|
|
|
ticker = trim_tickerlist(ticker_list, timerange)
|
|
|
|
ticker_len = len(ticker)
|
|
|
|
|
|
|
|
assert ticker_len == 10
|
|
|
|
assert ticker_list[0] is ticker[0] # The start of the list is included
|
|
|
|
assert ticker_list[9] is ticker[-1] # The element 10 is not included
|
|
|
|
|
|
|
|
# Test the pattern ^(\d{8})-$
|
|
|
|
# This pattern extracts elements from the date to now
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange('date', None, ticker_list[10][0] / 1000 - 1, None)
|
2018-04-27 21:16:34 +00:00
|
|
|
ticker = trim_tickerlist(ticker_list, timerange)
|
|
|
|
ticker_len = len(ticker)
|
|
|
|
|
|
|
|
assert ticker_len == ticker_list_len - 10
|
|
|
|
assert ticker_list[10] is ticker[0] # The first element is element #10
|
|
|
|
assert ticker_list[-1] is ticker[-1] # The last element is the same
|
|
|
|
|
2018-01-28 23:33:57 +00:00
|
|
|
# Test a wrong pattern
|
|
|
|
# This pattern must return the list unchanged
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange(None, None, None, 5)
|
2018-01-28 23:33:57 +00:00
|
|
|
ticker = trim_tickerlist(ticker_list, timerange)
|
|
|
|
ticker_len = len(ticker)
|
|
|
|
|
|
|
|
assert ticker_list_len == ticker_len
|
|
|
|
|
2018-01-28 22:38:17 +00:00
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
def test_file_dump_json() -> None:
|
2018-03-26 11:14:36 +00:00
|
|
|
file = os.path.join(os.path.dirname(__file__), '..', 'testdata',
|
|
|
|
'test_{id}.json'.format(id=str(uuid.uuid4())))
|
2018-01-28 23:03:54 +00:00
|
|
|
data = {'bar': 'foo'}
|
2018-01-28 22:38:17 +00:00
|
|
|
|
|
|
|
# check the file we will create does not exist
|
|
|
|
assert os.path.isfile(file) is False
|
|
|
|
|
|
|
|
# Create the Json file
|
|
|
|
file_dump_json(file, data)
|
|
|
|
|
|
|
|
# Check the file was create
|
|
|
|
assert os.path.isfile(file) is True
|
|
|
|
|
|
|
|
# Open the Json file created and test the data is in it
|
|
|
|
with open(file) as data_file:
|
|
|
|
json_from_file = json.load(data_file)
|
|
|
|
|
|
|
|
assert 'bar' in json_from_file
|
|
|
|
assert json_from_file['bar'] == 'foo'
|
|
|
|
|
|
|
|
# Remove the file
|
|
|
|
_clean_test_file(file)
|
2018-10-17 17:59:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_timeframe(default_conf, mocker) -> None:
|
|
|
|
patch_exchange(mocker)
|
|
|
|
strategy = DefaultStrategy(default_conf)
|
|
|
|
|
|
|
|
data = strategy.tickerdata_to_dataframe(
|
|
|
|
optimize.load_data(
|
|
|
|
None,
|
|
|
|
ticker_interval='1m',
|
|
|
|
pairs=['UNITTEST/BTC']
|
|
|
|
)
|
|
|
|
)
|
|
|
|
min_date, max_date = optimize.get_timeframe(data)
|
|
|
|
assert min_date.isoformat() == '2017-11-04T23:02:00+00:00'
|
|
|
|
assert max_date.isoformat() == '2017-11-14T22:58:00+00:00'
|
2018-10-18 17:42:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_validate_backtest_data_warn(default_conf, mocker, caplog) -> None:
|
|
|
|
patch_exchange(mocker)
|
|
|
|
strategy = DefaultStrategy(default_conf)
|
|
|
|
|
|
|
|
data = strategy.tickerdata_to_dataframe(
|
|
|
|
optimize.load_data(
|
|
|
|
None,
|
|
|
|
ticker_interval='1m',
|
|
|
|
pairs=['UNITTEST/BTC']
|
|
|
|
)
|
|
|
|
)
|
|
|
|
min_date, max_date = optimize.get_timeframe(data)
|
|
|
|
caplog.clear()
|
2018-10-18 17:48:54 +00:00
|
|
|
assert optimize.validate_backtest_data(data, min_date, max_date,
|
|
|
|
constants.TICKER_INTERVAL_MINUTES["1m"])
|
2018-10-18 17:42:54 +00:00
|
|
|
assert len(caplog.record_tuples) == 1
|
|
|
|
assert log_has('UNITTEST/BTC has missing frames: expected 14396, got 13680',
|
|
|
|
caplog.record_tuples)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_backtest_data(default_conf, mocker, caplog) -> None:
|
|
|
|
patch_exchange(mocker)
|
|
|
|
strategy = DefaultStrategy(default_conf)
|
|
|
|
|
|
|
|
timerange = TimeRange('index', 'index', 200, 250)
|
|
|
|
data = strategy.tickerdata_to_dataframe(
|
|
|
|
optimize.load_data(
|
|
|
|
None,
|
|
|
|
ticker_interval='5m',
|
|
|
|
pairs=['UNITTEST/BTC'],
|
|
|
|
timerange=timerange
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
min_date, max_date = optimize.get_timeframe(data)
|
|
|
|
caplog.clear()
|
2018-10-18 17:48:54 +00:00
|
|
|
assert not optimize.validate_backtest_data(data, min_date, max_date,
|
|
|
|
constants.TICKER_INTERVAL_MINUTES["5m"])
|
2018-10-18 17:42:54 +00:00
|
|
|
assert len(caplog.record_tuples) == 0
|
|
|
|
|