Add Bollinger Bands Volatility Protection indicator support

- Introduced BollingerBandsVolatilityProtection indicator in GeneticService with configuration settings for period and standard deviation (stdev).
- Updated ScenarioHelpers to handle creation and validation of the new indicator type.
- Enhanced CustomScenario, backtest, and scenario pages to include BollingerBandsVolatilityProtection in indicator lists and parameter mappings.
- Modified API and types to reflect the addition of the new indicator in relevant enums and mappings.
- Updated frontend components to support new parameters and visualization for Bollinger Bands.
This commit is contained in:
2025-11-25 02:12:57 +07:00
parent 3ec1da531a
commit 6376e13b07
21 changed files with 618 additions and 220 deletions

View File

@@ -44,6 +44,17 @@ Oversold Threshold: 20
Overbought Threshold: 80
```
**Bollinger Bands Example:**
```
Type: Context
Label: Bollinger Bands Volatility Protection
Core Logic: Uses the Bandwidth (distance between Upper and Lower bands) to measure market volatility and apply veto filters during extreme conditions.
Context Confidence Levels: Block signals when bandwidth is extremely high (>0.15) or low (<0.02), validate when normal (0.02-0.15).
Parameters:
Period (default: 20)
StDev (default: 2.0)
```
### Step 2: Determine Implementation Details
**Check Existing Indicators:**
@@ -103,13 +114,26 @@ public class {IndicatorName} : IndicatorBase
base(name, IndicatorType.{EnumName})
{
Signals = new List<LightSignal>();
// Initialize parameters
// Initialize parameters (e.g., Period, Multiplier, StDev)
}
// Implementation methods...
}
```
**For Bollinger Bands (use shared base):**
```csharp
public class {IndicatorName} : BollingerBandsBase
{
public {IndicatorName}(string name, int period, double stdev) :
base(name, IndicatorType.{EnumName}, period, stdev)
{
}
// Only implement ProcessBollingerBandsSignals method
}
```
**Shared Base Class Pattern (use only if mapping is shared):**
If another indicator uses the same candle result mapping, extend from a shared base class:
@@ -174,32 +198,69 @@ public enum IndicatorType
}
```
**Update IndicatorBase.cs:**
- Add any new parameter properties needed (e.g., `StDev` for Bollinger Bands)
**Update LightIndicator.cs:**
- Add any new parameter properties with proper Id attributes for Orleans serialization
- Update `LightToBase()` method to copy new properties
**Update IndicatorRequest.cs:**
- Add any new parameter properties to match LightIndicator
**Update ScenarioHelpers.cs:**
- Add case to `BuildIndicator()` method: `IndicatorType.{EnumName} => new {IndicatorName}(indicator.Name, {parameters})`
- Add case to `GetSignalType()` method: `IndicatorType.{EnumName} => SignalType.{Type}`
- Add parameters to `BuildIndicator()` overload if needed
- Add parameter validation in `BuildIndicator()` method switch statement
- Add new parameters to `BuildIndicator()` method signature if needed
- Update `BaseToLight()` method to copy all LightIndicator properties
**Update BacktestJobService.cs:**
- Update LightIndicator creation in bundle job creation to include all new properties
- Ensure all indicator parameters are properly mapped from requests
**Update GeneticService.cs:**
- Add default values to `DefaultIndicatorValues`: `[IndicatorType.{EnumName}] = new() { {param_mappings} }`
- Add parameter ranges to `IndicatorParameterRanges`: `[IndicatorType.{EnumName}] = new() { {param_ranges} }`
- Add parameter mapping to `IndicatorParamMapping`: `[IndicatorType.{EnumName}] = [{param_names}]`
- Update `TradingBotChromosome.GetSelectedIndicators()` to handle new parameters
**Update Frontend Files:**
*CustomScenario.tsx:*
- Add new parameters to indicator type definitions
- Update parameter input handling (float vs int parsing)
- Add default values for new parameters
*TradeChart.tsx (if applicable):*
- Add visualization logic for new indicator bands/lines
- Use appropriate colors and styles for differentiation
### Step 5: Test and Validate
**Compile Check:**
```bash
# Backend compilation
dotnet build
# Frontend compilation
cd src/Managing.WebApp && npm run build
```
**Basic Validation:**
- Verify indicator appears in GeneticService configurations
- Check that BuildIndicator methods work correctly
- Ensure proper SignalType assignment
- Verify LightIndicator serialization works (Orleans Id attributes)
- Check parameter validation in ScenarioHelpers.BuildIndicator
- Confirm frontend parameter handling works correctly
**Integration Test:**
- Create a simple backtest using the new indicator
- Verify signals are generated correctly
- Check parameter handling and validation
- Test frontend scenario creation with new parameters
- Verify chart visualization displays correctly (if applicable)
## Available Skender.Stock.Indicators
@@ -298,7 +359,7 @@ When implementing a new indicator, search the [Skender documentation](https://do
- **Moving Averages**: Period 5-300 (shorter = responsive, longer = smooth)
- **Oscillators**: Period 5-50 (RSI: 14, Stoch: 14, CCI: 20)
- **Trend Following**: Period 10-50, Multiplier 1.0-5.0
- **Volatility**: Period 5-50, Standard Deviations 1.0-3.0
- **Volatility**: Period 5-50, Standard Deviations (StDev) 1.0-3.0 (Bollinger Bands)
- **Volume**: Period 5-50 (OBV uses no period)
**Testing Parameters:**
@@ -343,6 +404,7 @@ When creating your `Candle{Indicator}` mapping class, include all relevant resul
- `GetStc(cyclePeriod, fastPeriod, slowPeriod)``StcResult` - Used in STC, Lagging STC
- `GetStdDev(period)``StdDevResult` - Used in StDev Context
- `GetChandelier(period, multiplier, type)``ChandelierResult` - Used in Chandelier Exit
- `GetBollingerBands(period, stdev)``BollingerBandsResult` - Used in Bollinger Bands indicators
- `GetAdx(period)``AdxResult` - Used in SuperTrend Cross EMA
**Available But Unused:**
@@ -390,6 +452,31 @@ public class StochasticFiltered : StochasticBase { /* Specific logic */ }
public class AnotherStochasticIndicator : StochasticBase { /* Specific logic */ }
```
**Bollinger Bands Example (Implemented):**
```csharp
public abstract class BollingerBandsBase : IndicatorBase
{
protected double Stdev { get; set; }
protected BollingerBandsBase(string name, IndicatorType type, int period, double stdev)
: base(name, type)
{
Stdev = stdev;
Period = period;
}
protected virtual IEnumerable<CandleBollingerBands> MapBollingerBandsToCandle(
IEnumerable<BollingerBandsResult> bbResults, IEnumerable<Candle> candles)
{
// Shared Bollinger Bands mapping logic with all properties
// (Sma, UpperBand, LowerBand, PercentB, ZScore, Width)
}
}
public class BollingerBandsPercentBMomentumBreakout : BollingerBandsBase { /* %B momentum logic */ }
public class BollingerBandsVolatilityProtection : BollingerBandsBase { /* Volatility protection logic */ }
```
**When NOT to Use:**
- Indicators have different result types (Stoch vs StochRsi)
- Mapping logic differs significantly
@@ -410,6 +497,13 @@ public class AnotherStochasticIndicator : StochasticBase { /* Specific logic */
- [ ] Constructor parameters match IIndicator interface
- [ ] SignalType correctly assigned
- [ ] Enum added to IndicatorType
- [ ] BuildIndicator methods updated
- [ ] GeneticService configurations updated
- [ ] Compiles without errors
- [ ] IndicatorBase.cs properties added if needed
- [ ] LightIndicator.cs properties added with proper Id attributes
- [ ] IndicatorRequest.cs properties added
- [ ] ScenarioHelpers.cs BuildIndicator and BaseToLight methods updated
- [ ] BacktestJobService.cs LightIndicator mapping updated
- [ ] GeneticService.cs configurations updated (defaults, ranges, mappings)
- [ ] Frontend CustomScenario.tsx updated for new parameters
- [ ] Frontend TradeChart.tsx updated for visualization if needed
- [ ] Compiles without errors (backend and frontend)
- [ ] TypeScript types properly aligned