tests backtesting cleanup and bugfix

This commit is contained in:
kryofly 2017-12-29 21:43:15 +01:00
parent d06a56d1f2
commit 2d2b424643

View File

@ -62,52 +62,41 @@ def load_data_test(what):
data = optimize.load_data(ticker_interval=1, pairs=['BTC_UNITEST']) data = optimize.load_data(ticker_interval=1, pairs=['BTC_UNITEST'])
data = trim_dictlist(data, -100) data = trim_dictlist(data, -100)
pair = data['BTC_UNITEST'] pair = data['BTC_UNITEST']
datalen = len(pair)
# Depending on the what parameter we now adjust the # Depending on the what parameter we now adjust the
# loaded data looks: # loaded data looks:
# pair :: [{'O': 0.123, 'H': 0.123, 'L': 0.123, # pair :: [{'O': 0.123, 'H': 0.123, 'L': 0.123,
# 'C': 0.123, 'V': 123.123, # 'C': 0.123, 'V': 123.123,
# 'T': '2017-11-04T23:02:00', 'BV': 0.123}] # 'T': '2017-11-04T23:02:00', 'BV': 0.123}]
o = 0.001 base = 0.001
h = o
ll = o
c = o
ll -= 0.0001
h += 0.0001
if what == 'raise': if what == 'raise':
for frame in pair: return {'BTC_UNITEST':
o += 0.0001 [{'T': pair[x]['T'], # Keep old dates
h += 0.0001 'V': pair[x]['V'], # Keep old volume
ll += 0.0001 'BV': pair[x]['BV'], # keep too
c += 0.0001 'O': x * base, # But replace O,H,L,C
# save prices rounded to satoshis 'H': x * base + 0.0001,
frame['O'] = round(o, 9) 'L': x * base - 0.0001,
frame['H'] = round(h, 9) 'C': x * base} for x in range(0,datalen)]}
frame['L'] = round(ll, 9)
frame['C'] = round(c, 9)
if what == 'lower': if what == 'lower':
for frame in pair: return {'BTC_UNITEST':
o -= 0.0001 [{'T': pair[x]['T'], # Keep old dates
h -= 0.0001 'V': pair[x]['V'], # Keep old volume
ll -= 0.0001 'BV': pair[x]['BV'], # keep too
c -= 0.0001 'O': 1 - x * base, # But replace O,H,L,C
# save prices rounded to satoshis 'H': 1 - x * base + 0.0001,
frame['O'] = round(o, 9) 'L': 1 - x * base - 0.0001,
frame['H'] = round(h, 9) 'C': 1 - x * base} for x in range(0,datalen)]}
frame['L'] = round(ll, 9)
frame['C'] = round(c, 9)
if what == 'sine': if what == 'sine':
i = 0 hz = 0.1 # frequency
for frame in pair: return {'BTC_UNITEST':
o = (2 + math.sin(i/10)) / 1000 [{'T': pair[x]['T'], # Keep old dates
h = (2 + math.sin(i/10)) / 1000 + 0.0001 'V': pair[x]['V'], # Keep old volume
ll = (2 + math.sin(i/10)) / 1000 - 0.0001 'BV': pair[x]['BV'], # keep too
c = (2 + math.sin(i/10)) / 1000 - 0.000001 'O': math.sin(x*hz) / 1000 + base, # But replace O,H,L,C
# save prices rounded to satoshis 'H': math.sin(x*hz) / 1000 + base + 0.0001,
frame['O'] = round(o, 9) 'L': math.sin(x*hz) / 1000 + base - 0.0001,
frame['H'] = round(h, 9) 'C': math.sin(x*hz) / 1000 + base} for x in range(0,datalen)]}
frame['L'] = round(ll, 9)
frame['C'] = round(c, 9)
i += 1
return data return data
@ -119,6 +108,7 @@ def simple_backtest(config, contour, num_results):
# results :: <class 'pandas.core.frame.DataFrame'> # results :: <class 'pandas.core.frame.DataFrame'>
assert len(results) == num_results assert len(results) == num_results
# Test backtest on offline data # Test backtest on offline data
# loaded by freqdata/optimize/__init__.py::load_data() # loaded by freqdata/optimize/__init__.py::load_data()
@ -127,16 +117,15 @@ def test_backtest2(default_conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch.dict('freqtrade.main._CONF', default_conf)
data = optimize.load_data(ticker_interval=5, pairs=['BTC_ETH']) data = optimize.load_data(ticker_interval=5, pairs=['BTC_ETH'])
results = backtest(default_conf['stake_amount'], optimize.preprocess(data), 10, True) results = backtest(default_conf['stake_amount'], optimize.preprocess(data), 10, True)
num_results = len(results) assert not results.empty
assert num_results > 0
def test_processed(default_conf, mocker): def test_processed(default_conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch.dict('freqtrade.main._CONF', default_conf)
data = load_data_test('raise') dict_of_tickerrows = load_data_test('raise')
x = optimize.preprocess(data) dataframes = optimize.preprocess(dict_of_tickerrows)
df = x['BTC_UNITEST'] dataframe = dataframes['BTC_UNITEST']
cols = df.columns cols = dataframe.columns
# assert the dataframe got some of the indicator columns # assert the dataframe got some of the indicator columns
for col in ['close', 'high', 'low', 'open', 'date', for col in ['close', 'high', 'low', 'open', 'date',
'ema50', 'ao', 'macd', 'plus_dm']: 'ema50', 'ao', 'macd', 'plus_dm']:
@ -145,6 +134,6 @@ def test_processed(default_conf, mocker):
def test_backtest_pricecontours(default_conf, mocker): def test_backtest_pricecontours(default_conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch.dict('freqtrade.main._CONF', default_conf)
tests = [['raise', 16], ['lower', 0], ['sine', 9]] tests = [['raise', 17], ['lower', 0], ['sine', 17]]
for [contour, numres] in tests: for [contour, numres] in tests:
simple_backtest(default_conf, contour, numres) simple_backtest(default_conf, contour, numres)