From 2d6408a3638250b88a46777b92b6689fd0efbb5a Mon Sep 17 00:00:00 2001 From: Joe Schr <8218910+TheJoeSchr@users.noreply.github.com> Date: Wed, 19 Jun 2024 19:22:49 +0200 Subject: [PATCH] feat: adds `max_candles` to orderflow config --- docs/advanced-orderflow.md | 2 ++ freqtrade/constants.py | 9 ++++++++- freqtrade/data/converter/orderflow.py | 5 ++++- tests/data/test_converter_public_trades.py | 23 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/docs/advanced-orderflow.md b/docs/advanced-orderflow.md index b5100064a..54655d488 100644 --- a/docs/advanced-orderflow.md +++ b/docs/advanced-orderflow.md @@ -21,6 +21,7 @@ This guide walks you through utilizing public trade data for advanced orderflow 2. **Configure Orderflow Processing:** Define your desired settings for orderflow processing within the orderflow section of config.json. Here, you can adjust factors like: +- `max_candles`: Filter how many candles get processed from the tail - `scale`: This controls the price bin size for the footprint chart. - `stacked_imbalance_range`: Defines the minimum consecutive imbalanced price levels required for consideration. - `imbalance_volume`: Filters out imbalances with volume below this threshold. @@ -28,6 +29,7 @@ This guide walks you through utilizing public trade data for advanced orderflow ```json "orderflow": { + "max_candles": 1500, "scale": 0.5, "stacked_imbalance_range": 3, // needs at least this amount of imbalance next to each other "imbalance_volume": 1, // filters out below diff --git a/freqtrade/constants.py b/freqtrade/constants.py index f56e14503..1105f4ffe 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -537,12 +537,19 @@ CONF_SCHEMA = { "orderflow": { "type": "object", "properties": { + "max_candles": {"type": "number", "minimum": 1, "default": 1500}, "scale": {"type": "number", "minimum": 0.0}, "stacked_imbalance_range": {"type": "number", "minimum": 0}, "imbalance_volume": {"type": "number", "minimum": 0}, "imbalance_ratio": {"type": "number", "minimum": 0.0}, }, - "required": ["scale", "stacked_imbalance_range", "imbalance_volume", "imbalance_ratio"], + "required": [ + "max_candles", + "scale", + "stacked_imbalance_range", + "imbalance_volume", + "imbalance_ratio", + ], }, }, "definitions": { diff --git a/freqtrade/data/converter/orderflow.py b/freqtrade/data/converter/orderflow.py index 8d60d5aaa..1b1d7a565 100644 --- a/freqtrade/data/converter/orderflow.py +++ b/freqtrade/data/converter/orderflow.py @@ -68,8 +68,11 @@ def populate_dataframe_with_trades( # calculate ohlcv candle start and end _calculate_ohlcv_candle_start_and_end(trades, timeframe) + # get date of earliest max_candles candle + max_candles = config_orderflow["max_candles"] + start_date = df.tail(max_candles).date.iat[0] # slice of trades that are before current ohlcv candles to make groupby faster - trades = trades.loc[trades.candle_start >= df.date[0]] + trades = trades.loc[trades.candle_start >= start_date] trades.reset_index(inplace=True, drop=True) # group trades by candle start diff --git a/tests/data/test_converter_public_trades.py b/tests/data/test_converter_public_trades.py index 07e17b1b7..9baadb868 100644 --- a/tests/data/test_converter_public_trades.py +++ b/tests/data/test_converter_public_trades.py @@ -90,6 +90,7 @@ def test_public_trades_mock_populate_dataframe_with_trades__check_orderflow( config = { "timeframe": "5m", "orderflow": { + "max_candles": 1500, "scale": 0.005, "imbalance_volume": 0, "imbalance_ratio": 3, @@ -200,6 +201,7 @@ def test_public_trades_trades_mock_populate_dataframe_with_trades__check_trades( config = { "timeframe": "5m", "orderflow": { + "max_candles": 1500, "scale": 0.5, "imbalance_volume": 0, "imbalance_ratio": 3, @@ -355,6 +357,27 @@ def test_public_trades_binned_big_sample_list(public_trades_list): assert 52.7199999 == pytest.approx(df["delta"].iat[0]) # delta +def test_public_trades_config_max_trades( + default_conf, populate_dataframe_with_trades_dataframe, populate_dataframe_with_trades_trades +): + dataframe = populate_dataframe_with_trades_dataframe.copy() + trades = populate_dataframe_with_trades_trades.copy() + default_conf["exchange"]["use_public_trades"] = True + orderflow_config = { + "timeframe": "5m", + "orderflow": { + "max_candles": 1, + "scale": 0.005, + "imbalance_volume": 0, + "imbalance_ratio": 3, + "stacked_imbalance_range": 3, + }, + } + + df = populate_dataframe_with_trades(default_conf | orderflow_config, dataframe, trades) + assert df.delta.count() == 1 + + def test_public_trades_testdata_sanity( candles, public_trades_list,