remove duplicated backtesting from hyperopt
This commit is contained in:
parent
f43ba44b15
commit
041e201713
@ -35,44 +35,6 @@ def conf():
|
||||
"stoploss": -0.05
|
||||
}
|
||||
|
||||
|
||||
def backtest2(conf, pairs, mocker, buy_strategy):
|
||||
trades = []
|
||||
mocker.patch.dict('freqtrade.main._CONF', conf)
|
||||
for pair in pairs:
|
||||
with open('freqtrade/tests/testdata/'+pair+'.json') as data_file:
|
||||
data = json.load(data_file)
|
||||
|
||||
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=data)
|
||||
mocker.patch('arrow.utcnow', return_value=arrow.get('2017-08-20T14:50:00'))
|
||||
mocker.patch('freqtrade.analyze.populate_buy_trend', side_effect=buy_strategy)
|
||||
ticker = analyze_ticker(pair)
|
||||
# for each buy point
|
||||
for index, row in ticker[ticker.buy == 1].iterrows():
|
||||
trade = Trade(
|
||||
open_rate=row['close'],
|
||||
open_date=arrow.get(row['date']).datetime,
|
||||
amount=1,
|
||||
)
|
||||
# calculate win/lose forwards from buy point
|
||||
for index2, row2 in ticker[index:].iterrows():
|
||||
if should_sell(trade, row2['close'], arrow.get(row2['date']).datetime):
|
||||
current_profit = (row2['close'] - trade.open_rate) / trade.open_rate
|
||||
|
||||
trades.append((pair, current_profit, index2 - index))
|
||||
break
|
||||
|
||||
labels = ['currency', 'profit', 'duration']
|
||||
results = DataFrame.from_records(trades, columns=labels)
|
||||
|
||||
print_results(results)
|
||||
|
||||
# set the value below to suit your number concurrent trades so its realistic to 20days of data
|
||||
TARGET_TRADES = 1200
|
||||
if results.profit.sum() == 0 or results.profit.mean() == 0:
|
||||
return 49999999999 # avoid division by zero, return huge value to discard result
|
||||
return abs(len(results.index) - 1200.1) / (results.profit.sum() ** 2) * results.duration.mean() # the smaller the better
|
||||
|
||||
def buy_strategy_generator(params):
|
||||
print(params)
|
||||
def populate_buy_trend(dataframe: DataFrame) -> DataFrame:
|
||||
@ -114,19 +76,18 @@ def buy_strategy_generator(params):
|
||||
|
||||
@pytest.mark.skipif(not os.environ.get('BACKTEST', False), reason="BACKTEST not set")
|
||||
def test_hyperopt(conf, pairs, mocker):
|
||||
|
||||
# def optimizer(params):
|
||||
# return backtest2(conf, pairs, mocker, buy_strategy_generator(params))
|
||||
|
||||
def optimizer(params):
|
||||
buy_strategy = buy_strategy_generator(params)
|
||||
mocker.patch('freqtrade.analyze.populate_buy_trend', side_effect=buy_strategy)
|
||||
results = backtest(conf, pairs, mocker)
|
||||
|
||||
print_results(results)
|
||||
if len(results.index) < 800: # require at least 800 trades
|
||||
return 100000 # return large number to "ignore" this result
|
||||
return results.duration.mean() ** 3 / results.profit.sum() / results.profit.mean() # the smaller the better
|
||||
|
||||
# set the value below to suit your number concurrent trades so its realistic to 20days of data
|
||||
TARGET_TRADES = 1200
|
||||
if results.profit.sum() == 0 or results.profit.mean() == 0:
|
||||
return 49999999999 # avoid division by zero, return huge value to discard result
|
||||
return abs(len(results.index) - 1200.1) / (results.profit.sum() ** 2) * results.duration.mean() # the smaller the better
|
||||
|
||||
space = {
|
||||
'mfi': hp.choice('mfi', [
|
||||
|
Loading…
Reference in New Issue
Block a user