Files
managing-apps/src/Managing.Domain/Indicators/Context/BollingerBandsVolatilityProtection.cs
Oda 9d536ea49e Refactoring TradingBotBase.cs + clean architecture (#38)
* Refactoring TradingBotBase.cs + clean architecture

* Fix basic tests

* Fix tests

* Fix workers

* Fix open positions

* Fix closing position stucking the grain

* Fix comments

* Refactor candle handling to use IReadOnlyList for chronological order preservation across various components
2025-12-01 19:32:06 +07:00

73 lines
3.1 KiB
C#

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)
{
}
/// <summary>
/// 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
/// </summary>
/// <param name="bbResults">List of Bollinger Bands calculation results</param>
/// <param name="candles">Candles to process</param>
protected override void ProcessBollingerBandsSignals(List<BollingerBandsResult> bbResults, IReadOnlyList<Candle> 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);
}
}
}