From 06f4f2db0a19043ff234b4dfa69af336cb6a0f90 Mon Sep 17 00:00:00 2001 From: robcaulk Date: Sat, 8 Oct 2022 12:45:49 +0200 Subject: [PATCH] improve performance and documentation of spice-rack. --- docs/freqai-spice-rack.md | 68 +++++++++++++++++++ freqtrade/freqai/data_kitchen.py | 2 +- .../freqai/spice_rack/lightgbm_config.json | 2 +- freqtrade/freqai/utils.py | 9 ++- mkdocs.yml | 1 + 5 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 docs/freqai-spice-rack.md diff --git a/docs/freqai-spice-rack.md b/docs/freqai-spice-rack.md new file mode 100644 index 000000000..fa32ccbed --- /dev/null +++ b/docs/freqai-spice-rack.md @@ -0,0 +1,68 @@ +# Using the `spice_rack` + +The `spice_rack` is aimed at users who do not wish to deal with setting up `FreqAI` confgs, but instead prefer to interact with `FreqAI` similar to a `talib` indicator. In this case, the user can instead simply add two keys to their config: + +```json + "freqai_spice_rack": true, + "freqai_identifier": "spicey-id", +``` + +Which tells `FreqAI` to set up a pre-set `FreqAI` instance automatically under the hood with preset parameters. Now the user can access a suite of custom `FreqAI` supercharged indicators inside their strategy: + +```python + dataframe['dissimilarity_index'] = self.freqai.spice_rack( + 'DI_values', dataframe, metadata, self) + dataframe['extrema'] = self.freqai.spice_rack( + '&s-extrema', dataframe, metadata, self) + self.freqai.close_spice_rack() # user must close the spicerack +``` + +Users can then use these columns, concert with all their own additional indicators added to `populate_indicators` in their entry/exit criteria and strategy callback methods the same way as any typical indicator (note: `spice_rack` indicators should not be used exclusively for entries and exits, the following example is just a demonstration of syntax. `spice_rack` indicators should **always** be used to support existing strategies). For example: + +```python + def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame: + + df.loc[ + ( + (df['dissimilarity_index'] < 1) & + (df['extrema'] > 0.1) + ), + 'enter_long'] = 1 + + df.loc[ + ( + (df['dissimilarity_index'] < 1) & + (df['extrema'] <> -0.1) + ), + 'enter_short'] = 1 + + return df + + def populate_exit_trend(self, df: DataFrame, metadata: dict) -> DataFrame: + + df.loc[ + ( + (df['dissimilarity_index'] < 1) & + (df['extrema'] > 0.1) + ), + + 'exit_long'] = 1 + + df.loc[ + ( + + (df['dissimilarity_index'] < 1) & + (df['extrema'] < -0.1) + ), + 'exit_short'] = 1 + + return df +``` + + +## Available indicators + +| Parameter | Description | +|------------|-------------| +| `DI_values` | **Required.**
The dissimilarity index of the current candle to the recent candles. More information available [here](freqai-feature-engineering.md#identifying-outliers-with-the-dissimilarity-index-di)
**Datatype:** Floats. +| `extrema` | **Required.**
A continuous prediction from FreqAI which aims to help predict if the current candle is a maxima or a minma. FreqAI aims for 1 to be a maxima and -1 to be a minima - but the values should typically hover between -0.2 and 0.2.
**Datatype:** Floats. \ No newline at end of file diff --git a/freqtrade/freqai/data_kitchen.py b/freqtrade/freqai/data_kitchen.py index 4180c3bc2..602400378 100644 --- a/freqtrade/freqai/data_kitchen.py +++ b/freqtrade/freqai/data_kitchen.py @@ -1262,7 +1262,7 @@ class FreqaiDataKitchen: return file_exists def spice_extractor(self, indicator: str, dataframe: DataFrame) -> npt.NDArray: - if indicator in dataframe: + if indicator in dataframe.columns: return np.array(dataframe[indicator]) else: logger.warning(f'User asked spice_rack for {indicator}, ' diff --git a/freqtrade/freqai/spice_rack/lightgbm_config.json b/freqtrade/freqai/spice_rack/lightgbm_config.json index 89ec978db..5fcef8db2 100644 --- a/freqtrade/freqai/spice_rack/lightgbm_config.json +++ b/freqtrade/freqai/spice_rack/lightgbm_config.json @@ -4,7 +4,7 @@ "enabled": true, "purge_old_models": true, "train_period_days": 4, - "backtest_period_days": 2, + "backtest_period_days": 1, "identifier": "spicy-id", "feature_parameters": { "include_timeframes": [ diff --git a/freqtrade/freqai/utils.py b/freqtrade/freqai/utils.py index c604c5adc..c00d9329e 100644 --- a/freqtrade/freqai/utils.py +++ b/freqtrade/freqai/utils.py @@ -160,14 +160,13 @@ def auto_populate_any_indicators( if set_generalized_indicators: df["%-day_of_week"] = (df["date"].dt.dayofweek + 1) / 7 df["%-hour_of_day"] = (df["date"].dt.hour + 1) / 25 - df["&s-minima"] = 0 - df["&s-maxima"] = 0 + df["&s-extrema"] = 0 min_peaks = argrelextrema(df["close"].values, np.less, order=80) max_peaks = argrelextrema(df["close"].values, np.greater, order=80) for mp in min_peaks[0]: - df.at[mp, "&s-minima"] = 1 + df.at[mp, "&s-extrema"] = -1 for mp in max_peaks[0]: - df.at[mp, "&s-maxima"] = 1 + df.at[mp, "&s-extrema"] = 1 return df @@ -222,7 +221,7 @@ def setup_freqai_spice_rack(config: dict, exchange: Optional[Exchange]) -> Dict[ config['freqai']['feature_parameters'].update({'include_timeframes': new_tfs}) config['freqai']['feature_parameters'].update({'include_corr_pairlist': new_corr_pairs}) - config.update({"freqaimodel": 'LightGBMRegressorMultiTarget'}) + config.update({"freqaimodel": 'LightGBMRegressor'}) return config # Keep below for when we wish to download heterogeneously lengthed data for FreqAI. diff --git a/mkdocs.yml b/mkdocs.yml index 6477c1feb..f98de3197 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -29,6 +29,7 @@ nav: - Parameter table: freqai-parameter-table.md - Feature engineering: freqai-feature-engineering.md - Running FreqAI: freqai-running.md + - Spice Rack: freqai-spice-rack.md - Developer guide: freqai-developers.md - Short / Leverage: leverage.md - Utility Sub-commands: utils.md