2018-02-09 07:35:38 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, W0212, line-too-long, C0103, unused-argument
|
2017-11-24 22:58:35 +00:00
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
import json
|
2017-12-28 14:58:02 +00:00
|
|
|
import math
|
2018-03-17 21:44:47 +00:00
|
|
|
import random
|
2018-02-09 07:35:38 +00:00
|
|
|
from copy import deepcopy
|
2018-03-17 21:44:47 +00:00
|
|
|
from typing import List
|
2018-01-05 09:23:12 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-02-08 19:49:43 +00:00
|
|
|
import numpy as np
|
2018-03-17 21:44:47 +00:00
|
|
|
import pandas as pd
|
|
|
|
from arrow import Arrow
|
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
from freqtrade import optimize
|
|
|
|
from freqtrade.analyze import Analyze
|
2018-06-05 21:53:49 +00:00
|
|
|
from freqtrade.arguments import Arguments, TimeRange
|
2018-03-17 21:44:47 +00:00
|
|
|
from freqtrade.optimize.backtesting import Backtesting, start, setup_configuration
|
2018-04-12 16:13:35 +00:00
|
|
|
from freqtrade.tests.conftest import log_has
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_args(args) -> List[str]:
|
|
|
|
return Arguments(args, '').get_parsed_arg()
|
2017-11-24 22:58:35 +00:00
|
|
|
|
2017-12-16 02:39:47 +00:00
|
|
|
|
2018-01-28 07:38:41 +00:00
|
|
|
def trim_dictlist(dict_list, num):
|
2018-01-17 17:19:39 +00:00
|
|
|
new = {}
|
2018-01-28 07:38:41 +00:00
|
|
|
for pair, pair_data in dict_list.items():
|
2018-01-17 17:19:39 +00:00
|
|
|
new[pair] = pair_data[num:]
|
|
|
|
return new
|
|
|
|
|
|
|
|
|
2017-12-28 14:58:02 +00:00
|
|
|
def load_data_test(what):
|
2018-06-07 07:21:07 +00:00
|
|
|
timerange = TimeRange(None, 'line', 0, -101)
|
2018-04-12 16:13:35 +00:00
|
|
|
data = optimize.load_data(None, ticker_interval='1m',
|
|
|
|
pairs=['UNITTEST/BTC'], timerange=timerange)
|
2018-02-03 16:15:40 +00:00
|
|
|
pair = data['UNITTEST/BTC']
|
2017-12-30 10:55:23 +00:00
|
|
|
datalen = len(pair)
|
2017-12-28 14:58:02 +00:00
|
|
|
# Depending on the what parameter we now adjust the
|
2017-12-30 10:55:23 +00:00
|
|
|
# loaded data looks:
|
2018-04-09 18:21:54 +00:00
|
|
|
# pair :: [[ 1509836520000, unix timestamp in ms
|
|
|
|
# 0.00162008, open
|
|
|
|
# 0.00162008, high
|
|
|
|
# 0.00162008, low
|
|
|
|
# 0.00162008, close
|
|
|
|
# 108.14853839 base volume
|
|
|
|
# ]]
|
2017-12-30 10:55:23 +00:00
|
|
|
base = 0.001
|
2017-12-28 14:58:02 +00:00
|
|
|
if what == 'raise':
|
2018-04-09 18:21:54 +00:00
|
|
|
return {'UNITTEST/BTC': [
|
|
|
|
[
|
|
|
|
pair[x][0], # Keep old dates
|
|
|
|
x * base, # But replace O,H,L,C
|
|
|
|
x * base + 0.0001,
|
|
|
|
x * base - 0.0001,
|
|
|
|
x * base,
|
|
|
|
pair[x][5], # Keep old volume
|
|
|
|
] for x in range(0, datalen)
|
|
|
|
]}
|
2017-12-28 14:58:02 +00:00
|
|
|
if what == 'lower':
|
2018-04-09 18:21:54 +00:00
|
|
|
return {'UNITTEST/BTC': [
|
|
|
|
[
|
|
|
|
pair[x][0], # Keep old dates
|
|
|
|
1 - x * base, # But replace O,H,L,C
|
|
|
|
1 - x * base + 0.0001,
|
|
|
|
1 - x * base - 0.0001,
|
|
|
|
1 - x * base,
|
|
|
|
pair[x][5] # Keep old volume
|
|
|
|
] for x in range(0, datalen)
|
|
|
|
]}
|
2017-12-28 14:58:02 +00:00
|
|
|
if what == 'sine':
|
2017-12-30 10:55:23 +00:00
|
|
|
hz = 0.1 # frequency
|
2018-04-09 18:21:54 +00:00
|
|
|
return {'UNITTEST/BTC': [
|
|
|
|
[
|
|
|
|
pair[x][0], # Keep old dates
|
|
|
|
math.sin(x * hz) / 1000 + base, # But replace O,H,L,C
|
|
|
|
math.sin(x * hz) / 1000 + base + 0.0001,
|
|
|
|
math.sin(x * hz) / 1000 + base - 0.0001,
|
|
|
|
math.sin(x * hz) / 1000 + base,
|
|
|
|
pair[x][5] # Keep old volume
|
|
|
|
] for x in range(0, datalen)
|
|
|
|
]}
|
2017-12-28 14:58:02 +00:00
|
|
|
return data
|
|
|
|
|
2017-12-28 19:05:33 +00:00
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def simple_backtest(config, contour, num_results, mocker) -> None:
|
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtesting = Backtesting(config)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
2017-12-28 14:58:02 +00:00
|
|
|
data = load_data_test(contour)
|
2018-02-09 07:35:38 +00:00
|
|
|
processed = backtesting.tickerdata_to_dataframe(data)
|
2017-12-28 14:58:02 +00:00
|
|
|
assert isinstance(processed, dict)
|
2018-02-09 07:35:38 +00:00
|
|
|
results = backtesting.backtest(
|
|
|
|
{
|
|
|
|
'stake_amount': config['stake_amount'],
|
|
|
|
'processed': processed,
|
|
|
|
'max_open_trades': 1,
|
|
|
|
'realistic': True
|
|
|
|
}
|
|
|
|
)
|
2017-12-28 14:58:02 +00:00
|
|
|
# results :: <class 'pandas.core.frame.DataFrame'>
|
|
|
|
assert len(results) == num_results
|
|
|
|
|
2017-12-30 10:55:23 +00:00
|
|
|
|
2018-03-25 20:03:26 +00:00
|
|
|
def mocked_load_data(datadir, pairs=[], ticker_interval='0m', refresh_pairs=False, timerange=None):
|
|
|
|
tickerdata = optimize.load_tickerdata_file(datadir, 'UNITTEST/BTC', '1m', timerange=timerange)
|
2018-02-03 16:15:40 +00:00
|
|
|
pairdata = {'UNITTEST/BTC': tickerdata}
|
2018-02-09 07:35:38 +00:00
|
|
|
return pairdata
|
|
|
|
|
|
|
|
|
2018-02-08 19:49:43 +00:00
|
|
|
# use for mock freqtrade.exchange.get_ticker_history'
|
|
|
|
def _load_pair_as_ticks(pair, tickfreq):
|
2018-03-04 00:42:37 +00:00
|
|
|
ticks = optimize.load_data(None, ticker_interval=tickfreq, pairs=[pair])
|
2018-06-07 07:21:07 +00:00
|
|
|
ticks = trim_dictlist(ticks, -201)
|
2018-02-08 19:49:43 +00:00
|
|
|
return ticks[pair]
|
|
|
|
|
|
|
|
|
|
|
|
# FIX: fixturize this?
|
2018-04-21 22:19:11 +00:00
|
|
|
def _make_backtest_conf(mocker, conf=None, pair='UNITTEST/BTC', record=None):
|
2018-03-25 20:03:26 +00:00
|
|
|
data = optimize.load_data(None, ticker_interval='8m', pairs=[pair])
|
2018-06-07 07:21:07 +00:00
|
|
|
data = trim_dictlist(data, -201)
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtesting = Backtesting(conf)
|
2018-03-04 00:42:37 +00:00
|
|
|
return {
|
|
|
|
'stake_amount': conf['stake_amount'],
|
2018-04-21 22:19:11 +00:00
|
|
|
'processed': backtesting.tickerdata_to_dataframe(data),
|
2018-03-04 00:42:37 +00:00
|
|
|
'max_open_trades': 10,
|
|
|
|
'realistic': True,
|
|
|
|
'record': record
|
|
|
|
}
|
2018-02-08 19:49:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _trend(signals, buy_value, sell_value):
|
|
|
|
n = len(signals['low'])
|
|
|
|
buy = np.zeros(n)
|
|
|
|
sell = np.zeros(n)
|
|
|
|
for i in range(0, len(signals['buy'])):
|
|
|
|
if random.random() > 0.5: # Both buy and sell signals at same timeframe
|
|
|
|
buy[i] = buy_value
|
|
|
|
sell[i] = sell_value
|
|
|
|
signals['buy'] = buy
|
|
|
|
signals['sell'] = sell
|
|
|
|
return signals
|
|
|
|
|
|
|
|
|
|
|
|
def _trend_alternate(dataframe=None):
|
|
|
|
signals = dataframe
|
|
|
|
low = signals['low']
|
|
|
|
n = len(low)
|
|
|
|
buy = np.zeros(n)
|
|
|
|
sell = np.zeros(n)
|
|
|
|
for i in range(0, len(buy)):
|
|
|
|
if i % 2 == 0:
|
|
|
|
buy[i] = 1
|
|
|
|
else:
|
|
|
|
sell[i] = 1
|
|
|
|
signals['buy'] = buy
|
|
|
|
signals['sell'] = sell
|
|
|
|
return dataframe
|
|
|
|
|
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
# Unit tests
|
|
|
|
def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None:
|
|
|
|
"""
|
|
|
|
Test setup_configuration() function
|
|
|
|
"""
|
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'--config', 'config.json',
|
2018-03-24 19:44:04 +00:00
|
|
|
'--strategy', 'DefaultStrategy',
|
2018-02-09 07:35:38 +00:00
|
|
|
'backtesting'
|
|
|
|
]
|
|
|
|
|
|
|
|
config = setup_configuration(get_args(args))
|
|
|
|
assert 'max_open_trades' in config
|
|
|
|
assert 'stake_currency' in config
|
|
|
|
assert 'stake_amount' in config
|
|
|
|
assert 'exchange' in config
|
|
|
|
assert 'pair_whitelist' in config['exchange']
|
|
|
|
assert 'datadir' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-06-02 08:32:05 +00:00
|
|
|
'Using data folder: {} ...'.format(config['datadir']),
|
2018-02-09 07:35:38 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
assert 'ticker_interval' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert not log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
assert 'live' not in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
assert 'realistic_simulation' not in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert not log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
assert 'refresh_pairs' not in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
assert 'timerange' not in config
|
|
|
|
assert 'export' not in config
|
|
|
|
|
|
|
|
|
|
|
|
def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> None:
|
|
|
|
"""
|
|
|
|
Test setup_configuration() function
|
|
|
|
"""
|
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'--config', 'config.json',
|
2018-03-24 19:44:04 +00:00
|
|
|
'--strategy', 'DefaultStrategy',
|
2018-02-09 07:35:38 +00:00
|
|
|
'--datadir', '/foo/bar',
|
|
|
|
'backtesting',
|
2018-03-25 20:03:26 +00:00
|
|
|
'--ticker-interval', '1m',
|
2018-02-09 07:35:38 +00:00
|
|
|
'--live',
|
|
|
|
'--realistic-simulation',
|
|
|
|
'--refresh-pairs-cached',
|
|
|
|
'--timerange', ':100',
|
2018-06-03 12:52:03 +00:00
|
|
|
'--export', '/bar/foo',
|
|
|
|
'--export-filename', 'foo_bar.json'
|
2018-02-09 07:35:38 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
config = setup_configuration(get_args(args))
|
|
|
|
assert 'max_open_trades' in config
|
|
|
|
assert 'stake_currency' in config
|
|
|
|
assert 'stake_amount' in config
|
|
|
|
assert 'exchange' in config
|
|
|
|
assert 'pair_whitelist' in config['exchange']
|
|
|
|
assert 'datadir' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-06-02 08:32:05 +00:00
|
|
|
'Using data folder: {} ...'.format(config['datadir']),
|
2018-02-09 07:35:38 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
assert 'ticker_interval' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
|
|
|
assert log_has(
|
2018-03-25 20:03:26 +00:00
|
|
|
'Using ticker_interval: 1m ...',
|
2018-02-09 07:35:38 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
|
|
|
assert 'live' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
2018-04-09 18:21:54 +00:00
|
|
|
assert 'realistic_simulation' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
|
|
|
assert log_has('Using max_open_trades: 1 ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
2018-04-09 18:21:54 +00:00
|
|
|
assert 'refresh_pairs' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
assert 'timerange' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-02-09 07:35:38 +00:00
|
|
|
'Parameter --timerange detected: {} ...'.format(config['timerange']),
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
|
|
|
assert 'export' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-02-09 07:35:38 +00:00
|
|
|
'Parameter --export detected: {} ...'.format(config['export']),
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
2018-06-03 12:52:03 +00:00
|
|
|
assert 'exportfilename' in config
|
|
|
|
assert log_has(
|
|
|
|
'Storing backtest results to {} ...'.format(config['exportfilename']),
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_start(mocker, fee, default_conf, caplog) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
|
|
|
Test start() function
|
|
|
|
"""
|
|
|
|
start_mock = MagicMock()
|
2018-04-21 21:20:12 +00:00
|
|
|
mocker.patch('freqtrade.exchange.get_fee', fee)
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
2018-02-09 07:35:38 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.start', start_mock)
|
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
args = [
|
|
|
|
'--config', 'config.json',
|
2018-03-24 19:44:04 +00:00
|
|
|
'--strategy', 'DefaultStrategy',
|
2018-02-09 07:35:38 +00:00
|
|
|
'backtesting'
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
|
|
|
start(args)
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-02-09 07:35:38 +00:00
|
|
|
'Starting freqtrade in Backtesting mode',
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
assert start_mock.call_count == 1
|
|
|
|
|
|
|
|
|
2018-04-07 21:00:07 +00:00
|
|
|
def test_backtesting_init(mocker, default_conf) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
|
|
|
Test Backtesting._init() method
|
|
|
|
"""
|
2018-04-07 21:00:07 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
2018-02-09 07:35:38 +00:00
|
|
|
backtesting = Backtesting(default_conf)
|
|
|
|
assert backtesting.config == default_conf
|
|
|
|
assert isinstance(backtesting.analyze, Analyze)
|
2018-03-25 20:03:26 +00:00
|
|
|
assert backtesting.ticker_interval == '5m'
|
2018-02-09 07:35:38 +00:00
|
|
|
assert callable(backtesting.tickerdata_to_dataframe)
|
|
|
|
assert callable(backtesting.populate_buy_trend)
|
|
|
|
assert callable(backtesting.populate_sell_trend)
|
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_tickerdata_to_dataframe(default_conf, mocker) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
"""
|
|
|
|
Test Backtesting.tickerdata_to_dataframe() method
|
|
|
|
"""
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
2018-06-05 21:53:49 +00:00
|
|
|
timerange = TimeRange(None, 'line', 0, -100)
|
2018-03-25 20:03:26 +00:00
|
|
|
tick = optimize.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
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
backtesting = Backtesting(default_conf)
|
2018-03-02 13:46:32 +00:00
|
|
|
data = backtesting.tickerdata_to_dataframe(tickerlist)
|
2018-06-07 09:56:39 +00:00
|
|
|
assert len(data['UNITTEST/BTC']) == 99
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
# Load Analyze to compare the result between Backtesting function and Analyze are the same
|
|
|
|
analyze = Analyze(default_conf)
|
|
|
|
data2 = analyze.tickerdata_to_dataframe(tickerlist)
|
2018-02-03 16:15:40 +00:00
|
|
|
assert data['UNITTEST/BTC'].equals(data2['UNITTEST/BTC'])
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_get_timeframe(default_conf, mocker) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
|
|
|
Test Backtesting.get_timeframe() method
|
|
|
|
"""
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtesting = Backtesting(default_conf)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
data = backtesting.tickerdata_to_dataframe(
|
|
|
|
optimize.load_data(
|
|
|
|
None,
|
2018-03-25 20:03:26 +00:00
|
|
|
ticker_interval='1m',
|
2018-02-03 16:15:40 +00:00
|
|
|
pairs=['UNITTEST/BTC']
|
2018-02-09 07:35:38 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
min_date, max_date = backtesting.get_timeframe(data)
|
|
|
|
assert min_date.isoformat() == '2017-11-04T23:02:00+00:00'
|
2018-06-07 09:56:39 +00:00
|
|
|
assert max_date.isoformat() == '2017-11-14T22:58:00+00:00'
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_generate_text_table(default_conf, mocker):
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
|
|
|
Test Backtesting.generate_text_table() method
|
|
|
|
"""
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtesting = Backtesting(default_conf)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
results = pd.DataFrame(
|
|
|
|
{
|
2018-02-03 16:15:40 +00:00
|
|
|
'currency': ['ETH/BTC', 'ETH/BTC'],
|
2018-02-09 07:35:38 +00:00
|
|
|
'profit_percent': [0.1, 0.2],
|
|
|
|
'profit_BTC': [0.2, 0.4],
|
|
|
|
'duration': [10, 30],
|
|
|
|
'profit': [2, 0],
|
|
|
|
'loss': [0, 0]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
result_str = (
|
2018-06-02 18:06:29 +00:00
|
|
|
'| pair | buy count | avg profit % | '
|
|
|
|
'total profit BTC | avg duration | profit | loss |\n'
|
|
|
|
'|:--------|------------:|---------------:|'
|
|
|
|
'-------------------:|---------------:|---------:|-------:|\n'
|
|
|
|
'| ETH/BTC | 2 | 15.00 | '
|
|
|
|
'0.60000000 | 20.0 | 2 | 0 |\n'
|
|
|
|
'| TOTAL | 2 | 15.00 | '
|
|
|
|
'0.60000000 | 20.0 | 2 | 0 |'
|
2018-02-09 07:35:38 +00:00
|
|
|
)
|
2018-02-03 16:15:40 +00:00
|
|
|
assert backtesting._generate_text_table(data={'ETH/BTC': {}}, results=results) == result_str
|
2017-12-28 14:58:02 +00:00
|
|
|
|
2017-12-28 19:05:33 +00:00
|
|
|
|
2018-02-09 07:35:38 +00:00
|
|
|
def test_backtesting_start(default_conf, mocker, caplog) -> None:
|
|
|
|
"""
|
|
|
|
Test Backtesting.start() method
|
|
|
|
"""
|
2018-04-09 18:21:54 +00:00
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
def get_timeframe(input1, input2):
|
|
|
|
return Arrow(2017, 11, 14, 21, 17), Arrow(2017, 11, 14, 22, 59)
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.freqtradebot.Analyze', MagicMock())
|
|
|
|
mocker.patch('freqtrade.optimize.load_data', mocked_load_data)
|
|
|
|
mocker.patch('freqtrade.exchange.get_ticker_history')
|
2018-04-07 21:00:07 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
2018-03-02 13:46:32 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.optimize.backtesting.Backtesting',
|
|
|
|
backtest=MagicMock(),
|
|
|
|
_generate_text_table=MagicMock(return_value='1'),
|
|
|
|
get_timeframe=get_timeframe,
|
|
|
|
)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
conf = deepcopy(default_conf)
|
2018-02-03 16:15:40 +00:00
|
|
|
conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
|
2018-02-09 07:35:38 +00:00
|
|
|
conf['ticker_interval'] = 1
|
|
|
|
conf['live'] = False
|
|
|
|
conf['datadir'] = None
|
|
|
|
conf['export'] = None
|
|
|
|
conf['timerange'] = '-100'
|
|
|
|
|
|
|
|
backtesting = Backtesting(conf)
|
|
|
|
backtesting.start()
|
|
|
|
# check the logs, that will contain the backtest result
|
|
|
|
exists = [
|
|
|
|
'Using local backtesting data (using whitelist in given config) ...',
|
|
|
|
'Using stake_currency: BTC ...',
|
|
|
|
'Using stake_amount: 0.001 ...',
|
|
|
|
'Measuring data from 2017-11-14T21:17:00+00:00 '
|
|
|
|
'up to 2017-11-14T22:59:00+00:00 (0 days)..'
|
|
|
|
]
|
|
|
|
for line in exists:
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(line, caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
|
2018-06-11 17:50:43 +00:00
|
|
|
def test_backtesting_start_no_data(default_conf, mocker, caplog) -> None:
|
|
|
|
"""
|
|
|
|
Test Backtesting.start() method if no data is found
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_timeframe(input1, input2):
|
|
|
|
return Arrow(2017, 11, 14, 21, 17), Arrow(2017, 11, 14, 22, 59)
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.freqtradebot.Analyze', MagicMock())
|
|
|
|
mocker.patch('freqtrade.optimize.load_data', MagicMock(return_value={}))
|
|
|
|
mocker.patch('freqtrade.exchange.get_ticker_history')
|
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.optimize.backtesting.Backtesting',
|
|
|
|
backtest=MagicMock(),
|
|
|
|
_generate_text_table=MagicMock(return_value='1'),
|
|
|
|
get_timeframe=get_timeframe,
|
|
|
|
)
|
|
|
|
|
|
|
|
conf = deepcopy(default_conf)
|
|
|
|
conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
|
2018-06-11 19:02:24 +00:00
|
|
|
conf['ticker_interval'] = "1m"
|
2018-06-11 17:50:43 +00:00
|
|
|
conf['live'] = False
|
|
|
|
conf['datadir'] = None
|
|
|
|
conf['export'] = None
|
|
|
|
conf['timerange'] = '20180101-20180102'
|
|
|
|
|
|
|
|
backtesting = Backtesting(conf)
|
|
|
|
backtesting.start()
|
|
|
|
# check the logs, that will contain the backtest result
|
2018-06-11 19:02:24 +00:00
|
|
|
|
|
|
|
assert log_has('No data found. Terminating.', caplog.record_tuples)
|
2018-06-11 17:50:43 +00:00
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_backtest(default_conf, fee, mocker) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
|
|
|
Test Backtesting.backtest() method
|
|
|
|
"""
|
2018-04-21 17:39:18 +00:00
|
|
|
mocker.patch('freqtrade.exchange.get_fee', fee)
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtesting = Backtesting(default_conf)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
2018-03-25 20:03:26 +00:00
|
|
|
data = optimize.load_data(None, ticker_interval='5m', pairs=['UNITTEST/BTC'])
|
2018-01-17 17:19:39 +00:00
|
|
|
data = trim_dictlist(data, -200)
|
2018-02-09 07:35:38 +00:00
|
|
|
results = backtesting.backtest(
|
|
|
|
{
|
|
|
|
'stake_amount': default_conf['stake_amount'],
|
|
|
|
'processed': backtesting.tickerdata_to_dataframe(data),
|
|
|
|
'max_open_trades': 10,
|
|
|
|
'realistic': True
|
|
|
|
}
|
|
|
|
)
|
2017-12-30 10:55:23 +00:00
|
|
|
assert not results.empty
|
2017-12-28 14:58:02 +00:00
|
|
|
|
2017-12-28 19:05:33 +00:00
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_backtest_1min_ticker_interval(default_conf, fee, mocker) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
|
|
|
Test Backtesting.backtest() method with 1 min ticker
|
|
|
|
"""
|
2018-04-21 17:39:18 +00:00
|
|
|
mocker.patch('freqtrade.exchange.get_fee', fee)
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtesting = Backtesting(default_conf)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
# Run a backtesting for an exiting 5min ticker_interval
|
2018-03-25 20:03:26 +00:00
|
|
|
data = optimize.load_data(None, ticker_interval='1m', pairs=['UNITTEST/BTC'])
|
2018-02-09 07:35:38 +00:00
|
|
|
data = trim_dictlist(data, -200)
|
|
|
|
results = backtesting.backtest(
|
|
|
|
{
|
|
|
|
'stake_amount': default_conf['stake_amount'],
|
|
|
|
'processed': backtesting.tickerdata_to_dataframe(data),
|
|
|
|
'max_open_trades': 1,
|
|
|
|
'realistic': True
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert not results.empty
|
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_processed(default_conf, mocker) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
|
|
|
Test Backtesting.backtest() method with offline data
|
|
|
|
"""
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtesting = Backtesting(default_conf)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
2017-12-30 10:55:23 +00:00
|
|
|
dict_of_tickerrows = load_data_test('raise')
|
2018-02-09 07:35:38 +00:00
|
|
|
dataframes = backtesting.tickerdata_to_dataframe(dict_of_tickerrows)
|
2018-02-03 16:15:40 +00:00
|
|
|
dataframe = dataframes['UNITTEST/BTC']
|
2017-12-30 10:55:23 +00:00
|
|
|
cols = dataframe.columns
|
|
|
|
# assert the dataframe got some of the indicator columns
|
|
|
|
for col in ['close', 'high', 'low', 'open', 'date',
|
|
|
|
'ema50', 'ao', 'macd', 'plus_dm']:
|
|
|
|
assert col in cols
|
2017-12-28 19:05:33 +00:00
|
|
|
|
2017-12-28 14:58:02 +00:00
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_backtest_pricecontours(default_conf, fee, mocker) -> None:
|
2018-04-15 10:09:12 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.exchange.get_fee', fee)
|
2018-06-07 09:56:39 +00:00
|
|
|
tests = [['raise', 17], ['lower', 0], ['sine', 16]]
|
2017-12-28 14:58:02 +00:00
|
|
|
for [contour, numres] in tests:
|
2018-04-21 22:19:11 +00:00
|
|
|
simple_backtest(default_conf, contour, numres, mocker)
|
2017-12-28 14:58:02 +00:00
|
|
|
|
2017-12-28 19:05:33 +00:00
|
|
|
|
2018-03-04 00:42:37 +00:00
|
|
|
# Test backtest using offline data (testdata directory)
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_backtest_ticks(default_conf, fee, mocker):
|
2018-04-21 17:39:18 +00:00
|
|
|
mocker.patch('freqtrade.exchange.get_fee', fee)
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
2018-02-08 19:49:43 +00:00
|
|
|
ticks = [1, 5]
|
2018-04-21 22:19:11 +00:00
|
|
|
fun = Backtesting(default_conf).populate_buy_trend
|
2018-04-21 17:39:18 +00:00
|
|
|
for _ in ticks:
|
2018-04-21 22:19:11 +00:00
|
|
|
backtest_conf = _make_backtest_conf(mocker, conf=default_conf)
|
|
|
|
backtesting = Backtesting(default_conf)
|
|
|
|
backtesting.populate_buy_trend = fun # Override
|
|
|
|
backtesting.populate_sell_trend = fun # Override
|
|
|
|
results = backtesting.backtest(backtest_conf)
|
2018-02-08 19:49:43 +00:00
|
|
|
assert not results.empty
|
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_backtest_clash_buy_sell(mocker, default_conf):
|
2018-02-08 19:49:43 +00:00
|
|
|
# Override the default buy trend function in our default_strategy
|
|
|
|
def fun(dataframe=None):
|
|
|
|
buy_value = 1
|
|
|
|
sell_value = 1
|
|
|
|
return _trend(dataframe, buy_value, sell_value)
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtest_conf = _make_backtest_conf(mocker, conf=default_conf)
|
|
|
|
backtesting = Backtesting(default_conf)
|
|
|
|
backtesting.populate_buy_trend = fun # Override
|
|
|
|
backtesting.populate_sell_trend = fun # Override
|
|
|
|
results = backtesting.backtest(backtest_conf)
|
2018-02-08 19:49:43 +00:00
|
|
|
assert results.empty
|
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_backtest_only_sell(mocker, default_conf):
|
2018-02-08 19:49:43 +00:00
|
|
|
# Override the default buy trend function in our default_strategy
|
|
|
|
def fun(dataframe=None):
|
|
|
|
buy_value = 0
|
|
|
|
sell_value = 1
|
|
|
|
return _trend(dataframe, buy_value, sell_value)
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtest_conf = _make_backtest_conf(mocker, conf=default_conf)
|
|
|
|
backtesting = Backtesting(default_conf)
|
|
|
|
backtesting.populate_buy_trend = fun # Override
|
|
|
|
backtesting.populate_sell_trend = fun # Override
|
|
|
|
results = backtesting.backtest(backtest_conf)
|
2018-02-08 19:49:43 +00:00
|
|
|
assert results.empty
|
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_backtest_alternate_buy_sell(default_conf, fee, mocker):
|
2018-04-15 10:09:12 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.exchange.get_fee', fee)
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
backtest_conf = _make_backtest_conf(mocker, conf=default_conf, pair='UNITTEST/BTC')
|
|
|
|
backtesting = Backtesting(default_conf)
|
|
|
|
backtesting.populate_buy_trend = _trend_alternate # Override
|
|
|
|
backtesting.populate_sell_trend = _trend_alternate # Override
|
|
|
|
results = backtesting.backtest(backtest_conf)
|
2018-02-08 19:49:43 +00:00
|
|
|
assert len(results) == 3
|
|
|
|
|
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_backtest_record(default_conf, fee, mocker):
|
2018-02-08 19:49:43 +00:00
|
|
|
names = []
|
|
|
|
records = []
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
2018-04-15 10:09:12 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.exchange.get_fee', fee)
|
2018-03-04 00:42:37 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.optimize.backtesting.file_dump_json',
|
|
|
|
new=lambda n, r: (names.append(n), records.append(r))
|
|
|
|
)
|
2018-02-08 19:49:43 +00:00
|
|
|
backtest_conf = _make_backtest_conf(
|
2018-04-21 22:19:11 +00:00
|
|
|
mocker,
|
2018-02-08 19:49:43 +00:00
|
|
|
conf=default_conf,
|
2018-02-03 16:15:40 +00:00
|
|
|
pair='UNITTEST/BTC',
|
2018-02-08 19:49:43 +00:00
|
|
|
record="trades"
|
|
|
|
)
|
2018-04-21 22:19:11 +00:00
|
|
|
backtesting = Backtesting(default_conf)
|
|
|
|
backtesting.populate_buy_trend = _trend_alternate # Override
|
|
|
|
backtesting.populate_sell_trend = _trend_alternate # Override
|
|
|
|
results = backtesting.backtest(backtest_conf)
|
2018-02-08 19:49:43 +00:00
|
|
|
assert len(results) == 3
|
|
|
|
# Assert file_dump_json was only called once
|
|
|
|
assert names == ['backtest-result.json']
|
|
|
|
records = records[0]
|
|
|
|
# Ensure records are of correct type
|
|
|
|
assert len(records) == 3
|
2018-02-03 16:15:40 +00:00
|
|
|
# ('UNITTEST/BTC', 0.00331158, '1510684320', '1510691700', 0, 117)
|
2018-02-08 19:49:43 +00:00
|
|
|
# Below follows just a typecheck of the schema/type of trade-records
|
|
|
|
oix = None
|
|
|
|
for (pair, profit, date_buy, date_sell, buy_index, dur) in records:
|
2018-02-03 16:15:40 +00:00
|
|
|
assert pair == 'UNITTEST/BTC'
|
2018-02-08 19:49:43 +00:00
|
|
|
isinstance(profit, float)
|
|
|
|
# FIX: buy/sell should be converted to ints
|
|
|
|
isinstance(date_buy, str)
|
|
|
|
isinstance(date_sell, str)
|
|
|
|
isinstance(buy_index, pd._libs.tslib.Timestamp)
|
|
|
|
if oix:
|
|
|
|
assert buy_index > oix
|
|
|
|
oix = buy_index
|
|
|
|
assert dur > 0
|
2017-12-28 14:58:02 +00:00
|
|
|
|
2017-12-28 19:05:33 +00:00
|
|
|
|
2018-04-21 22:19:11 +00:00
|
|
|
def test_backtest_start_live(default_conf, mocker, caplog):
|
2018-04-09 19:15:47 +00:00
|
|
|
conf = deepcopy(default_conf)
|
|
|
|
conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
|
2018-02-08 19:49:43 +00:00
|
|
|
mocker.patch('freqtrade.exchange.get_ticker_history',
|
|
|
|
new=lambda n, i: _load_pair_as_ticks(n, i))
|
2018-04-09 19:15:47 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock())
|
2018-03-04 00:42:37 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest', MagicMock())
|
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting._generate_text_table', MagicMock())
|
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
2018-04-09 19:15:47 +00:00
|
|
|
read_data=json.dumps(conf)
|
2018-03-04 00:42:37 +00:00
|
|
|
))
|
|
|
|
|
2018-02-08 19:49:43 +00:00
|
|
|
args = MagicMock()
|
|
|
|
args.ticker_interval = 1
|
|
|
|
args.level = 10
|
|
|
|
args.live = True
|
|
|
|
args.datadir = None
|
|
|
|
args.export = None
|
2018-03-24 19:44:04 +00:00
|
|
|
args.strategy = 'DefaultStrategy'
|
2018-02-08 19:49:43 +00:00
|
|
|
args.timerange = '-100' # needed due to MagicMock malleability
|
2018-03-04 00:42:37 +00:00
|
|
|
|
|
|
|
args = [
|
|
|
|
'--config', 'config.json',
|
2018-03-24 19:44:04 +00:00
|
|
|
'--strategy', 'DefaultStrategy',
|
2018-03-04 00:42:37 +00:00
|
|
|
'backtesting',
|
2018-03-25 20:03:26 +00:00
|
|
|
'--ticker-interval', '1m',
|
2018-03-04 00:42:37 +00:00
|
|
|
'--live',
|
|
|
|
'--timerange', '-100'
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
|
|
|
start(args)
|
2018-02-08 19:49:43 +00:00
|
|
|
# check the logs, that will contain the backtest result
|
2018-03-04 00:42:37 +00:00
|
|
|
exists = [
|
|
|
|
'Parameter -i/--ticker-interval detected ...',
|
2018-03-25 20:03:26 +00:00
|
|
|
'Using ticker_interval: 1m ...',
|
2018-03-04 00:42:37 +00:00
|
|
|
'Parameter -l/--live detected ...',
|
|
|
|
'Using max_open_trades: 1 ...',
|
|
|
|
'Parameter --timerange detected: -100 ..',
|
2018-06-02 08:32:05 +00:00
|
|
|
'Using data folder: freqtrade/tests/testdata ...',
|
2018-03-04 00:42:37 +00:00
|
|
|
'Using stake_currency: BTC ...',
|
|
|
|
'Using stake_amount: 0.001 ...',
|
|
|
|
'Downloading data for all pairs in whitelist ...',
|
|
|
|
'Measuring data from 2017-11-14T19:32:00+00:00 up to 2017-11-14T22:59:00+00:00 (0 days)..'
|
|
|
|
]
|
|
|
|
|
2018-02-08 19:49:43 +00:00
|
|
|
for line in exists:
|
2018-03-04 10:06:40 +00:00
|
|
|
log_has(line, caplog.record_tuples)
|