add explicit metadata argument to example strat, include it with backtesting

This commit is contained in:
robcaulk 2023-02-04 16:53:17 +01:00
parent 5da60b718d
commit e569f6f6df
4 changed files with 15 additions and 15 deletions

View File

@ -16,7 +16,7 @@ Meanwhile, high level feature engineering is handled within `"feature_parameters
It is advisable to start from the template `feature_engineering_*` functions in the source provided example strategy (found in `templates/FreqaiExampleStrategy.py`) to ensure that the feature definitions are following the correct conventions. Here is an example of how to set the indicators and labels in the strategy: It is advisable to start from the template `feature_engineering_*` functions in the source provided example strategy (found in `templates/FreqaiExampleStrategy.py`) to ensure that the feature definitions are following the correct conventions. Here is an example of how to set the indicators and labels in the strategy:
```python ```python
def feature_engineering_expand_all(self, dataframe, period, **kwargs): def feature_engineering_expand_all(self, dataframe, period, metadata, **kwargs):
""" """
*Only functional with FreqAI enabled strategies* *Only functional with FreqAI enabled strategies*
This function will automatically expand the defined features on the config defined This function will automatically expand the defined features on the config defined
@ -66,7 +66,7 @@ It is advisable to start from the template `feature_engineering_*` functions in
return dataframe return dataframe
def feature_engineering_expand_basic(self, dataframe, **kwargs): def feature_engineering_expand_basic(self, dataframe, metadata, **kwargs):
""" """
*Only functional with FreqAI enabled strategies* *Only functional with FreqAI enabled strategies*
This function will automatically expand the defined features on the config defined This function will automatically expand the defined features on the config defined
@ -94,7 +94,7 @@ It is advisable to start from the template `feature_engineering_*` functions in
dataframe["%-raw_price"] = dataframe["close"] dataframe["%-raw_price"] = dataframe["close"]
return dataframe return dataframe
def feature_engineering_standard(self, dataframe, **kwargs): def feature_engineering_standard(self, dataframe, metadata, **kwargs):
""" """
*Only functional with FreqAI enabled strategies* *Only functional with FreqAI enabled strategies*
This optional function will be called once with the dataframe of the base timeframe. This optional function will be called once with the dataframe of the base timeframe.
@ -119,7 +119,7 @@ It is advisable to start from the template `feature_engineering_*` functions in
dataframe["%-hour_of_day"] = (dataframe["date"].dt.hour + 1) / 25 dataframe["%-hour_of_day"] = (dataframe["date"].dt.hour + 1) / 25
return dataframe return dataframe
def set_freqai_targets(self, dataframe, **kwargs): def set_freqai_targets(self, dataframe, metadata, **kwargs):
""" """
*Only functional with FreqAI enabled strategies* *Only functional with FreqAI enabled strategies*
Required function to set the targets for the model. Required function to set the targets for the model.
@ -183,7 +183,7 @@ In total, the number of features the user of the presented example strat has cre
All `feature_engineering_*` and `set_freqai_targets()` functions are passed a `metadata` dictionary which contains information about the `pair`, `tf` (timeframe), and `period` that FreqAI is automating for feature building. As such, a user can use `metadata` inside `feature_engineering_*` functions as criteria for blocking/reserving features for certain timeframes, periods, pairs etc. All `feature_engineering_*` and `set_freqai_targets()` functions are passed a `metadata` dictionary which contains information about the `pair`, `tf` (timeframe), and `period` that FreqAI is automating for feature building. As such, a user can use `metadata` inside `feature_engineering_*` functions as criteria for blocking/reserving features for certain timeframes, periods, pairs etc.
```py ```py
def feature_engineering_expand_all(self, dataframe, period, **kwargs): def feature_engineering_expand_all(self, dataframe, period, metadata, **kwargs):
if metadata["tf"] == "1h": if metadata["tf"] == "1h":
dataframe["%-roc-period"] = ta.ROC(dataframe, timeperiod=period) dataframe["%-roc-period"] = ta.ROC(dataframe, timeperiod=period)
``` ```

View File

@ -1253,13 +1253,11 @@ class FreqaiDataKitchen:
informative_copy = informative_df.copy() informative_copy = informative_df.copy()
for t in self.freqai_config["feature_parameters"]["indicator_periods_candles"]: for t in self.freqai_config["feature_parameters"]["indicator_periods_candles"]:
metadata["period"] = t
df_features = strategy.feature_engineering_expand_all( df_features = strategy.feature_engineering_expand_all(
informative_copy.copy(), t, metadata=metadata) informative_copy.copy(), t, metadata=metadata)
suffix = f"{t}" suffix = f"{t}"
informative_df = self.merge_features(informative_df, df_features, tf, tf, suffix) informative_df = self.merge_features(informative_df, df_features, tf, tf, suffix)
metadata.pop("period")
generic_df = strategy.feature_engineering_expand_basic( generic_df = strategy.feature_engineering_expand_basic(
informative_copy.copy(), metadata=metadata) informative_copy.copy(), metadata=metadata)
suffix = "gen" suffix = "gen"

View File

@ -324,9 +324,11 @@ class IFreqaiModel(ABC):
populate_indicators = False populate_indicators = False
dataframe_base_train = dataframe.loc[dataframe["date"] < tr_train.stopdt, :] dataframe_base_train = dataframe.loc[dataframe["date"] < tr_train.stopdt, :]
dataframe_base_train = strategy.set_freqai_targets(dataframe_base_train) dataframe_base_train = strategy.set_freqai_targets(
dataframe_base_train, metadata=metadata)
dataframe_base_backtest = dataframe.loc[dataframe["date"] < tr_backtest.stopdt, :] dataframe_base_backtest = dataframe.loc[dataframe["date"] < tr_backtest.stopdt, :]
dataframe_base_backtest = strategy.set_freqai_targets(dataframe_base_backtest) dataframe_base_backtest = strategy.set_freqai_targets(
dataframe_base_backtest, metadata=metadata)
dataframe_train = dk.slice_dataframe(tr_train, dataframe_base_train) dataframe_train = dk.slice_dataframe(tr_train, dataframe_base_train)
dataframe_backtest = dk.slice_dataframe(tr_backtest, dataframe_base_backtest) dataframe_backtest = dk.slice_dataframe(tr_backtest, dataframe_base_backtest)

View File

@ -46,7 +46,7 @@ class FreqaiExampleStrategy(IStrategy):
std_dev_multiplier_sell = CategoricalParameter( std_dev_multiplier_sell = CategoricalParameter(
[0.75, 1, 1.25, 1.5, 1.75], space="sell", default=1.25, optimize=True) [0.75, 1, 1.25, 1.5, 1.75], space="sell", default=1.25, optimize=True)
def feature_engineering_expand_all(self, dataframe, period, **kwargs): def feature_engineering_expand_all(self, dataframe, period, metadata, **kwargs):
""" """
*Only functional with FreqAI enabled strategies* *Only functional with FreqAI enabled strategies*
This function will automatically expand the defined features on the config defined This function will automatically expand the defined features on the config defined
@ -58,9 +58,9 @@ class FreqaiExampleStrategy(IStrategy):
All features must be prepended with `%` to be recognized by FreqAI internals. All features must be prepended with `%` to be recognized by FreqAI internals.
Access metadata such as the current pair/timeframe/period with: Access metadata such as the current pair/timeframe with:
`metadata["pair"]` `metadata["tf"]` `metadata["period"]` `metadata["pair"]` `metadata["tf"]`
More details on how these config defined parameters accelerate feature engineering More details on how these config defined parameters accelerate feature engineering
in the documentation at: in the documentation at:
@ -103,7 +103,7 @@ class FreqaiExampleStrategy(IStrategy):
return dataframe return dataframe
def feature_engineering_expand_basic(self, dataframe, **kwargs): def feature_engineering_expand_basic(self, dataframe, metadata, **kwargs):
""" """
*Only functional with FreqAI enabled strategies* *Only functional with FreqAI enabled strategies*
This function will automatically expand the defined features on the config defined This function will automatically expand the defined features on the config defined
@ -138,7 +138,7 @@ class FreqaiExampleStrategy(IStrategy):
dataframe["%-raw_price"] = dataframe["close"] dataframe["%-raw_price"] = dataframe["close"]
return dataframe return dataframe
def feature_engineering_standard(self, dataframe, **kwargs): def feature_engineering_standard(self, dataframe, metadata, **kwargs):
""" """
*Only functional with FreqAI enabled strategies* *Only functional with FreqAI enabled strategies*
This optional function will be called once with the dataframe of the base timeframe. This optional function will be called once with the dataframe of the base timeframe.
@ -167,7 +167,7 @@ class FreqaiExampleStrategy(IStrategy):
dataframe["%-hour_of_day"] = dataframe["date"].dt.hour dataframe["%-hour_of_day"] = dataframe["date"].dt.hour
return dataframe return dataframe
def set_freqai_targets(self, dataframe, **kwargs): def set_freqai_targets(self, dataframe, metadata, **kwargs):
""" """
*Only functional with FreqAI enabled strategies* *Only functional with FreqAI enabled strategies*
Required function to set the targets for the model. Required function to set the targets for the model.