2018-01-28 07:38:41 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, protected-access, C0103
|
2017-12-28 08:11:52 +00:00
|
|
|
|
|
|
|
import os
|
2018-01-28 22:20:20 +00:00
|
|
|
import json
|
2017-12-28 08:11:52 +00:00
|
|
|
import logging
|
2018-01-28 22:38:17 +00:00
|
|
|
import uuid
|
2017-12-28 08:11:52 +00:00
|
|
|
from shutil import copyfile
|
|
|
|
from freqtrade import exchange, optimize
|
|
|
|
from freqtrade.exchange import Bittrex
|
2018-01-06 22:24:35 +00:00
|
|
|
from freqtrade.optimize.__init__ import make_testdata_path, download_pairs,\
|
2018-01-28 22:38:17 +00:00
|
|
|
download_backtesting_testdata, load_tickerdata_file, trim_tickerlist, file_dump_json
|
2018-01-05 09:20:48 +00:00
|
|
|
|
|
|
|
# Change this if modifying BTC_UNITEST 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-01-12 16:02:35 +00:00
|
|
|
def test_load_data_30min_ticker(default_conf, ticker_history, mocker, caplog):
|
|
|
|
mocker.patch('freqtrade.optimize.get_ticker_history', return_value=ticker_history)
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
|
|
|
|
exchange._API = Bittrex({'key': '', 'secret': ''})
|
|
|
|
|
2018-01-12 16:06:26 +00:00
|
|
|
file = 'freqtrade/tests/testdata/BTC_UNITTEST-30.json'
|
2018-01-12 16:02:35 +00:00
|
|
|
_backup_file(file, copy_file=True)
|
2018-01-12 16:06:26 +00:00
|
|
|
optimize.load_data(None, pairs=['BTC_UNITTEST'], ticker_interval=30)
|
2018-01-12 16:02:35 +00:00
|
|
|
assert os.path.isfile(file) is True
|
|
|
|
assert ('freqtrade.optimize',
|
|
|
|
logging.INFO,
|
2018-01-28 07:38:41 +00:00
|
|
|
'Download the pair: "BTC_ETH", Interval: 30 min') not in caplog.record_tuples
|
2018-01-12 16:02:35 +00:00
|
|
|
_clean_test_file(file)
|
|
|
|
|
2017-12-28 08:11:52 +00:00
|
|
|
|
|
|
|
def test_load_data_5min_ticker(default_conf, ticker_history, mocker, caplog):
|
|
|
|
mocker.patch('freqtrade.optimize.get_ticker_history', return_value=ticker_history)
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
|
|
|
|
exchange._API = Bittrex({'key': '', 'secret': ''})
|
|
|
|
|
|
|
|
file = 'freqtrade/tests/testdata/BTC_ETH-5.json'
|
|
|
|
_backup_file(file, copy_file=True)
|
2018-01-12 16:02:35 +00:00
|
|
|
optimize.load_data(None, pairs=['BTC_ETH'], ticker_interval=5)
|
2017-12-28 08:11:52 +00:00
|
|
|
assert os.path.isfile(file) is True
|
|
|
|
assert ('freqtrade.optimize',
|
|
|
|
logging.INFO,
|
2018-01-28 07:38:41 +00:00
|
|
|
'Download the pair: "BTC_ETH", Interval: 5 min') not in caplog.record_tuples
|
2017-12-28 08:11:52 +00:00
|
|
|
_clean_test_file(file)
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_data_1min_ticker(default_conf, ticker_history, mocker, caplog):
|
|
|
|
mocker.patch('freqtrade.optimize.get_ticker_history', return_value=ticker_history)
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
|
|
|
|
exchange._API = Bittrex({'key': '', 'secret': ''})
|
|
|
|
|
|
|
|
file = 'freqtrade/tests/testdata/BTC_ETH-1.json'
|
|
|
|
_backup_file(file, copy_file=True)
|
2018-01-06 22:24:35 +00:00
|
|
|
optimize.load_data(None, ticker_interval=1, pairs=['BTC_ETH'])
|
2017-12-28 08:11:52 +00:00
|
|
|
assert os.path.isfile(file) is True
|
|
|
|
assert ('freqtrade.optimize',
|
|
|
|
logging.INFO,
|
2018-01-28 07:38:41 +00:00
|
|
|
'Download the pair: "BTC_ETH", Interval: 1 min') not in caplog.record_tuples
|
2017-12-28 08:11:52 +00:00
|
|
|
_clean_test_file(file)
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_data_with_new_pair_1min(default_conf, ticker_history, mocker, caplog):
|
|
|
|
mocker.patch('freqtrade.optimize.get_ticker_history', return_value=ticker_history)
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
|
|
|
|
exchange._API = Bittrex({'key': '', 'secret': ''})
|
|
|
|
|
|
|
|
file = 'freqtrade/tests/testdata/BTC_MEME-1.json'
|
|
|
|
_backup_file(file)
|
2018-01-06 22:24:35 +00:00
|
|
|
optimize.load_data(None, ticker_interval=1, pairs=['BTC_MEME'])
|
2017-12-28 08:11:52 +00:00
|
|
|
assert os.path.isfile(file) is True
|
|
|
|
assert ('freqtrade.optimize',
|
|
|
|
logging.INFO,
|
2018-01-28 07:38:41 +00:00
|
|
|
'Download the pair: "BTC_MEME", Interval: 1 min') in caplog.record_tuples
|
2017-12-28 08:11:52 +00:00
|
|
|
_clean_test_file(file)
|
|
|
|
|
|
|
|
|
|
|
|
def test_testdata_path():
|
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
|
|
|
|
|
|
|
|
|
|
|
def test_download_pairs(default_conf, ticker_history, mocker):
|
|
|
|
mocker.patch('freqtrade.optimize.__init__.get_ticker_history', return_value=ticker_history)
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
exchange._API = Bittrex({'key': '', 'secret': ''})
|
|
|
|
|
|
|
|
file1_1 = 'freqtrade/tests/testdata/BTC_MEME-1.json'
|
|
|
|
file1_5 = 'freqtrade/tests/testdata/BTC_MEME-5.json'
|
|
|
|
file2_1 = 'freqtrade/tests/testdata/BTC_CFI-1.json'
|
|
|
|
file2_5 = 'freqtrade/tests/testdata/BTC_CFI-5.json'
|
|
|
|
|
|
|
|
_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-01-13 07:32:44 +00:00
|
|
|
assert download_pairs(None, pairs=['BTC-MEME', 'BTC-CFI'], ticker_interval=1) 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
|
|
|
|
|
|
|
assert download_pairs(None, pairs=['BTC-MEME', 'BTC-CFI'], ticker_interval=5) 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)
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_pairs_exception(default_conf, ticker_history, mocker, caplog):
|
|
|
|
mocker.patch('freqtrade.optimize.__init__.get_ticker_history', return_value=ticker_history)
|
|
|
|
mocker.patch('freqtrade.optimize.__init__.download_backtesting_testdata',
|
|
|
|
side_effect=BaseException('File Error'))
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
exchange._API = Bittrex({'key': '', 'secret': ''})
|
|
|
|
|
|
|
|
file1_1 = 'freqtrade/tests/testdata/BTC_MEME-1.json'
|
|
|
|
file1_5 = 'freqtrade/tests/testdata/BTC_MEME-5.json'
|
|
|
|
_backup_file(file1_1)
|
|
|
|
_backup_file(file1_5)
|
|
|
|
|
2018-01-13 07:32:44 +00:00
|
|
|
download_pairs(None, pairs=['BTC-MEME'], ticker_interval=1)
|
2017-12-28 08:11:52 +00:00
|
|
|
# clean files freshly downloaded
|
|
|
|
_clean_test_file(file1_1)
|
|
|
|
_clean_test_file(file1_5)
|
|
|
|
assert ('freqtrade.optimize.__init__',
|
|
|
|
logging.INFO,
|
2018-01-28 07:38:41 +00:00
|
|
|
'Failed to download the pair: "BTC-MEME", Interval: 1 min') in caplog.record_tuples
|
2017-12-28 08:11:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_download_backtesting_testdata(default_conf, ticker_history, mocker):
|
|
|
|
mocker.patch('freqtrade.optimize.__init__.get_ticker_history', return_value=ticker_history)
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
exchange._API = Bittrex({'key': '', 'secret': ''})
|
|
|
|
|
|
|
|
# Download a 1 min ticker file
|
|
|
|
file1 = 'freqtrade/tests/testdata/BTC_XEL-1.json'
|
|
|
|
_backup_file(file1)
|
2018-01-06 22:24:35 +00:00
|
|
|
download_backtesting_testdata(None, pair="BTC-XEL", interval=1)
|
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
|
|
|
|
file2 = 'freqtrade/tests/testdata/BTC_STORJ-5.json'
|
|
|
|
_backup_file(file2)
|
|
|
|
|
2018-01-06 22:24:35 +00:00
|
|
|
download_backtesting_testdata(None, pair="BTC-STORJ", interval=5)
|
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-01-26 16:56:56 +00:00
|
|
|
def test_download_backtesting_testdata2(mocker):
|
2018-01-10 11:00:00 +00:00
|
|
|
tick = [{'T': 'bar'}, {'T': 'foo'}]
|
2018-01-20 08:02:41 +00:00
|
|
|
mocker.patch('freqtrade.misc.file_dump_json', return_value=None)
|
2018-01-10 11:00:00 +00:00
|
|
|
mocker.patch('freqtrade.optimize.__init__.get_ticker_history', return_value=tick)
|
|
|
|
assert download_backtesting_testdata(None, pair="BTC-UNITEST", interval=1)
|
|
|
|
assert download_backtesting_testdata(None, pair="BTC-UNITEST", interval=3)
|
|
|
|
|
|
|
|
|
2018-01-05 09:20:48 +00:00
|
|
|
def test_load_tickerdata_file():
|
2018-01-29 03:21:01 +00:00
|
|
|
# 7 does not exist in either format.
|
2018-01-06 22:24:35 +00:00
|
|
|
assert not load_tickerdata_file(None, 'BTC_UNITEST', 7)
|
2018-01-29 03:21:01 +00:00
|
|
|
# 1 exists only as a .json
|
2018-01-06 22:24:35 +00:00
|
|
|
tickerdata = load_tickerdata_file(None, 'BTC_UNITEST', 1)
|
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-01-29 02:22:55 +00:00
|
|
|
tickerdata = load_tickerdata_file(None, 'BTC_UNITEST', 8)
|
|
|
|
assert _BTC_UNITTEST_LENGTH == len(tickerdata)
|
2018-01-10 11:00:00 +00:00
|
|
|
|
|
|
|
|
2018-01-20 20:24:28 +00:00
|
|
|
def test_init(default_conf, mocker):
|
2018-01-10 11:00:00 +00:00
|
|
|
conf = {'exchange': {'pair_whitelist': []}}
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt_optimize_conf', return_value=conf)
|
2018-01-20 20:24:28 +00:00
|
|
|
assert {} == optimize.load_data('', pairs=[], refresh_pairs=True,
|
|
|
|
ticker_interval=int(default_conf['ticker_interval']))
|
2018-01-19 07:05:52 +00:00
|
|
|
|
|
|
|
|
2018-01-10 22:03:05 +00:00
|
|
|
def test_tickerdata_to_dataframe():
|
2018-01-15 21:25:02 +00:00
|
|
|
timerange = ((None, 'line'), None, -100)
|
|
|
|
tick = load_tickerdata_file(None, 'BTC_UNITEST', 1, timerange=timerange)
|
2018-01-10 22:03:05 +00:00
|
|
|
tickerlist = {'BTC_UNITEST': tick}
|
2018-01-15 21:25:02 +00:00
|
|
|
data = optimize.tickerdata_to_dataframe(tickerlist)
|
2018-01-26 16:57:09 +00:00
|
|
|
assert len(data['BTC_UNITEST']) == 100
|
2018-01-28 22:20:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_trim_tickerlist():
|
|
|
|
with open('freqtrade/tests/testdata/BTC_ETH-1.json') as data_file:
|
|
|
|
ticker_list = json.load(data_file)
|
|
|
|
ticker_list_len = len(ticker_list)
|
|
|
|
|
|
|
|
# Test the pattern ^(-\d+)$
|
|
|
|
# This pattern remove X element from the beginning
|
|
|
|
timerange = ((None, 'line'), None, 5)
|
|
|
|
ticker = trim_tickerlist(ticker_list, timerange)
|
|
|
|
ticker_len = len(ticker)
|
|
|
|
|
|
|
|
assert ticker_list_len == ticker_len + 5
|
|
|
|
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
|
|
|
|
timerange = (('line', None), 5, None)
|
|
|
|
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
|
|
|
|
timerange = (('index', 'index'), 5, 10)
|
|
|
|
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-01-28 23:33:57 +00:00
|
|
|
# Test a wrong pattern
|
|
|
|
# This pattern must return the list unchanged
|
|
|
|
timerange = ((None, None), None, 5)
|
|
|
|
ticker = trim_tickerlist(ticker_list, timerange)
|
|
|
|
ticker_len = len(ticker)
|
|
|
|
|
|
|
|
assert ticker_list_len == ticker_len
|
|
|
|
|
2018-01-28 22:38:17 +00:00
|
|
|
|
|
|
|
def test_file_dump_json():
|
|
|
|
|
|
|
|
file = 'freqtrade/tests/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)
|