Simplify functional tests

This commit is contained in:
Matthias 2018-10-29 20:17:15 +01:00
parent 98050ff594
commit 6096f3ca47

View File

@ -1,11 +1,11 @@
# pragma pylint: disable=missing-docstring, W0212, line-too-long, C0103, unused-argument # pragma pylint: disable=missing-docstring, W0212, line-too-long, C0103, unused-argument
import logging import logging
from unittest.mock import MagicMock from unittest.mock import MagicMock
from typing import NamedTuple from typing import NamedTuple, List
from pandas import DataFrame from pandas import DataFrame
import pytest import pytest
from arrow import get as getdate import arrow
from freqtrade.optimize.backtesting import Backtesting from freqtrade.optimize.backtesting import Backtesting
@ -13,11 +13,15 @@ from freqtrade.strategy.interface import SellType
from freqtrade.tests.conftest import patch_exchange, log_has from freqtrade.tests.conftest import patch_exchange, log_has
ticker_start_time = arrow.get(2018, 10, 3)
ticker_interval_in_minute = 60
class BTContainer(NamedTuple): class BTContainer(NamedTuple):
""" """
NamedTuple Defining BacktestResults inputs. NamedTuple Defining BacktestResults inputs.
""" """
data: DataFrame data: List[float]
stop_loss: float stop_loss: float
roi: float roi: float
trades: int trades: int
@ -25,19 +29,24 @@ class BTContainer(NamedTuple):
sell_r: SellType sell_r: SellType
columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'buy', 'sell'] def _build_dataframe(ticker_with_signals):
data_profit = DataFrame([ columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'buy', 'sell']
[getdate('2018-07-08 18:00:00').datetime, 0.0009910,
0.001011, 0.00098618, 0.001000, 12345, 1, 0], frame = DataFrame.from_records(ticker_with_signals, columns=columns)
[getdate('2018-07-08 19:00:00').datetime, 0.001000, frame['date'] = frame['date'].apply(lambda x: ticker_start_time.shift(
0.001010, 0.0009900, 0.0009900, 12345, 0, 0], minutes=(x * ticker_interval_in_minute)).datetime)
[getdate('2018-07-08 20:00:00').datetime, 0.0009900, # Ensure floats are in place
0.001011, 0.00091618, 0.0009900, 12345, 0, 0], for column in ['open', 'high', 'low', 'close', 'volume']:
[getdate('2018-07-08 21:00:00').datetime, 0.001000, frame[column] = frame[column].astype('float64')
0.001011, 0.00098618, 0.001100, 12345, 0, 1], return frame
[getdate('2018-07-08 22:00:00').datetime, 0.001000,
0.001011, 0.00098618, 0.0009900, 12345, 0, 0]
], columns=columns) data_profit = [
[0, 0.0009910, 0.001011, 0.00098618, 0.001000, 12345, 1, 0],
[1, 0.001000, 0.001010, 0.0009900, 0.0009900, 12345, 0, 0],
[2, 0.0009900, 0.001011, 0.00091618, 0.0009900, 12345, 0, 0],
[3, 0.001000, 0.001011, 0.00098618, 0.001100, 12345, 0, 1],
[4, 0.001000, 0.001011, 0.00098618, 0.0009900, 12345, 0, 0]]
tc_profit1 = BTContainer(data=data_profit, stop_loss=-0.01, roi=1, trades=1, tc_profit1 = BTContainer(data=data_profit, stop_loss=-0.01, roi=1, trades=1,
profit_perc=0.10557, sell_r=SellType.STOP_LOSS) # should be stoploss - drops 8% profit_perc=0.10557, sell_r=SellType.STOP_LOSS) # should be stoploss - drops 8%
@ -45,18 +54,12 @@ tc_profit2 = BTContainer(data=data_profit, stop_loss=-0.10, roi=1,
trades=1, profit_perc=0.10557, sell_r=SellType.STOP_LOSS) trades=1, profit_perc=0.10557, sell_r=SellType.STOP_LOSS)
tc_loss0 = BTContainer(data=DataFrame([ tc_loss0 = BTContainer(data=[
[getdate('2018-07-08 18:00:00').datetime, 0.0009910, [0, 0.0009910, 0.001011, 0.00098618, 0.001000, 12345, 1, 0],
0.001011, 0.00098618, 0.001000, 12345, 1, 0], [1, 0.001000, 0.001010, 0.0009900, 0.001000, 12345, 0, 0],
[getdate('2018-07-08 19:00:00').datetime, 0.001000, [2, 0.001000, 0.001011, 0.0010618, 0.00091618, 12345, 0, 0],
0.001010, 0.0009900, 0.001000, 12345, 0, 0], [3, 0.001000, 0.001011, 0.00098618, 0.00091618, 12345, 0, 0],
[getdate('2018-07-08 20:00:00').datetime, 0.001000, [4, 0.001000, 0.001011, 0.00098618, 0.00091618, 12345, 0, 0]],
0.001011, 0.0010618, 0.00091618, 12345, 0, 0],
[getdate('2018-07-08 21:00:00').datetime, 0.001000,
0.001011, 0.00098618, 0.00091618, 12345, 0, 0],
[getdate('2018-07-08 22:00:00').datetime, 0.001000,
0.001011, 0.00098618, 0.00091618, 12345, 0, 0]
], columns=columns),
stop_loss=-0.05, roi=1, trades=1, profit_perc=-0.08839, sell_r=SellType.STOP_LOSS) stop_loss=-0.05, roi=1, trades=1, profit_perc=-0.08839, sell_r=SellType.STOP_LOSS)
@ -64,30 +67,27 @@ tc_loss0 = BTContainer(data=DataFrame([
# Candle Data for test 1 close at -8% (9200) # Candle Data for test 1 close at -8% (9200)
# Test with Stop-loss at 1% # Test with Stop-loss at 1%
# TC1: Stop-Loss Triggered 1% loss # TC1: Stop-Loss Triggered 1% loss
tc1 = BTContainer(data=DataFrame([ tc1 = BTContainer(data=[
[getdate('2018-06-10 07:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 1, 0], [0, 10000.0, 10050, 9950, 9975, 12345, 1, 0],
[getdate('2018-06-10 08:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 0, 0], [1, 10000, 10050, 9950, 9975, 12345, 0, 0],
[getdate('2018-06-10 09:00:00').datetime, 9975, 10025, 9200, 9200, 12345, 0, 0], [2, 9975, 10025, 9200, 9200, 12345, 0, 0],
[getdate('2018-06-10 10:00:00').datetime, 9950, 10000, 9960, 9955, 12345, 0, 0], [3, 9950, 10000, 9960, 9955, 12345, 0, 0],
[getdate('2018-06-10 11:00:00').datetime, 9955, 9975, 9955, 9990, 12345, 0, 0], [4, 9955, 9975, 9955, 9990, 12345, 0, 0],
[getdate('2018-06-10 12:00:00').datetime, 9990, 9990, 9990, 9900, 12345, 0, 0] [5, 9990, 9990, 9990, 9900, 12345, 0, 0]],
], columns=columns), stop_loss=-0.01, roi=1, trades=1, profit_perc=-0.01, sell_r=SellType.STOP_LOSS)
stop_loss=-0.01, roi=1, trades=1, profit_perc=-0.01, sell_r=SellType.STOP_LOSS) # should be
# stop_loss=-0.01, roi=1, trades=1, profit_perc=-0.003, sell_r=SellType.FORCE_SELL) #
# Test 2 Minus 4% Low, minus 1% close # Test 2 Minus 4% Low, minus 1% close
# Candle Data for test 2 # Candle Data for test 2
# Test with Stop-Loss at 3% # Test with Stop-Loss at 3%
# TC2: Stop-Loss Triggered 3% Loss # TC2: Stop-Loss Triggered 3% Loss
tc2 = BTContainer(data=DataFrame([ tc2 = BTContainer(data=[
[getdate('2018-06-10 07:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 1, 0], [0, 10000, 10050, 9950, 9975, 12345, 1, 0],
[getdate('2018-06-10 08:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 0, 0], [1, 10000, 10050, 9950, 9975, 12345, 0, 0],
[getdate('2018-06-10 09:00:00').datetime, 9975, 10025, 9925, 9950, 12345, 0, 0], [2, 9975, 10025, 9925, 9950, 12345, 0, 0],
[getdate('2018-06-10 10:00:00').datetime, 9950, 10000, 9600, 9925, 12345, 0, 0], [3, 9950, 10000, 9600, 9925, 12345, 0, 0],
[getdate('2018-06-10 11:00:00').datetime, 9925, 9975, 9875, 9900, 12345, 0, 0], [4, 9925, 9975, 9875, 9900, 12345, 0, 0],
[getdate('2018-06-10 12:00:00').datetime, 9900, 9950, 9850, 9900, 12345, 0, 0] [5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
], columns=columns),
stop_loss=-0.03, roi=1, trades=1, profit_perc=-0.03, sell_r=SellType.STOP_LOSS) #should be stop_loss=-0.03, roi=1, trades=1, profit_perc=-0.03, sell_r=SellType.STOP_LOSS) #should be
# stop_loss=-0.03, roi=1, trades=1, profit_perc=-0.007, sell_r=SellType.FORCE_SELL) # # stop_loss=-0.03, roi=1, trades=1, profit_perc=-0.007, sell_r=SellType.FORCE_SELL) #
@ -99,15 +99,14 @@ tc2 = BTContainer(data=DataFrame([
# Test with Stop-Loss at 2% # Test with Stop-Loss at 2%
# TC3: Trade-A: Stop-Loss Triggered 2% Loss # TC3: Trade-A: Stop-Loss Triggered 2% Loss
# Trade-B: Stop-Loss Triggered 2% Loss # Trade-B: Stop-Loss Triggered 2% Loss
tc3 = BTContainer(data=DataFrame([ tc3 = BTContainer(data=[
[getdate('2018-06-10 07:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 1, 0], [0, 10000, 10050, 9950, 9975, 12345, 1, 0],
[getdate('2018-06-10 08:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 0, 0], [1, 10000, 10050, 9950, 9975, 12345, 0, 0],
[getdate('2018-06-10 09:00:00').datetime, 9975, 10025, 9600, 9950, 12345, 0, 0], [2, 9975, 10025, 9600, 9950, 12345, 0, 0],
[getdate('2018-06-10 10:00:00').datetime, 9950, 10000, 9900, 9925, 12345, 1, 0], [3, 9950, 10000, 9900, 9925, 12345, 1, 0],
[getdate('2018-06-10 11:00:00').datetime, 9950, 10000, 9900, 9925, 12345, 0, 0], [4, 9950, 10000, 9900, 9925, 12345, 0, 0],
[getdate('2018-06-10 12:00:00').datetime, 9925, 9975, 8000, 8000, 12345, 0, 0], [5, 9925, 9975, 8000, 8000, 12345, 0, 0],
[getdate('2018-06-10 13:00:00').datetime, 9900, 9950, 9950, 9900, 12345, 0, 0] [6, 9900, 9950, 9950, 9900, 12345, 0, 0]],
], columns=columns),
stop_loss=-0.02, roi=1, trades=2, profit_perc=-0.04, sell_r=SellType.STOP_LOSS) #should be stop_loss=-0.02, roi=1, trades=2, profit_perc=-0.04, sell_r=SellType.STOP_LOSS) #should be
# stop_loss=-0.02, roi=1, trades=1, profit_perc=-0.02, sell_r=SellType.STOP_LOSS) #should be # stop_loss=-0.02, roi=1, trades=1, profit_perc=-0.02, sell_r=SellType.STOP_LOSS) #should be
# stop_loss=-0.02, roi=1, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL) # # stop_loss=-0.02, roi=1, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL) #
@ -117,14 +116,13 @@ tc3 = BTContainer(data=DataFrame([
# Candle Data for test 4 Candle drops 3% Closed 15% up # Candle Data for test 4 Candle drops 3% Closed 15% up
# Test with Stop-loss at 2% ROI 6% # Test with Stop-loss at 2% ROI 6%
# TC4: Stop-Loss Triggered 2% Loss # TC4: Stop-Loss Triggered 2% Loss
tc4 = BTContainer(data=DataFrame([ tc4 = BTContainer(data=[
[getdate('2018-06-10 07:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 1, 0], [0, 10000, 10050, 9950, 9975, 12345, 1, 0],
[getdate('2018-06-10 08:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 0, 0], [1, 10000, 10050, 9950, 9975, 12345, 0, 0],
[getdate('2018-06-10 09:00:00').datetime, 9975, 11500, 9700, 11500, 12345, 0, 0], [2, 9975, 11500, 9700, 11500, 12345, 0, 0],
[getdate('2018-06-10 10:00:00').datetime, 9950, 10000, 9900, 9925, 12345, 0, 0], [3, 9950, 10000, 9900, 9925, 12345, 0, 0],
[getdate('2018-06-10 11:00:00').datetime, 9925, 9975, 9875, 9900, 12345, 0, 0], [4, 9925, 9975, 9875, 9900, 12345, 0, 0],
[getdate('2018-06-10 12:00:00').datetime, 9900, 9950, 9850, 9900, 12345, 0, 0] [5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
], columns=columns),
stop_loss=-0.02, roi=0.06, trades=1, profit_perc=-0.02, sell_r=SellType.STOP_LOSS) #should be stop_loss=-0.02, roi=0.06, trades=1, profit_perc=-0.02, sell_r=SellType.STOP_LOSS) #should be
# stop_loss=-0.02, roi=0.06, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL) # stop_loss=-0.02, roi=0.06, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL)
@ -132,14 +130,13 @@ tc4 = BTContainer(data=DataFrame([
# Candle Data for test 5 # Candle Data for test 5
# Set stop-loss at 1% ROI 3% # Set stop-loss at 1% ROI 3%
# TC5: ROI triggers 3% Gain # TC5: ROI triggers 3% Gain
tc5 = BTContainer(data=DataFrame([ tc5 = BTContainer(data=[
[getdate('2018-06-10 07:00:00').datetime, 10000, 10050, 9960, 9975, 12345, 1, 0], [0, 10000, 10050, 9960, 9975, 12345, 1, 0],
[getdate('2018-06-10 08:00:00').datetime, 10000, 10050, 9960, 9975, 12345, 0, 0], [1, 10000, 10050, 9960, 9975, 12345, 0, 0],
[getdate('2018-06-10 09:00:00').datetime, 9975, 10050, 9950, 9975, 12345, 0, 0], [2, 9975, 10050, 9950, 9975, 12345, 0, 0],
[getdate('2018-06-10 10:00:00').datetime, 9950, 12000, 9950, 12000, 12345, 0, 0], [3, 9950, 12000, 9950, 12000, 12345, 0, 0],
[getdate('2018-06-10 11:00:00').datetime, 9925, 9975, 9945, 9900, 12345, 0, 0], [4, 9925, 9975, 9945, 9900, 12345, 0, 0],
[getdate('2018-06-10 12:00:00').datetime, 9900, 9950, 9850, 9900, 12345, 0, 0] [5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
], columns=columns),
stop_loss=-0.01, roi=0.03, trades=1, profit_perc=0.03, sell_r=SellType.ROI) #should be stop_loss=-0.01, roi=0.03, trades=1, profit_perc=0.03, sell_r=SellType.ROI) #should be
# stop_loss=-0.01, roi=0.03, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL) # stop_loss=-0.01, roi=0.03, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL)
@ -147,14 +144,13 @@ tc5 = BTContainer(data=DataFrame([
# Candle Data for test 6 # Candle Data for test 6
# Set stop-loss at 2% ROI at 5% # Set stop-loss at 2% ROI at 5%
# TC6: Stop-Loss triggers 2% Loss # TC6: Stop-Loss triggers 2% Loss
tc6 = BTContainer(data=DataFrame([ tc6 = BTContainer(data=[
[getdate('2018-06-10 07:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 1, 0], [0, 10000, 10050, 9950, 9975, 12345, 1, 0],
[getdate('2018-06-10 08:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 0, 0], [1, 10000, 10050, 9950, 9975, 12345, 0, 0],
[getdate('2018-06-10 09:00:00').datetime, 9975, 10600, 9700, 10100, 12345, 0, 0], [2, 9975, 10600, 9700, 10100, 12345, 0, 0],
[getdate('2018-06-10 10:00:00').datetime, 9950, 10000, 9900, 9925, 12345, 0, 0], [3, 9950, 10000, 9900, 9925, 12345, 0, 0],
[getdate('2018-06-10 11:00:00').datetime, 9925, 9975, 9945, 9900, 12345, 0, 0], [4, 9925, 9975, 9945, 9900, 12345, 0, 0],
[getdate('2018-06-10 12:00:00').datetime, 9900, 9950, 9850, 9900, 12345, 0, 0] [5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
], columns=columns),
stop_loss=-0.02, roi=0.05, trades=1, profit_perc=-0.02, sell_r=SellType.STOP_LOSS) #should be stop_loss=-0.02, roi=0.05, trades=1, profit_perc=-0.02, sell_r=SellType.STOP_LOSS) #should be
# stop_loss=-0.02, roi=0.05, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL) # # stop_loss=-0.02, roi=0.05, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL) #
@ -162,14 +158,13 @@ tc6 = BTContainer(data=DataFrame([
# Candle Data for test 7 # Candle Data for test 7
# Set stop-loss at 2% ROI at 3% # Set stop-loss at 2% ROI at 3%
# TC7: ROI Triggers 3% Gain # TC7: ROI Triggers 3% Gain
tc7 = BTContainer(data=DataFrame([ tc7 = BTContainer(data=[
[getdate('2018-06-10 07:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 1, 0], [0, 10000, 10050, 9950, 9975, 12345, 1, 0],
[getdate('2018-06-10 08:00:00').datetime, 10000, 10050, 9950, 9975, 12345, 0, 0], [1, 10000, 10050, 9950, 9975, 12345, 0, 0],
[getdate('2018-06-10 09:00:00').datetime, 9975, 10600, 9900, 10100, 12345, 0, 0], [2, 9975, 10600, 9900, 10100, 12345, 0, 0],
[getdate('2018-06-10 10:00:00').datetime, 9950, 10000, 9900, 9925, 12345, 0, 0], [3, 9950, 10000, 9900, 9925, 12345, 0, 0],
[getdate('2018-06-10 11:00:00').datetime, 9925, 9975, 9945, 9900, 12345, 0, 0], [4, 9925, 9975, 9945, 9900, 12345, 0, 0],
[getdate('2018-06-10 12:00:00').datetime, 9900, 9950, 9850, 9900, 12345, 0, 0] [5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
], columns=columns),
stop_loss=-0.02, roi=0.03, trades=1, profit_perc=0.03, sell_r=SellType.ROI) #should be stop_loss=-0.02, roi=0.03, trades=1, profit_perc=0.03, sell_r=SellType.ROI) #should be
# stop_loss=-0.02, roi=0.03, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL) # # stop_loss=-0.02, roi=0.03, trades=1, profit_perc=-0.012, sell_r=SellType.FORCE_SELL) #
@ -198,10 +193,10 @@ def test_backtest_results(default_conf, fee, mocker, caplog, data) -> None:
# TODO: don't Mock fee to for now # TODO: don't Mock fee to for now
mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.0)) mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.0))
patch_exchange(mocker) patch_exchange(mocker)
frame = _build_dataframe(data.data)
backtesting = Backtesting(default_conf) backtesting = Backtesting(default_conf)
backtesting.advise_buy = lambda a, m: data.data backtesting.advise_buy = lambda a, m: frame
backtesting.advise_sell = lambda a, m: data.data backtesting.advise_sell = lambda a, m: frame
caplog.set_level(logging.DEBUG) caplog.set_level(logging.DEBUG)
pair = 'UNITTEST/BTC' pair = 'UNITTEST/BTC'