Merge branch 'develop' into sortino_hyperopt_loss
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from typing import Dict, List, NamedTuple
|
||||
from typing import Dict, List, NamedTuple, Optional
|
||||
|
||||
import arrow
|
||||
from pandas import DataFrame
|
||||
@@ -23,14 +23,14 @@ class BTContainer(NamedTuple):
|
||||
"""
|
||||
Minimal BacktestContainer defining Backtest inputs and results.
|
||||
"""
|
||||
data: List[float]
|
||||
data: List[List[float]]
|
||||
stop_loss: float
|
||||
roi: Dict[str, float]
|
||||
trades: List[BTrade]
|
||||
profit_perc: float
|
||||
trailing_stop: bool = False
|
||||
trailing_only_offset_is_reached: bool = False
|
||||
trailing_stop_positive: float = None
|
||||
trailing_stop_positive: Optional[float] = None
|
||||
trailing_stop_positive_offset: float = 0.0
|
||||
use_sell_signal: bool = False
|
||||
|
||||
|
@@ -364,7 +364,7 @@ def test_backtest_results(default_conf, fee, mocker, caplog, data) -> None:
|
||||
default_conf["trailing_stop"] = data.trailing_stop
|
||||
default_conf["trailing_only_offset_is_reached"] = data.trailing_only_offset_is_reached
|
||||
# Only add this to configuration If it's necessary
|
||||
if data.trailing_stop_positive:
|
||||
if data.trailing_stop_positive is not None:
|
||||
default_conf["trailing_stop_positive"] = data.trailing_stop_positive
|
||||
default_conf["trailing_stop_positive_offset"] = data.trailing_stop_positive_offset
|
||||
default_conf["ask_strategy"] = {"use_sell_signal": data.use_sell_signal}
|
||||
|
@@ -20,8 +20,8 @@ from freqtrade.data.dataprovider import DataProvider
|
||||
from freqtrade.data.history import get_timerange
|
||||
from freqtrade.exceptions import DependencyException, OperationalException
|
||||
from freqtrade.optimize.backtesting import Backtesting
|
||||
from freqtrade.resolvers import StrategyResolver
|
||||
from freqtrade.state import RunMode
|
||||
from freqtrade.strategy.default_strategy import DefaultStrategy
|
||||
from freqtrade.strategy.interface import SellType
|
||||
from tests.conftest import (get_args, log_has, log_has_re, patch_exchange,
|
||||
patched_configuration_load_config_file)
|
||||
@@ -287,8 +287,8 @@ def test_start(mocker, fee, default_conf, caplog) -> None:
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'DefaultStrategy',
|
||||
]
|
||||
args = get_args(args)
|
||||
start_backtesting(args)
|
||||
pargs = get_args(args)
|
||||
start_backtesting(pargs)
|
||||
assert log_has('Starting freqtrade in Backtesting mode', caplog)
|
||||
assert start_mock.call_count == 1
|
||||
|
||||
@@ -350,7 +350,9 @@ def test_tickerdata_to_dataframe_bt(default_conf, mocker, testdatadir) -> None:
|
||||
assert len(data['UNITTEST/BTC']) == 102
|
||||
|
||||
# Load strategy to compare the result between Backtesting function and strategy are the same
|
||||
strategy = DefaultStrategy(default_conf)
|
||||
default_conf.update({'strategy': 'DefaultStrategy'})
|
||||
strategy = StrategyResolver.load_strategy(default_conf)
|
||||
|
||||
data2 = strategy.tickerdata_to_dataframe(tickerlist)
|
||||
assert data['UNITTEST/BTC'].equals(data2['UNITTEST/BTC'])
|
||||
|
||||
|
@@ -82,8 +82,8 @@ def test_start(mocker, fee, edge_conf, caplog) -> None:
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'DefaultStrategy',
|
||||
]
|
||||
args = get_args(args)
|
||||
start_edge(args)
|
||||
pargs = get_args(args)
|
||||
start_edge(pargs)
|
||||
assert log_has('Starting freqtrade in Edge mode', caplog)
|
||||
assert start_mock.call_count == 1
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
import locale
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, List
|
||||
from unittest.mock import MagicMock, PropertyMock
|
||||
|
||||
import pandas as pd
|
||||
@@ -9,7 +10,8 @@ import pytest
|
||||
from arrow import Arrow
|
||||
from filelock import Timeout
|
||||
|
||||
from freqtrade.commands.optimize_commands import setup_optimize_configuration, start_hyperopt
|
||||
from freqtrade.commands.optimize_commands import (setup_optimize_configuration,
|
||||
start_hyperopt)
|
||||
from freqtrade.data.converter import parse_ticker_dataframe
|
||||
from freqtrade.data.history import load_tickerdata_file
|
||||
from freqtrade.exceptions import OperationalException
|
||||
@@ -54,7 +56,7 @@ def hyperopt_results():
|
||||
|
||||
|
||||
# Functions for recurrent object patching
|
||||
def create_trials(mocker, hyperopt, testdatadir) -> None:
|
||||
def create_trials(mocker, hyperopt, testdatadir) -> List[Dict]:
|
||||
"""
|
||||
When creating trials, mock the hyperopt Trials so that *by default*
|
||||
- we don't create any pickle'd files in the filesystem
|
||||
@@ -228,10 +230,10 @@ def test_start_not_installed(mocker, default_conf, caplog, import_fails) -> None
|
||||
'--hyperopt', 'DefaultHyperOpt',
|
||||
'--epochs', '5'
|
||||
]
|
||||
args = get_args(args)
|
||||
pargs = get_args(args)
|
||||
|
||||
with pytest.raises(OperationalException, match=r"Please ensure that the hyperopt dependencies"):
|
||||
start_hyperopt(args)
|
||||
start_hyperopt(pargs)
|
||||
|
||||
|
||||
def test_start(mocker, default_conf, caplog) -> None:
|
||||
@@ -246,8 +248,8 @@ def test_start(mocker, default_conf, caplog) -> None:
|
||||
'--hyperopt', 'DefaultHyperOpt',
|
||||
'--epochs', '5'
|
||||
]
|
||||
args = get_args(args)
|
||||
start_hyperopt(args)
|
||||
pargs = get_args(args)
|
||||
start_hyperopt(pargs)
|
||||
|
||||
assert log_has('Starting freqtrade in Hyperopt mode', caplog)
|
||||
assert start_mock.call_count == 1
|
||||
@@ -269,9 +271,9 @@ def test_start_no_data(mocker, default_conf, caplog) -> None:
|
||||
'--hyperopt', 'DefaultHyperOpt',
|
||||
'--epochs', '5'
|
||||
]
|
||||
args = get_args(args)
|
||||
pargs = get_args(args)
|
||||
with pytest.raises(OperationalException, match='No data found. Terminating.'):
|
||||
start_hyperopt(args)
|
||||
start_hyperopt(pargs)
|
||||
|
||||
|
||||
def test_start_filelock(mocker, default_conf, caplog) -> None:
|
||||
@@ -286,16 +288,19 @@ def test_start_filelock(mocker, default_conf, caplog) -> None:
|
||||
'--hyperopt', 'DefaultHyperOpt',
|
||||
'--epochs', '5'
|
||||
]
|
||||
args = get_args(args)
|
||||
start_hyperopt(args)
|
||||
pargs = get_args(args)
|
||||
start_hyperopt(pargs)
|
||||
assert log_has("Another running instance of freqtrade Hyperopt detected.", caplog)
|
||||
|
||||
|
||||
def test_loss_calculation_prefer_correct_trade_count(default_conf, hyperopt_results) -> None:
|
||||
hl = HyperOptLossResolver.load_hyperoptloss(default_conf)
|
||||
correct = hl.hyperopt_loss_function(hyperopt_results, 600)
|
||||
over = hl.hyperopt_loss_function(hyperopt_results, 600 + 100)
|
||||
under = hl.hyperopt_loss_function(hyperopt_results, 600 - 100)
|
||||
correct = hl.hyperopt_loss_function(hyperopt_results, 600,
|
||||
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
||||
over = hl.hyperopt_loss_function(hyperopt_results, 600 + 100,
|
||||
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
||||
under = hl.hyperopt_loss_function(hyperopt_results, 600 - 100,
|
||||
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
||||
assert over > correct
|
||||
assert under > correct
|
||||
|
||||
@@ -305,8 +310,10 @@ def test_loss_calculation_prefer_shorter_trades(default_conf, hyperopt_results)
|
||||
resultsb.loc[1, 'trade_duration'] = 20
|
||||
|
||||
hl = HyperOptLossResolver.load_hyperoptloss(default_conf)
|
||||
longer = hl.hyperopt_loss_function(hyperopt_results, 100)
|
||||
shorter = hl.hyperopt_loss_function(resultsb, 100)
|
||||
longer = hl.hyperopt_loss_function(hyperopt_results, 100,
|
||||
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
||||
shorter = hl.hyperopt_loss_function(resultsb, 100,
|
||||
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
||||
assert shorter < longer
|
||||
|
||||
|
||||
@@ -317,9 +324,12 @@ def test_loss_calculation_has_limited_profit(default_conf, hyperopt_results) ->
|
||||
results_under['profit_percent'] = hyperopt_results['profit_percent'] / 2
|
||||
|
||||
hl = HyperOptLossResolver.load_hyperoptloss(default_conf)
|
||||
correct = hl.hyperopt_loss_function(hyperopt_results, 600)
|
||||
over = hl.hyperopt_loss_function(results_over, 600)
|
||||
under = hl.hyperopt_loss_function(results_under, 600)
|
||||
correct = hl.hyperopt_loss_function(hyperopt_results, 600,
|
||||
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
||||
over = hl.hyperopt_loss_function(results_over, 600,
|
||||
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
||||
under = hl.hyperopt_loss_function(results_under, 600,
|
||||
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
||||
assert over < correct
|
||||
assert under > correct
|
||||
|
||||
|
@@ -15,20 +15,21 @@ def test_generate_text_table(default_conf, mocker):
|
||||
'profit_percent': [0.1, 0.2],
|
||||
'profit_abs': [0.2, 0.4],
|
||||
'trade_duration': [10, 30],
|
||||
'profit': [2, 0],
|
||||
'loss': [0, 0]
|
||||
'wins': [2, 0],
|
||||
'draws': [0, 0],
|
||||
'losses': [0, 0]
|
||||
}
|
||||
)
|
||||
|
||||
result_str = (
|
||||
'| Pair | Buy Count | Avg Profit % | Cum Profit % | Tot Profit BTC '
|
||||
'| Tot Profit % | Avg Duration | Wins | Losses |\n'
|
||||
'|:--------|------------:|---------------:|---------------:|-----------------:'
|
||||
'|---------------:|:---------------|-------:|---------:|\n'
|
||||
'| ETH/BTC | 2 | 15.00 | 30.00 | 0.60000000 '
|
||||
'| 15.00 | 0:20:00 | 2 | 0 |\n'
|
||||
'| TOTAL | 2 | 15.00 | 30.00 | 0.60000000 '
|
||||
'| 15.00 | 0:20:00 | 2 | 0 |'
|
||||
'| Pair | Buys | Avg Profit % | Cum Profit % | Tot Profit BTC |'
|
||||
' Tot Profit % | Avg Duration | Wins | Draws | Losses |\n'
|
||||
'|:--------|-------:|---------------:|---------------:|-----------------:|'
|
||||
'---------------:|:---------------|-------:|--------:|---------:|\n'
|
||||
'| ETH/BTC | 2 | 15.00 | 30.00 | 0.60000000 |'
|
||||
' 15.00 | 0:20:00 | 2 | 0 | 0 |\n'
|
||||
'| TOTAL | 2 | 15.00 | 30.00 | 0.60000000 |'
|
||||
' 15.00 | 0:20:00 | 2 | 0 | 0 |'
|
||||
)
|
||||
assert generate_text_table(data={'ETH/BTC': {}},
|
||||
stake_currency='BTC', max_open_trades=2,
|
||||
@@ -43,21 +44,22 @@ def test_generate_text_table_sell_reason(default_conf, mocker):
|
||||
'profit_percent': [0.1, 0.2, -0.1],
|
||||
'profit_abs': [0.2, 0.4, -0.2],
|
||||
'trade_duration': [10, 30, 10],
|
||||
'profit': [2, 0, 0],
|
||||
'loss': [0, 0, 1],
|
||||
'wins': [2, 0, 0],
|
||||
'draws': [0, 0, 0],
|
||||
'losses': [0, 0, 1],
|
||||
'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
|
||||
}
|
||||
)
|
||||
|
||||
result_str = (
|
||||
'| Sell Reason | Sell Count | Wins | Losses | Avg Profit % |'
|
||||
' Cum Profit % | Tot Profit BTC | Tot Profit % |\n'
|
||||
'|:--------------|-------------:|-------:|---------:|---------------:|'
|
||||
'---------------:|-----------------:|---------------:|\n'
|
||||
'| roi | 2 | 2 | 0 | 15 |'
|
||||
' 30 | 0.6 | 15 |\n'
|
||||
'| stop_loss | 1 | 0 | 1 | -10 |'
|
||||
' -10 | -0.2 | -5 |'
|
||||
'| Sell Reason | Sells | Wins | Draws | Losses |'
|
||||
' Avg Profit % | Cum Profit % | Tot Profit BTC | Tot Profit % |\n'
|
||||
'|:--------------|--------:|-------:|--------:|---------:|'
|
||||
'---------------:|---------------:|-----------------:|---------------:|\n'
|
||||
'| roi | 2 | 2 | 0 | 0 |'
|
||||
' 15 | 30 | 0.6 | 15 |\n'
|
||||
'| stop_loss | 1 | 0 | 0 | 1 |'
|
||||
' -10 | -10 | -0.2 | -5 |'
|
||||
)
|
||||
assert generate_text_table_sell_reason(
|
||||
data={'ETH/BTC': {}},
|
||||
@@ -67,38 +69,40 @@ def test_generate_text_table_sell_reason(default_conf, mocker):
|
||||
|
||||
def test_generate_text_table_strategy(default_conf, mocker):
|
||||
results = {}
|
||||
results['ETH/BTC'] = pd.DataFrame(
|
||||
results['TestStrategy1'] = pd.DataFrame(
|
||||
{
|
||||
'pair': ['ETH/BTC', 'ETH/BTC', 'ETH/BTC'],
|
||||
'profit_percent': [0.1, 0.2, 0.3],
|
||||
'profit_abs': [0.2, 0.4, 0.5],
|
||||
'trade_duration': [10, 30, 10],
|
||||
'profit': [2, 0, 0],
|
||||
'loss': [0, 0, 1],
|
||||
'wins': [2, 0, 0],
|
||||
'draws': [0, 0, 0],
|
||||
'losses': [0, 0, 1],
|
||||
'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
|
||||
}
|
||||
)
|
||||
results['LTC/BTC'] = pd.DataFrame(
|
||||
results['TestStrategy2'] = pd.DataFrame(
|
||||
{
|
||||
'pair': ['LTC/BTC', 'LTC/BTC', 'LTC/BTC'],
|
||||
'profit_percent': [0.4, 0.2, 0.3],
|
||||
'profit_abs': [0.4, 0.4, 0.5],
|
||||
'trade_duration': [15, 30, 15],
|
||||
'profit': [4, 1, 0],
|
||||
'loss': [0, 0, 1],
|
||||
'wins': [4, 1, 0],
|
||||
'draws': [0, 0, 0],
|
||||
'losses': [0, 0, 1],
|
||||
'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
|
||||
}
|
||||
)
|
||||
|
||||
result_str = (
|
||||
'| Strategy | Buy Count | Avg Profit % | Cum Profit % | Tot Profit BTC '
|
||||
'| Tot Profit % | Avg Duration | Wins | Losses |\n'
|
||||
'|:-----------|------------:|---------------:|---------------:|-----------------:'
|
||||
'|---------------:|:---------------|-------:|---------:|\n'
|
||||
'| ETH/BTC | 3 | 20.00 | 60.00 '
|
||||
'| 1.10000000 | 30.00 | 0:17:00 | 3 | 0 |\n'
|
||||
'| LTC/BTC | 3 | 30.00 | 90.00 '
|
||||
'| 1.30000000 | 45.00 | 0:20:00 | 3 | 0 |'
|
||||
'| Strategy | Buys | Avg Profit % | Cum Profit % | Tot'
|
||||
' Profit BTC | Tot Profit % | Avg Duration | Wins | Draws | Losses |\n'
|
||||
'|:--------------|-------:|---------------:|---------------:|------'
|
||||
'-----------:|---------------:|:---------------|-------:|--------:|---------:|\n'
|
||||
'| TestStrategy1 | 3 | 20.00 | 60.00 | '
|
||||
' 1.10000000 | 30.00 | 0:17:00 | 3 | 0 | 0 |\n'
|
||||
'| TestStrategy2 | 3 | 30.00 | 90.00 | '
|
||||
' 1.30000000 | 45.00 | 0:20:00 | 3 | 0 | 0 |'
|
||||
)
|
||||
assert generate_text_table_strategy('BTC', 2, all_results=results) == result_str
|
||||
|
||||
@@ -111,4 +115,4 @@ def test_generate_edge_table(edge_conf, mocker):
|
||||
assert generate_edge_table(results).count(':|') == 7
|
||||
assert generate_edge_table(results).count('| ETH/BTC |') == 1
|
||||
assert generate_edge_table(results).count(
|
||||
'| risk reward ratio | required risk reward | expectancy |') == 1
|
||||
'| Risk Reward Ratio | Required Risk Reward | Expectancy |') == 1
|
||||
|
Reference in New Issue
Block a user