Refactor test_bbrsi.py and enhance strategy implementations

- Removed unused configuration for daily data and consolidated minute configuration into a single config dictionary.
- Updated plotting logic to dynamically handle different strategies, ensuring appropriate bands and signals are displayed based on the selected strategy.
- Improved error handling and logging for missing data in plots.
- Enhanced the Bollinger Bands and RSI classes to support adaptive parameters based on market regimes, improving flexibility in strategy execution.
- Added new CryptoTradingStrategy with multi-timeframe analysis and volume confirmation for better trading signal accuracy.
- Updated documentation to reflect changes in strategy implementations and configuration requirements.
This commit is contained in:
Ajasra
2025-05-22 17:57:04 +08:00
parent 934c807246
commit 3a9dec543c
6 changed files with 521 additions and 258 deletions

View File

@@ -8,7 +8,7 @@ The `Analysis` module includes classes for calculating common technical indicato
- **Relative Strength Index (RSI)**: Implemented in `cycles/Analysis/rsi.py`.
- **Bollinger Bands**: Implemented in `cycles/Analysis/boillinger_band.py`.
- **Trading Strategies**: Implemented in `cycles/Analysis/strategies.py`.
- Note: Trading strategies are detailed in `strategies.md`.
## Class: `RSI`
@@ -16,126 +16,91 @@ Found in `cycles/Analysis/rsi.py`.
Calculates the Relative Strength Index.
### Mathematical Model
1. **Average Gain (AvgU)** and **Average Loss (AvgD)** over 14 periods:
The standard RSI calculation typically involves Wilder's smoothing for average gains and losses.
1. **Price Change (Delta)**: Difference between consecutive closing prices.
2. **Gain and Loss**: Separate positive (gain) and negative (loss, expressed as positive) price changes.
3. **Average Gain (AvgU)** and **Average Loss (AvgD)**: Smoothed averages of gains and losses over the RSI period. Wilder's smoothing is a specific type of exponential moving average (EMA):
- Initial AvgU/AvgD: Simple Moving Average (SMA) over the first `period` values.
- Subsequent AvgU: `(Previous AvgU * (period - 1) + Current Gain) / period`
- Subsequent AvgD: `(Previous AvgD * (period - 1) + Current Loss) / period`
4. **Relative Strength (RS)**:
$$
\text{AvgU} = \frac{\sum \text{Upward Price Changes}}{14}, \quad \text{AvgD} = \frac{\sum \text{Downward Price Changes}}{14}
RS = \\frac{\\text{AvgU}}{\\text{AvgD}}
$$
2. **Relative Strength (RS)**:
5. **RSI**:
$$
RS = \frac{\text{AvgU}}{\text{AvgD}}
$$
3. **RSI**:
RSI = 100 - \\frac{100}{1 + RS}
$$
RSI = 100 - \frac{100}{1 + RS}
$$
Special conditions:
- If AvgD is 0: RSI is 100 if AvgU > 0, or 50 if AvgU is also 0 (neutral).
### `__init__(self, period: int = 14)`
### `__init__(self, config: dict)`
- **Description**: Initializes the RSI calculator.
- **Parameters**:
- `period` (int, optional): The period for RSI calculation. Defaults to 14. Must be a positive integer.
- **Parameters**:\n - `config` (dict): Configuration dictionary. Must contain an `'rsi_period'` key with a positive integer value (e.g., `{'rsi_period': 14}`).
### `calculate(self, data_df: pd.DataFrame, price_column: str = 'close') -> pd.DataFrame`
- **Description**: Calculates the RSI and adds it as an 'RSI' column to the input DataFrame. Handles cases where data length is less than the period by returning the original DataFrame with a warning.
- **Description**: Calculates the RSI (using Wilder's smoothing by default) and adds it as an 'RSI' column to the input DataFrame. This method utilizes `calculate_custom_rsi` internally with `smoothing='EMA'`.
- **Parameters**:\n - `data_df` (pd.DataFrame): DataFrame with historical price data. Must contain the `price_column`.\n - `price_column` (str, optional): The name of the column containing price data. Defaults to 'close'.
- **Returns**: `pd.DataFrame` - A copy of the input DataFrame with an added 'RSI' column. If data length is insufficient for the period, the 'RSI' column will contain `np.nan`.
### `calculate_custom_rsi(price_series: pd.Series, window: int = 14, smoothing: str = 'SMA') -> pd.Series` (Static Method)
- **Description**: Calculates RSI with a specified window and smoothing method (SMA or EMA). This is the core calculation engine.
- **Parameters**:
- `data_df` (pd.DataFrame): DataFrame with historical price data. Must contain the `price_column`.
- `price_column` (str, optional): The name of the column containing price data. Defaults to 'close'.
- **Returns**: `pd.DataFrame` - The input DataFrame with an added 'RSI' column (containing `np.nan` for initial periods where RSI cannot be calculated). Returns a copy of the original DataFrame if the period is larger than the number of data points.
- `price_series` (pd.Series): Series of prices.
- `window` (int, optional): The period for RSI calculation. Defaults to 14. Must be a positive integer.
- `smoothing` (str, optional): Smoothing method, can be 'SMA' (Simple Moving Average) or 'EMA' (Exponential Moving Average, specifically Wilder's smoothing when `alpha = 1/window`). Defaults to 'SMA'.
- **Returns**: `pd.Series` - Series containing the RSI values. Returns a series of NaNs if data length is insufficient.
## Class: `BollingerBands`
Found in `cycles/Analysis/boillinger_band.py`.
## **Bollinger Bands**
Calculates Bollinger Bands.
### Mathematical Model
1. **Middle Band**: 20-day Simple Moving Average (SMA)
1. **Middle Band**: Simple Moving Average (SMA) over `period`.
$$
\text{Middle Band} = \frac{1}{20} \sum_{i=1}^{20} \text{Close}_{t-i}
\\text{Middle Band} = \\text{SMA}(\\text{price}, \\text{period})
$$
2. **Upper Band**: Middle Band + 2 × 20-day Standard Deviation (σ)
2. **Standard Deviation (σ)**: Standard deviation of price over `period`.
3. **Upper Band**: Middle Band + `num_std` × σ
$$
\text{Upper Band} = \text{Middle Band} + 2 \times \sigma_{20}
\\text{Upper Band} = \\text{Middle Band} + \\text{num_std} \\times \\sigma_{\\text{period}}
$$
3. **Lower Band**: Middle Band 2 × 20-day Standard Deviation (σ)
4. **Lower Band**: Middle Band `num_std` × σ
$$
\text{Lower Band} = \text{Middle Band} - 2 \times \sigma_{20}
\\text{Lower Band} = \\text{Middle Band} - \\text{num_std} \\times \\sigma_{\\text{period}}
$$
For the adaptive calculation in the `calculate` method (when `squeeze=False`):
- **BBWidth**: `(Reference Upper Band - Reference Lower Band) / SMA`, where reference bands are typically calculated using a 2.0 standard deviation multiplier.
- **MarketRegime**: Determined by comparing `BBWidth` to a threshold from the configuration. `1` for sideways, `0` for trending.
- The `num_std` used for the final Upper and Lower Bands then varies based on this `MarketRegime` and the `bb_std_dev_multiplier` values for "trending" and "sideways" markets from the configuration, applied row-wise.
### `__init__(self, period: int = 20, std_dev_multiplier: float = 2.0)`
### `__init__(self, config: dict)`
- **Description**: Initializes the BollingerBands calculator.
- **Parameters**:\n - `config` (dict): Configuration dictionary. It must contain:
- `'bb_period'` (int): Positive integer for the moving average and standard deviation period.
- `'trending'` (dict): Containing `'bb_std_dev_multiplier'` (float, positive) for trending markets.
- `'sideways'` (dict): Containing `'bb_std_dev_multiplier'` (float, positive) for sideways markets.
- `'bb_width'` (float): Positive float threshold for determining market regime.
### `calculate(self, data_df: pd.DataFrame, price_column: str = 'close', squeeze: bool = False) -> pd.DataFrame`
- **Description**: Calculates Bollinger Bands and adds relevant columns to the DataFrame.
- If `squeeze` is `False` (default): Calculates adaptive Bollinger Bands. It determines the market regime (trending/sideways) based on `BBWidth` and applies different standard deviation multipliers (from the `config`) on a row-by-row basis. Adds 'SMA', 'UpperBand', 'LowerBand', 'BBWidth', and 'MarketRegime' columns.
- If `squeeze` is `True`: Calculates simpler Bollinger Bands with a fixed window of 14 and a standard deviation multiplier of 1.5 by calling `calculate_custom_bands`. Adds 'SMA', 'UpperBand', 'LowerBand' columns; 'BBWidth' and 'MarketRegime' will be `NaN`.
- **Parameters**:\n - `data_df` (pd.DataFrame): DataFrame with price data. Must include the `price_column`.\n - `price_column` (str, optional): The name of the column containing the price data. Defaults to 'close'.\n - `squeeze` (bool, optional): If `True`, calculates bands with fixed parameters (window 14, std 1.5). Defaults to `False`.
- **Returns**: `pd.DataFrame` - A copy of the original DataFrame with added Bollinger Band related columns.
### `calculate_custom_bands(price_series: pd.Series, window: int = 20, num_std: float = 2.0, min_periods: int = None) -> tuple[pd.Series, pd.Series, pd.Series]` (Static Method)
- **Description**: Calculates Bollinger Bands with a specified window, standard deviation multiplier, and minimum periods.
- **Parameters**:
- `period` (int, optional): The period for the moving average and standard deviation. Defaults to 20. Must be a positive integer.
- `std_dev_multiplier` (float, optional): The number of standard deviations for the upper and lower bands. Defaults to 2.0. Must be positive.
### `calculate(self, data_df: pd.DataFrame, price_column: str = 'close') -> pd.DataFrame`
- **Description**: Calculates Bollinger Bands and adds 'SMA' (Simple Moving Average), 'UpperBand', and 'LowerBand' columns to the DataFrame.
- **Parameters**:
- `data_df` (pd.DataFrame): DataFrame with price data. Must include the `price_column`.
- `price_column` (str, optional): The name of the column containing the price data (e.g., 'close'). Defaults to 'close'.
- **Returns**: `pd.DataFrame` - The original DataFrame with added columns: 'SMA', 'UpperBand', 'LowerBand'.
## Class: `Strategy`
Found in `cycles/Analysis/strategies.py`.
Implements various trading strategies using technical indicators.
### `__init__(self, config = None, logging = None)`
- **Description**: Initializes the Strategy class with configuration and logging.
- **Parameters**:
- `config` (dict): Configuration dictionary with strategy parameters. Must be provided.
- `logging` (logging object, optional): Logger for output messages. Defaults to None.
### `run(self, data, strategy_name)`
- **Description**: Executes a specified strategy on the provided data.
- **Parameters**:
- `data` (pd.DataFrame): DataFrame with price, indicator data, and market regime information.
- `strategy_name` (str): Name of the strategy to run. Currently supports "MarketRegimeStrategy".
- **Returns**: Tuple of (buy_condition, sell_condition) as pandas Series with boolean values.
### `no_strategy(self, data)`
- **Description**: Returns empty buy/sell conditions (all False).
- **Parameters**:
- `data` (pd.DataFrame): Input data DataFrame.
- **Returns**: Tuple of (buy_condition, sell_condition) as pandas Series with all False values.
### `rsi_bollinger_confirmation(self, rsi, window=14, std_mult=1.5)`
- **Description**: Calculates Bollinger Bands on RSI values for signal confirmation.
- **Parameters**:
- `rsi` (pd.Series): Series containing RSI values.
- `window` (int, optional): The period for the moving average. Defaults to 14.
- `std_mult` (float, optional): Standard deviation multiplier for bands. Defaults to 1.5.
- **Returns**: Tuple of (oversold_condition, overbought_condition) as pandas Series with boolean values.
### `MarketRegimeStrategy(self, data)`
- **Description**: Advanced strategy combining Bollinger Bands, RSI, volume analysis, and market regime detection.
- **Parameters**:
- `data` (pd.DataFrame): DataFrame with price data, technical indicators, and market regime information.
- **Returns**: Tuple of (buy_condition, sell_condition) as pandas Series with boolean values.
#### Strategy Logic
This strategy adapts to different market conditions:
**Trending Market (Breakout Mode):**
- Buy: Price < Lower Band ∧ RSI < 50 ∧ Volume Spike (≥1.5× 20D Avg)
- Sell: Price > Upper Band ∧ RSI > 50 ∧ Volume Spike
**Sideways Market (Mean Reversion):**
- Buy: Price ≤ Lower Band ∧ RSI ≤ 40
- Sell: Price ≥ Upper Band ∧ RSI ≥ 60
When `SqueezeStrategy` is enabled, additional confirmation using RSI Bollinger Bands is required:
- For buy signals: RSI must be below its lower Bollinger Band
- For sell signals: RSI must be above its upper Bollinger Band
For sideways markets, volume contraction (< 0.7× 30D Avg) is also checked to avoid false signals.
- `price_series` (pd.Series): Series of prices.
- `window` (int, optional): The period for the moving average and standard deviation. Defaults to 20.
- `num_std` (float, optional): The number of standard deviations for the upper and lower bands. Defaults to 2.0.
- `min_periods` (int, optional): Minimum number of observations in window required to have a value. Defaults to `window` if `None`.
- **Returns**: `tuple[pd.Series, pd.Series, pd.Series]` - A tuple containing the Upper band, SMA, and Lower band series.

View File

@@ -1,43 +1,98 @@
# Optimized Bollinger Bands + RSI Strategy for Crypto Trading (Including Sideways Markets)
# Trading Strategies (`cycles/Analysis/strategies.py`)
This advanced strategy combines volatility analysis, momentum confirmation, and regime detection to adapt to Bitcoin's unique market conditions. Backtested on 2018-2025 BTC data, it achieved 58% annualized returns with 22% max drawdown.
This document outlines the trading strategies implemented within the `Strategy` class. These strategies utilize technical indicators calculated by other classes in the `Analysis` module.
## Class: `Strategy`
Manages and executes different trading strategies.
### `__init__(self, config: dict = None, logging = None)`
- **Description**: Initializes the Strategy class.
- **Parameters**:
- `config` (dict, optional): Configuration dictionary containing parameters for various indicators and strategy settings. Must be provided if strategies requiring config are used.
- `logging` (logging.Logger, optional): Logger object for outputting messages. Defaults to `None`.
### `run(self, data: pd.DataFrame, strategy_name: str) -> pd.DataFrame`
- **Description**: Executes a specified trading strategy on the input data.
- **Parameters**:
- `data` (pd.DataFrame): Input DataFrame containing at least price data (e.g., 'close', 'volume'). Specific strategies might require other columns or will calculate them.
- `strategy_name` (str): The name of the strategy to run. Supported names include:
- `"MarketRegimeStrategy"`
- `"CryptoTradingStrategy"`
- `"no_strategy"` (or any other unrecognized name will default to this)
- **Returns**: `pd.DataFrame` - A DataFrame containing the original data augmented with indicator values, and `BuySignal` and `SellSignal` (boolean) columns specific to the executed strategy. The structure of the DataFrame (e.g., daily, 15-minute) depends on the strategy.
### `no_strategy(self, data: pd.DataFrame) -> pd.DataFrame`
- **Description**: A default strategy that generates no trading signals. It can serve as a baseline or placeholder.
- **Parameters**:
- `data` (pd.DataFrame): Input data DataFrame.
- **Returns**: `pd.DataFrame` - The input DataFrame with `BuySignal` and `SellSignal` columns added, both containing all `False` values.
---
## **Adaptive Parameters**
### **Core Configuration**
| Indicator | Trending Market | Sideways Market |
|-----------------|-------------------------|-------------------------|
| **Bollinger** | 20 SMA, 2.5σ | 20 SMA, 1.8σ |
| **RSI** | 14-period, 30/70 | 14-period, 40/60 |
| **Confirmation**| Volume > 20% 30D Avg | Bollinger Band Width <5%|
## Implemented Strategies
## Strategy Components
### 1. `MarketRegimeStrategy`
### 1. Market Regime Detection
- **Description**: An adaptive strategy that combines Bollinger Bands and RSI, adjusting its parameters based on detected market regimes (trending vs. sideways). It operates on daily aggregated data (aggregation is performed internally).
- **Core Logic**:
- Calculates Bollinger Bands (using `BollingerBands` class) with adaptive standard deviation multipliers based on `MarketRegime` (derived from `BBWidth`).
- Calculates RSI (using `RSI` class).
- **Trending Market (Breakout Mode)**:
- Buy: Price < Lower Band ∧ RSI < 50 ∧ Volume Spike.
- Sell: Price > Upper Band ∧ RSI > 50 ∧ Volume Spike.
- **Sideways Market (Mean Reversion)**:
- Buy: Price ≤ Lower Band ∧ RSI ≤ 40.
- Sell: Price ≥ Upper Band ∧ RSI ≥ 60.
- **Squeeze Confirmation** (if `config["SqueezeStrategy"]` is `True`):
- Requires additional confirmation from RSI Bollinger Bands (calculated by `rsi_bollinger_confirmation` helper method).
- Sideways markets also check for volume contraction.
- **Key Configuration Parameters (from `config` dict)**:
- `bb_period`, `bb_width`
- `trending['bb_std_dev_multiplier']`, `trending['rsi_threshold']`
- `sideways['bb_std_dev_multiplier']`, `sideways['rsi_threshold']`
- `rsi_period`
- `SqueezeStrategy` (boolean)
- **Output DataFrame Columns (Daily)**: Includes input columns plus `SMA`, `UpperBand`, `LowerBand`, `BBWidth`, `MarketRegime`, `RSI`, `BuySignal`, `SellSignal`.
### 2. Entry Conditions
#### `rsi_bollinger_confirmation(self, rsi: pd.Series, window: int = 14, std_mult: float = 1.5) -> tuple`
***Trending Market (Breakout Mode):***
Buy: Price > Upper Band ∧ RSI > 50 ∧ Volume Spike (≥1.5× 20D Avg)
Sell: Price < Lower Band ∧ RSI < 50 ∧ Volume Spike
***Sideways Market (Mean Reversion):***
Buy: Price ≤ Lower Band ∧ RSI ≤ 40
Sell: Price ≥ Upper Band ∧ RSI ≥ 60
- **Description** (Helper for `MarketRegimeStrategy`): Calculates Bollinger Bands on RSI values for signal confirmation.
- **Parameters**:
- `rsi` (pd.Series): Series containing RSI values.
- `window` (int, optional): The period for the moving average. Defaults to 14.
- `std_mult` (float, optional): Standard deviation multiplier for bands. Defaults to 1.5.
- **Returns**: `tuple` - (oversold_condition, overbought_condition) as pandas Series (boolean).
### 2. `CryptoTradingStrategy`
### **Enhanced Signals with RSI Bollinger Squeeze**
*Signal Boost*: Requires both price and RSI to breach their respective bands.
- **Description**: A multi-timeframe strategy primarily designed for volatile assets like cryptocurrencies. It aggregates input data into 15-minute and 1-hour intervals for analysis.
- **Core Logic**:
- Aggregates data to 15-minute (`data_15m`) and 1-hour (`data_1h`) resolutions using `aggregate_to_minutes` and `aggregate_to_hourly` from `data_utils.py`.
- Calculates 15-minute Bollinger Bands (20-period, 2 std dev) and 15-minute EMA-smoothed RSI (14-period) using `BollingerBands.calculate_custom_bands` and `RSI.calculate_custom_rsi`.
- Calculates 1-hour Bollinger Bands (50-period, 1.8 std dev) using `BollingerBands.calculate_custom_bands`.
- **Signal Generation (on 15m timeframe)**:
- Buy Signal: Price ≤ Lower 15m Band ∧ Price ≤ Lower 1h Band ∧ RSI_15m < 35 ∧ Volume Confirmation.
- Sell Signal: Price ≥ Upper 15m Band ∧ Price ≥ Upper 1h Band ∧ RSI_15m > 65 ∧ Volume Confirmation.
- **Volume Confirmation**: Current 15m volume > 1.5 × 20-period MA of 15m volume.
- **Risk Management**: Calculates `StopLoss` and `TakeProfit` levels based on a simplified ATR (standard deviation of 15m close prices over the last 4 periods).
- Buy: SL = Price - 2 * ATR; TP = Price + 4 * ATR
- Sell: SL = Price + 2 * ATR; TP = Price - 4 * ATR
- **Key Configuration Parameters**: While this strategy uses fixed parameters for its core indicator calculations, the `config` object passed to the `Strategy` class might be used by helper functions or for future extensions (though not heavily used by the current `CryptoTradingStrategy` logic itself for primary indicator settings).
- **Output DataFrame Columns (15-minute)**: Includes resampled 15m OHLCV, plus `UpperBand_15m`, `SMA_15m`, `LowerBand_15m`, `RSI_15m`, `VolumeMA_15m`, `UpperBand_1h` (forward-filled), `LowerBand_1h` (forward-filled), `BuySignal`, `SellSignal`, `StopLoss`, `TakeProfit`.
---
## **Risk Management System**
### Volatility-Adjusted Position Sizing
$$ \text{Position Size} = \frac{\text{Capital} \times 0.02}{\text{ATR}_{14} \times \text{Price}} $$
## General Strategy Concepts (from previous high-level notes)
While the specific implementations above have their own detailed logic, some general concepts that often inspire trading strategies include:
**Key Adjustments:**
1. Use narrower Bollinger Bands (1.8σ) to avoid whipsaws
2. Require RSI confirmation within 40-60 range
3. Add volume contraction filter
- **Adaptive Parameters**: Adjusting indicator settings (like Bollinger Band width or RSI thresholds) based on market conditions (e.g., trending vs. sideways).
- **Multi-Timeframe Analysis**: Confirming signals on one timeframe with trends or levels on another (e.g., 15-minute signals confirmed by 1-hour context).
- **Volume Confirmation**: Using volume spikes or contractions to validate price-based signals.
- **Volatility-Adjusted Risk Management**: Using measures like ATR (Average True Range) to set stop-loss and take-profit levels, or to size positions dynamically.
These concepts are partially reflected in the implemented strategies, particularly in `MarketRegimeStrategy` (adaptive parameters) and `CryptoTradingStrategy` (multi-timeframe, volume confirmation, ATR-based risk levels).