freqtrade_origin/docs/advanced-orderflow.md

135 lines
5.6 KiB
Markdown
Raw Normal View History

2024-07-13 07:49:09 +00:00
# Orderflow data
2024-02-06 13:09:26 +00:00
2024-02-20 19:12:50 +00:00
This guide walks you through utilizing public trade data for advanced orderflow analysis in Freqtrade.
2024-02-06 13:09:26 +00:00
2024-06-06 15:37:54 +00:00
!!! Warning "Experimental Feature"
The orderflow feature is currently in beta and may be subject to changes in future releases. Please report any issues or feedback on the [Freqtrade GitHub repository](https://github.com/freqtrade/freqtrade/issues).
!!! Warning "Performance"
2024-07-12 18:00:46 +00:00
Orderflow requires raw trades data. This data is rather large, and can cause a slow initial startup, when freqtrade needs to download the trades data for the last X candles. Additionally, enabling this feature will cause increased memory usage. Please ensure to have sufficient resources available.
2024-02-06 13:09:26 +00:00
2024-02-20 19:12:50 +00:00
## Getting Started
2024-02-06 13:09:26 +00:00
2024-07-13 07:49:09 +00:00
### Enable Public Trades
In your `config.json` file, set the `use_public_trades` option to true under the `exchange` section.
2024-06-06 15:37:54 +00:00
2024-02-20 19:12:50 +00:00
```json
2024-02-06 13:09:26 +00:00
"exchange": {
...
"use_public_trades": true,
}
```
2024-06-06 15:37:54 +00:00
2024-07-13 07:49:09 +00:00
### Configure Orderflow Processing
Define your desired settings for orderflow processing within the orderflow section of config.json. Here, you can adjust factors like:
2024-02-20 19:12:50 +00:00
2024-06-24 15:21:29 +00:00
- `cache_size`: How many previous orderflow candles are saved into cache instead of calculated every new candle
2024-07-13 07:49:09 +00:00
- `max_candles`: Filter how many candles would you like to get trades data for.
2024-02-20 19:12:50 +00:00
- `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.
- `imbalance_ratio`: Filters out imbalances with a ratio (difference between ask and bid volume) lower than this value.
```json
2024-02-06 13:09:26 +00:00
"orderflow": {
2024-06-24 15:21:29 +00:00
"cache_size": 1000,
"max_candles": 1500,
2024-02-06 13:09:26 +00:00
"scale": 0.5,
"stacked_imbalance_range": 3, // needs at least this amount of imbalance next to each other
2024-03-16 18:03:56 +00:00
"imbalance_volume": 1, // filters out below
"imbalance_ratio": 3 // filters out ratio lower than
2024-02-06 13:09:26 +00:00
},
```
2024-02-20 19:12:50 +00:00
## Downloading Trade Data for Backtesting
2024-02-06 13:09:26 +00:00
2024-02-20 19:12:50 +00:00
To download historical trade data for backtesting, use the --dl-trades flag with the freqtrade download-data command.
2024-02-06 13:09:26 +00:00
2024-02-20 19:12:50 +00:00
```bash
2024-07-12 18:00:46 +00:00
freqtrade download-data -p BTC/USDT:USDT --timerange 20230101- --trading-mode futures --timeframes 5m --dl-trades
2024-02-06 13:09:26 +00:00
```
2024-07-13 07:49:09 +00:00
!!! Warning "Data availability"
Not all exchanges provide public trade data. For supported exchanges, freqtrade will warn you if public trade data is not available if you start downloading data with the `--dl-trades` flag.
2024-02-20 19:12:50 +00:00
## Accessing Orderflow Data
2024-02-06 13:09:26 +00:00
2024-02-20 19:12:50 +00:00
Once activated, several new columns become available in your dataframe:
2024-07-12 18:00:46 +00:00
2024-02-06 13:09:26 +00:00
``` python
2024-07-13 07:49:09 +00:00
dataframe["trades"] # Contains information about each individual trade.
dataframe["orderflow"] # Represents a footprint chart dataframe (see below)
dataframe["imbalances"] # Contains information about imbalances in the order flow.
dataframe["bid"] # Total bid volume
dataframe["ask"] # Total ask volume
dataframe["delta"] # Difference between ask and bid volume.
dataframe["min_delta"] # Minimum delta within the candle
dataframe["max_delta"] # Maximum delta within the candle
dataframe["total_trades"] # Total number of trades
dataframe["stacked_imbalances_bid"] # Price level of stacked bid imbalance
dataframe["stacked_imbalances_ask"] # Price level of stacked ask imbalance
2024-02-06 13:09:26 +00:00
```
2024-02-20 19:12:50 +00:00
You can access these columns in your strategy code for further analysis. Here's an example:
2024-06-06 15:37:54 +00:00
2024-02-06 13:09:26 +00:00
``` python
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
2024-02-20 19:12:50 +00:00
# Calculating cumulative delta
2024-06-06 15:37:54 +00:00
dataframe["cum_delta"] = cumulative_delta(dataframe["delta"])
2024-02-20 19:12:50 +00:00
# Accessing total trades
2024-06-06 15:37:54 +00:00
total_trades = dataframe["total_trades"]
2024-02-20 19:12:50 +00:00
...
2024-02-06 13:09:26 +00:00
def cumulative_delta(delta: Series):
cumdelta = delta.cumsum()
return cumdelta
```
2024-06-06 15:37:54 +00:00
2024-07-13 07:49:09 +00:00
### Footprint chart (`dataframe["orderflow"]`)
2024-02-13 10:27:25 +00:00
2024-02-20 19:12:50 +00:00
This column provides a detailed breakdown of buy and sell orders at different price levels, offering valuable insights into order flow dynamics. The scale parameter in your configuration determines the price bin size for this representation
2024-02-13 10:27:25 +00:00
2024-02-20 19:12:50 +00:00
The `orderflow` dataframe includes columns like:
2024-02-13 10:27:25 +00:00
2024-02-20 19:12:50 +00:00
- `bid_amount`: Total volume bought at each price level.
- `ask_amount`: Total volume sold at each price level.
- `bid`: Number of buy orders at each price level.
- `ask`: Number of sell orders at each price level.
- `delta`: Difference between ask and bid volume at each price level.
- `total_volume`: Total volume (ask amount + bid amount) at each price level.
- `total_trades`: Total number of trades (ask + bid) at each price level.
By leveraging these features, you can gain valuable insights into market sentiment and potential trading opportunities based on order flow analysis.
2024-07-13 07:49:09 +00:00
### Raw trades data (`dataframe["trades"]`)
Dataframe with the individual trades that occurred during the candle. This data can be used for more granular analysis of order flow dynamics.
The `trades` dataframe includes the following columns:
- `timestamp`: Timestamp of the trade.
- `date`: Date of the trade.
- `price`: Price of the trade.
- `amount`: Volume of the trade.
- `side`: Buy or sell.
- `id`: Unique identifier for the trade.
- `cost`: Total cost of the trade (price * amount).
### Imbalances (`dataframe["imbalances"]`)
Dataframe with information about imbalances in the order flow. An imbalance occurs when there is a significant difference between the ask and bid volume at a given price level.
The dataframe looks as follows - with price as index, and the corresponding bid and ask imbalance values as columns
``` output
bid_imbalance ask_imbalance
price
58130.0 False False
58130.5 False False
58131.0 False False
```