using Managing.Domain.Candles;
using Managing.Domain.Strategies.Base;
using Skender.Stock.Indicators;
using static Managing.Common.Enums;
namespace Managing.Domain.Strategies.Context;
public class BollingerBandsVolatilityProtection : BollingerBandsBase
{
public BollingerBandsVolatilityProtection(string name, int period, double stdev) : base(name, IndicatorType.BollingerBandsVolatilityProtection, period, stdev)
{
}
///
/// Processes Bollinger Bands volatility protection signals based on bandwidth analysis.
/// This method applies a veto filter during periods of market extremes:
/// - Blocks signals when bandwidth is extremely high (dangerous expansion) or extremely low (dead market)
/// - Validates signals when bandwidth is normal to low (excluding squeeze extremes)
/// Bandwidth = (UpperBand - LowerBand) / Sma represents market volatility
///
/// List of Bollinger Bands calculation results
/// Candles to process
protected override void ProcessBollingerBandsSignals(List bbResults, IReadOnlyList candles)
{
var bbCandles = MapBollingerBandsToCandle(bbResults, candles.TakeLast(Period.Value)).ToList();
if (bbCandles.Count == 0)
return;
foreach (var currentCandle in bbCandles)
{
var width = currentCandle.Width ?? 0;
// Determine confidence based on width levels (bandwidth as % of SMA)
// Lower width = less volatility = higher confidence for trading
// Higher width = more volatility = lower confidence for trading
// Typical BB bandwidth values: 0.01-0.08 for most markets
Confidence confidence;
if (width >= 0.06) // High volatility - dangerous expansion (6%+)
{
// Block all signals during dangerous volatility expansion
confidence = Confidence.None;
}
else if (width <= 0.01) // Extremely low volatility - dead market/squeeze (1% or less)
{
// Block all signals in dead markets or extreme squeezes
confidence = Confidence.None;
}
else if (width <= 0.025) // High confidence - optimal for trading (1% - 2.5%)
{
// Validate signals during low volatility trending conditions
confidence = Confidence.High;
}
else if (width <= 0.035) // Medium confidence - acceptable for trading (2.5% - 3.5%)
{
// Validate signals during normal volatility conditions
confidence = Confidence.Medium;
}
else // Moderate high volatility (3.5% - 6%)
{
// Lower confidence during elevated but not extreme volatility
confidence = Confidence.Low;
}
// Context strategies always return TradeDirection.None
// The confidence level indicates the quality of market conditions
AddSignal(currentCandle, TradeDirection.None, confidence);
}
}
}