2018-01-28 07:38:41 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, W0212, line-too-long, C0103
|
2018-02-08 19:49:43 +00:00
|
|
|
import random
|
2018-01-05 09:23:12 +00:00
|
|
|
import logging
|
2017-12-28 14:58:02 +00:00
|
|
|
import math
|
2018-01-05 09:23:12 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-01-28 07:38:41 +00:00
|
|
|
import pandas as pd
|
2018-02-08 19:49:43 +00:00
|
|
|
import numpy as np
|
2017-11-25 00:04:11 +00:00
|
|
|
from freqtrade import exchange, optimize
|
2017-11-24 22:58:35 +00:00
|
|
|
from freqtrade.exchange import Bittrex
|
2018-01-02 19:32:11 +00:00
|
|
|
from freqtrade.optimize import preprocess
|
2017-12-26 06:05:49 +00:00
|
|
|
from freqtrade.optimize.backtesting import backtest, generate_text_table, get_timeframe
|
2018-01-05 09:23:12 +00:00
|
|
|
import freqtrade.optimize.backtesting as backtesting
|
2018-02-24 19:18:53 +00:00
|
|
|
from freqtrade.tests.conftest import log_has
|
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
|
|
|
|
|
|
|
|
|
2018-02-08 19:49:43 +00:00
|
|
|
# use for mock freqtrade.exchange.get_ticker_history'
|
|
|
|
def _load_pair_as_ticks(pair, tickfreq):
|
|
|
|
ticks = optimize.load_data(None, ticker_interval=8, pairs=[pair])
|
|
|
|
ticks = trim_dictlist(ticks, -200)
|
|
|
|
return ticks[pair]
|
|
|
|
|
|
|
|
|
|
|
|
# FIX: fixturize this?
|
|
|
|
def _make_backtest_conf(conf=None,
|
|
|
|
pair='BTC_UNITEST',
|
|
|
|
record=None):
|
|
|
|
data = optimize.load_data(None, ticker_interval=8, pairs=[pair])
|
|
|
|
data = trim_dictlist(data, -200)
|
|
|
|
return {'stake_amount': conf['stake_amount'],
|
|
|
|
'processed': optimize.preprocess(data),
|
|
|
|
'max_open_trades': 10,
|
|
|
|
'realistic': True,
|
|
|
|
'record': record}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def _run_backtest_1(strategy, fun, backtest_conf):
|
|
|
|
# strategy is a global (hidden as a singleton), so we
|
|
|
|
# emulate strategy being pure, by override/restore here
|
|
|
|
# if we dont do this, the override in strategy will carry over
|
|
|
|
# to other tests
|
|
|
|
old_buy = strategy.populate_buy_trend
|
|
|
|
old_sell = strategy.populate_sell_trend
|
|
|
|
strategy.populate_buy_trend = fun # Override
|
|
|
|
strategy.populate_sell_trend = fun # Override
|
|
|
|
results = backtest(backtest_conf)
|
|
|
|
strategy.populate_buy_trend = old_buy # restore override
|
|
|
|
strategy.populate_sell_trend = old_sell # restore override
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
2017-12-26 06:05:49 +00:00
|
|
|
def test_generate_text_table():
|
|
|
|
results = pd.DataFrame(
|
|
|
|
{
|
|
|
|
'currency': ['BTC_ETH', 'BTC_ETH'],
|
|
|
|
'profit_percent': [0.1, 0.2],
|
|
|
|
'profit_BTC': [0.2, 0.4],
|
2018-01-03 10:30:24 +00:00
|
|
|
'duration': [10, 30],
|
2018-01-03 10:35:54 +00:00
|
|
|
'profit': [2, 0],
|
|
|
|
'loss': [0, 0]
|
2017-12-26 06:05:49 +00:00
|
|
|
}
|
|
|
|
)
|
2018-02-05 16:09:09 +00:00
|
|
|
print(generate_text_table({'BTC_ETH': {}}, results, 'BTC'))
|
|
|
|
assert generate_text_table({'BTC_ETH': {}}, results, 'BTC') == (
|
2018-01-03 16:36:40 +00:00
|
|
|
'pair buy count avg profit % total profit BTC avg duration profit loss\n' # noqa
|
|
|
|
'------- ----------- -------------- ------------------ -------------- -------- ------\n' # noqa
|
2018-02-05 16:09:09 +00:00
|
|
|
'BTC_ETH 2 15.00 0.60000000 20.0 2 0\n' # noqa
|
|
|
|
'TOTAL 2 15.00 0.60000000 20.0 2 0') # noqa
|
2017-12-26 06:05:49 +00:00
|
|
|
|
|
|
|
|
2018-01-24 10:05:27 +00:00
|
|
|
def test_get_timeframe(default_strategy):
|
2018-01-03 16:36:40 +00:00
|
|
|
data = preprocess(optimize.load_data(
|
2018-01-06 22:24:35 +00:00
|
|
|
None, ticker_interval=1, pairs=['BTC_UNITEST']))
|
2017-12-26 06:05:49 +00:00
|
|
|
min_date, max_date = get_timeframe(data)
|
|
|
|
assert min_date.isoformat() == '2017-11-04T23:02:00+00:00'
|
|
|
|
assert max_date.isoformat() == '2017-11-14T22:59:00+00:00'
|
|
|
|
|
|
|
|
|
2018-01-24 10:05:27 +00:00
|
|
|
def test_backtest(default_strategy, default_conf, mocker):
|
2017-11-25 00:04:11 +00:00
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
2017-11-24 22:58:35 +00:00
|
|
|
exchange._API = Bittrex({'key': '', 'secret': ''})
|
|
|
|
|
2018-01-06 22:24:35 +00:00
|
|
|
data = optimize.load_data(None, ticker_interval=5, pairs=['BTC_ETH'])
|
2018-01-17 17:19:39 +00:00
|
|
|
data = trim_dictlist(data, -200)
|
2018-01-11 16:45:41 +00:00
|
|
|
results = backtest({'stake_amount': default_conf['stake_amount'],
|
|
|
|
'processed': optimize.preprocess(data),
|
|
|
|
'max_open_trades': 10,
|
|
|
|
'realistic': True})
|
2017-12-25 11:07:50 +00:00
|
|
|
assert not results.empty
|
2017-12-11 21:11:06 +00:00
|
|
|
|
|
|
|
|
2018-01-24 10:05:27 +00:00
|
|
|
def test_backtest_1min_ticker_interval(default_strategy, default_conf, mocker):
|
2017-12-11 21:11:06 +00:00
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
exchange._API = Bittrex({'key': '', 'secret': ''})
|
|
|
|
|
|
|
|
# Run a backtesting for an exiting 5min ticker_interval
|
2018-01-06 22:24:35 +00:00
|
|
|
data = optimize.load_data(None, ticker_interval=1, pairs=['BTC_UNITEST'])
|
2018-01-17 17:19:39 +00:00
|
|
|
data = trim_dictlist(data, -200)
|
2018-01-11 16:45:41 +00:00
|
|
|
results = backtest({'stake_amount': default_conf['stake_amount'],
|
|
|
|
'processed': optimize.preprocess(data),
|
|
|
|
'max_open_trades': 1,
|
|
|
|
'realistic': True})
|
2017-12-25 11:07:50 +00:00
|
|
|
assert not results.empty
|
2017-12-11 21:11:06 +00:00
|
|
|
|
2017-12-18 16:36:00 +00:00
|
|
|
|
2017-12-28 14:58:02 +00:00
|
|
|
def load_data_test(what):
|
2018-01-15 21:25:02 +00:00
|
|
|
timerange = ((None, 'line'), None, -100)
|
|
|
|
data = optimize.load_data(None, ticker_interval=1, pairs=['BTC_UNITEST'], timerange=timerange)
|
2017-12-28 14:58:02 +00:00
|
|
|
pair = data['BTC_UNITEST']
|
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:
|
2017-12-28 19:05:33 +00:00
|
|
|
# pair :: [{'O': 0.123, 'H': 0.123, 'L': 0.123,
|
|
|
|
# 'C': 0.123, 'V': 123.123,
|
|
|
|
# 'T': '2017-11-04T23:02:00', 'BV': 0.123}]
|
2017-12-30 10:55:23 +00:00
|
|
|
base = 0.001
|
2017-12-28 14:58:02 +00:00
|
|
|
if what == 'raise':
|
2017-12-30 10:55:23 +00:00
|
|
|
return {'BTC_UNITEST':
|
2018-01-03 16:36:40 +00:00
|
|
|
[{'T': pair[x]['T'], # Keep old dates
|
|
|
|
'V': pair[x]['V'], # Keep old volume
|
2017-12-30 10:55:23 +00:00
|
|
|
'BV': pair[x]['BV'], # keep too
|
2018-01-03 16:36:40 +00:00
|
|
|
'O': x * base, # But replace O,H,L,C
|
|
|
|
'H': x * base + 0.0001,
|
|
|
|
'L': x * base - 0.0001,
|
|
|
|
'C': x * base} for x in range(0, datalen)]}
|
2017-12-28 14:58:02 +00:00
|
|
|
if what == 'lower':
|
2017-12-30 10:55:23 +00:00
|
|
|
return {'BTC_UNITEST':
|
|
|
|
[{'T': pair[x]['T'], # Keep old dates
|
|
|
|
'V': pair[x]['V'], # Keep old volume
|
|
|
|
'BV': pair[x]['BV'], # keep too
|
|
|
|
'O': 1 - x * base, # But replace O,H,L,C
|
|
|
|
'H': 1 - x * base + 0.0001,
|
|
|
|
'L': 1 - x * base - 0.0001,
|
|
|
|
'C': 1 - x * base} 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
|
|
|
|
return {'BTC_UNITEST':
|
|
|
|
[{'T': pair[x]['T'], # Keep old dates
|
|
|
|
'V': pair[x]['V'], # Keep old volume
|
|
|
|
'BV': pair[x]['BV'], # keep too
|
2018-01-03 16:36:40 +00:00
|
|
|
# But replace O,H,L,C
|
|
|
|
'O': math.sin(x * hz) / 1000 + base,
|
|
|
|
'H': math.sin(x * hz) / 1000 + base + 0.0001,
|
|
|
|
'L': math.sin(x * hz) / 1000 + base - 0.0001,
|
|
|
|
'C': math.sin(x * hz) / 1000 + base} for x in range(0, datalen)]}
|
2017-12-28 14:58:02 +00:00
|
|
|
return data
|
|
|
|
|
2017-12-28 19:05:33 +00:00
|
|
|
|
2017-12-28 14:58:02 +00:00
|
|
|
def simple_backtest(config, contour, num_results):
|
|
|
|
data = load_data_test(contour)
|
|
|
|
processed = optimize.preprocess(data)
|
|
|
|
assert isinstance(processed, dict)
|
2018-01-11 16:45:41 +00:00
|
|
|
results = 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-02-08 19:49:43 +00:00
|
|
|
# Test backtest using offline data (testdata directory)
|
2017-12-28 14:58:02 +00:00
|
|
|
|
2017-12-28 19:05:33 +00:00
|
|
|
|
2018-02-08 19:49:43 +00:00
|
|
|
def test_backtest_ticks(default_conf, mocker, default_strategy):
|
2017-12-28 14:58:02 +00:00
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
2018-02-08 19:49:43 +00:00
|
|
|
ticks = [1, 5]
|
|
|
|
fun = default_strategy.populate_buy_trend
|
|
|
|
for tick in ticks:
|
|
|
|
backtest_conf = _make_backtest_conf(conf=default_conf)
|
|
|
|
results = _run_backtest_1(default_strategy, fun, backtest_conf)
|
|
|
|
assert not results.empty
|
|
|
|
|
|
|
|
|
|
|
|
def test_backtest_clash_buy_sell(default_conf, mocker, default_strategy):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
backtest_conf = _make_backtest_conf(conf=default_conf)
|
|
|
|
results = _run_backtest_1(default_strategy, fun, backtest_conf)
|
|
|
|
assert results.empty
|
|
|
|
|
|
|
|
|
|
|
|
def test_backtest_only_sell(default_conf, mocker, default_strategy):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
backtest_conf = _make_backtest_conf(conf=default_conf)
|
|
|
|
results = _run_backtest_1(default_strategy, fun, backtest_conf)
|
|
|
|
assert results.empty
|
|
|
|
|
|
|
|
|
|
|
|
def test_backtest_alternate_buy_sell(default_conf, mocker, default_strategy):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
backtest_conf = _make_backtest_conf(conf=default_conf, pair='BTC_UNITEST')
|
|
|
|
results = _run_backtest_1(default_strategy, _trend_alternate,
|
|
|
|
backtest_conf)
|
|
|
|
assert len(results) == 3
|
|
|
|
|
|
|
|
|
|
|
|
def test_backtest_record(default_conf, mocker, default_strategy):
|
|
|
|
names = []
|
|
|
|
records = []
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
mocker.patch('freqtrade.misc.file_dump_json',
|
|
|
|
new=lambda n, r: (names.append(n), records.append(r)))
|
|
|
|
backtest_conf = _make_backtest_conf(
|
|
|
|
conf=default_conf,
|
|
|
|
pair='BTC_UNITEST',
|
|
|
|
record="trades"
|
|
|
|
)
|
|
|
|
results = _run_backtest_1(default_strategy, _trend_alternate,
|
|
|
|
backtest_conf)
|
|
|
|
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
|
|
|
|
# ('BTC_UNITEST', 0.00331158, '1510684320', '1510691700', 0, 117)
|
|
|
|
# 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:
|
|
|
|
assert pair == 'BTC_UNITEST'
|
|
|
|
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-01-24 10:05:27 +00:00
|
|
|
def test_processed(default_conf, mocker, default_strategy):
|
2017-12-28 14:58:02 +00:00
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
2017-12-30 10:55:23 +00:00
|
|
|
dict_of_tickerrows = load_data_test('raise')
|
|
|
|
dataframes = optimize.preprocess(dict_of_tickerrows)
|
|
|
|
dataframe = dataframes['BTC_UNITEST']
|
|
|
|
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-01-24 10:05:27 +00:00
|
|
|
def test_backtest_pricecontours(default_conf, mocker, default_strategy):
|
2017-12-28 14:58:02 +00:00
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
2017-12-30 10:55:23 +00:00
|
|
|
tests = [['raise', 17], ['lower', 0], ['sine', 17]]
|
2017-12-28 14:58:02 +00:00
|
|
|
for [contour, numres] in tests:
|
|
|
|
simple_backtest(default_conf, contour, numres)
|
2017-12-30 10:55:23 +00:00
|
|
|
|
2018-01-05 09:23:12 +00:00
|
|
|
|
2018-01-15 21:25:02 +00:00
|
|
|
def mocked_load_data(datadir, pairs=[], ticker_interval=0, refresh_pairs=False, timerange=None):
|
|
|
|
tickerdata = optimize.load_tickerdata_file(datadir, 'BTC_UNITEST', 1, timerange=timerange)
|
2018-01-05 09:23:12 +00:00
|
|
|
pairdata = {'BTC_UNITEST': tickerdata}
|
2018-01-15 21:25:02 +00:00
|
|
|
return pairdata
|
2018-01-05 09:23:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_backtest_start(default_conf, mocker, caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2018-01-05 09:23:12 +00:00
|
|
|
default_conf['exchange']['pair_whitelist'] = ['BTC_UNITEST']
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
mocker.patch('freqtrade.misc.load_config', new=lambda s: default_conf)
|
|
|
|
mocker.patch.multiple('freqtrade.optimize',
|
|
|
|
load_data=mocked_load_data)
|
|
|
|
args = MagicMock()
|
|
|
|
args.ticker_interval = 1
|
|
|
|
args.level = 10
|
|
|
|
args.live = False
|
2018-01-06 22:24:35 +00:00
|
|
|
args.datadir = None
|
2018-01-11 14:45:39 +00:00
|
|
|
args.export = None
|
2018-01-15 21:25:02 +00:00
|
|
|
args.timerange = '-100' # needed due to MagicMock malleability
|
2018-01-05 09:23:12 +00:00
|
|
|
backtesting.start(args)
|
|
|
|
# check the logs, that will contain the backtest result
|
|
|
|
exists = ['Using max_open_trades: 1 ...',
|
|
|
|
'Using stake_amount: 0.001 ...',
|
2018-01-26 09:25:35 +00:00
|
|
|
'Measuring data from 2017-11-14T21:17:00+00:00 '
|
|
|
|
'up to 2017-11-14T22:59:00+00:00 (0 days)..']
|
2018-01-05 09:23:12 +00:00
|
|
|
for line in exists:
|
2018-02-24 19:18:53 +00:00
|
|
|
assert log_has(line, caplog.record_tuples)
|
2018-02-08 19:49:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_backtest_start_live(default_strategy, default_conf, mocker, caplog):
|
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
default_conf['exchange']['pair_whitelist'] = ['BTC_UNITEST']
|
|
|
|
mocker.patch('freqtrade.exchange.get_ticker_history',
|
|
|
|
new=lambda n, i: _load_pair_as_ticks(n, i))
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
|
|
|
mocker.patch('freqtrade.misc.load_config', new=lambda s: default_conf)
|
|
|
|
args = MagicMock()
|
|
|
|
args.ticker_interval = 1
|
|
|
|
args.level = 10
|
|
|
|
args.live = True
|
|
|
|
args.datadir = None
|
|
|
|
args.export = None
|
|
|
|
args.timerange = '-100' # needed due to MagicMock malleability
|
|
|
|
backtesting.start(args)
|
|
|
|
# check the logs, that will contain the backtest result
|
|
|
|
exists = ['Using max_open_trades: 1 ...',
|
|
|
|
'Using stake_amount: 0.001 ...',
|
|
|
|
'Measuring data from 2017-11-14T19:32:00+00:00 '
|
|
|
|
'up to 2017-11-14T22:59:00+00:00 (0 days)..']
|
|
|
|
for line in exists:
|
2018-02-24 19:18:53 +00:00
|
|
|
assert log_has(line, caplog.record_tuples)
|