From 8888f423cfcb2307f7a634899ca90e1cf1776a75 Mon Sep 17 00:00:00 2001 From: Joe Schr Date: Fri, 5 Feb 2021 18:16:24 +0100 Subject: [PATCH 1/2] add hyperopts for BinHV45 strategy --- user_data/hyperopts/BinHV45HyperOpt.py | 104 +++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 user_data/hyperopts/BinHV45HyperOpt.py diff --git a/user_data/hyperopts/BinHV45HyperOpt.py b/user_data/hyperopts/BinHV45HyperOpt.py new file mode 100644 index 0000000..81ee16e --- /dev/null +++ b/user_data/hyperopts/BinHV45HyperOpt.py @@ -0,0 +1,104 @@ +# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement + +# --- Do not remove these libs --- +from functools import reduce +from typing import Any, Callable, Dict, List + +import numpy as np # noqa +import pandas as pd # noqa +from pandas import DataFrame +from skopt.space import Categorical, Dimension, Integer, Real # noqa + +from freqtrade.optimize.hyperopt_interface import IHyperOpt + +# -------------------------------- +# Add your lib to import here +import talib.abstract as ta # noqa +import freqtrade.vendor.qtpylib.indicators as qtpylib + + +class BinHV45HyperOpt(IHyperOpt): + """ + This is a Hyperopt template to get you started. + + More information in the documentation: https://www.freqtrade.io/en/latest/hyperopt/ + + You should: + - Add any lib you need to build your hyperopt. + + You must keep: + - The prototypes for the methods: populate_indicators, indicator_space, buy_strategy_generator. + + The methods roi_space, generate_roi_table and stoploss_space are not required + and are provided by default. + However, you may override them if you need 'roi' and 'stoploss' spaces that + differ from the defaults offered by Freqtrade. + Sample implementation of these methods will be copied to `user_data/hyperopts` when + creating the user-data directory using `freqtrade create-userdir --userdir user_data`, + or is available online under the following URL: + https://github.com/freqtrade/freqtrade/blob/develop/freqtrade/templates/sample_hyperopt_advanced.py. + """ + + @staticmethod + def buy_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the buy strategy parameters to be used by Hyperopt. + """ + def populate_buy_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Buy strategy Hyperopt will build and use. + """ + conditions = [] + + conditions.append(dataframe['lower'].shift().gt(0)) + conditions.append(dataframe['bbdelta'].gt( + dataframe['close'] * params['bbdelta'] / 1000)) + conditions.append(dataframe['closedelta'].gt( + dataframe['close'] * params['closedelta'] / 1000)) + conditions.append(dataframe['tail'].lt(dataframe['bbdelta'] * params['tail'] / 1000)) + conditions.append(dataframe['close'].lt(dataframe['lower'].shift())) + conditions.append(dataframe['close'].le(dataframe['close'].shift())) + + # Check that the candle had volume + conditions.append(dataframe['volume'] > 0) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'buy'] = 1 + + return dataframe + + return populate_buy_trend + + @staticmethod + def indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching buy strategy parameters. + """ + return [ + Integer(1, 15, name='bbdelta'), + Integer(15, 20, name='closedelta'), + Integer(20, 30, name='tail'), + ] + + @staticmethod + def sell_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the sell strategy parameters to be used by Hyperopt. + """ + def populate_sell_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + no sell signal + """ + dataframe['sell'] = 0 + return dataframe + + return populate_sell_trend + + @staticmethod + def sell_indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching sell strategy parameters. + """ + return [] From d4f1514a325bb11d0ea5510a0422e0a0582fdb96 Mon Sep 17 00:00:00 2001 From: Joe Schr Date: Sun, 7 Feb 2021 15:49:43 +0100 Subject: [PATCH 2/2] Improve leading comment explaining hyperopt strategy --- user_data/hyperopts/BinHV45HyperOpt.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/user_data/hyperopts/BinHV45HyperOpt.py b/user_data/hyperopts/BinHV45HyperOpt.py index 81ee16e..eafb3b2 100644 --- a/user_data/hyperopts/BinHV45HyperOpt.py +++ b/user_data/hyperopts/BinHV45HyperOpt.py @@ -19,24 +19,17 @@ import freqtrade.vendor.qtpylib.indicators as qtpylib class BinHV45HyperOpt(IHyperOpt): """ - This is a Hyperopt template to get you started. + Hyperopt file for optimizing BinHV45Strategy. + Uses ranges to find best parameter combination for bbdelta, closedelta and tail + of the buy strategy. - More information in the documentation: https://www.freqtrade.io/en/latest/hyperopt/ + Sell strategy is ignored, because it's ignored in BinHV45Strategy as well. + This strategy therefor works without explicit sell signal therefor hyperopting + for 'roi' is recommend as well - You should: - - Add any lib you need to build your hyperopt. - - You must keep: - - The prototypes for the methods: populate_indicators, indicator_space, buy_strategy_generator. - - The methods roi_space, generate_roi_table and stoploss_space are not required - and are provided by default. - However, you may override them if you need 'roi' and 'stoploss' spaces that - differ from the defaults offered by Freqtrade. - Sample implementation of these methods will be copied to `user_data/hyperopts` when - creating the user-data directory using `freqtrade create-userdir --userdir user_data`, - or is available online under the following URL: - https://github.com/freqtrade/freqtrade/blob/develop/freqtrade/templates/sample_hyperopt_advanced.py. + Also, this is just ONE way to optimize this strategy - others might also include + disabling certain conditions completely. This file is just a starting point, feel free + to improve and PR. """ @staticmethod