using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using TASCIndicators;namespace WealthLab.Strategies { public class SVESmoothedVolatilityBands : WealthScript { private StrategyParameter paramBandAvg; private StrategyParameter paramMiddleLine; private StrategyParameter paramDevFact; private StrategyParameter paramLowBAdj; private StrategyParameter paramSqueezeLookback; public SVESmoothedVolatilityBands() { paramBandAvg = CreateParameter("Band average", 20, 2, 100, 1); paramMiddleLine = CreateParameter("Middle line period", 20, 2, 100, 1); paramDevFact = CreateParameter("Deviation factor", 2.4, 0.2, 5.0, 0.2); paramLowBAdj = CreateParameter("Low Band Adj.", 0.9, 0.1, 3.0, 0.1); paramSqueezeLookback = CreateParameter("Squeeze lookback", 200, 5, 150, 1 ); } protected override void Execute() { int average = paramBandAvg.ValueInt; int middleperiod = paramMiddleLine.ValueInt; double devfact = paramDevFact.Value; double lowbandadjust = paramLowBAdj.Value; int squeezelen = paramSqueezeLookback.ValueInt; // Smoothed Volatility Bands var TypicalPrice = AveragePriceC.Series( Bars ); var MedianAverage = WMA.Series(TypicalPrice, middleperiod); var HighChannel = SVESmoothedVolatilityBandUpper.Series( Bars, average, middleperiod, devfact, lowbandadjust ); var LowChannel = SVESmoothedVolatilityBandLower.Series( Bars, average, middleperiod, devfact, lowbandadjust ); SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(50, Color.Violet)); PlotSeriesFillBand(PricePane, HighChannel, LowChannel, Color.Blue, shadowBrush, LineStyle.Solid, 2); PlotSeries(PricePane, MedianAverage, Color.Blue, LineStyle.Solid, 1); HideVolume(); // Squeeze and expansion in SveVolatilityBands var PctWidth = ( ( HighChannel - LowChannel ) / MedianAverage ) * 100; PctWidth.Description = "% Width"; var PctWidthMax = Highest.Series( PctWidth, squeezelen ); var PctWidthMin = Lowest.Series( PctWidth, squeezelen ); ChartPane PctWPane = CreatePane( 20, true, true ); PlotSeries( PctWPane, PctWidth, Color.Blue, WealthLab.LineStyle.Solid, 2 ); PlotSeries( PctWPane, Lowest.Series( PctWidth, squeezelen ), Color.DarkGreen, LineStyle.Dashed, 2 ); PlotSeries( PctWPane, Highest.Series( PctWidth, squeezelen ), Color.DarkRed, LineStyle.Dashed, 2 ); for(int bar = GetTradingLoopStartBar(average); bar < Bars.Count; bar++) { if (IsLastPositionActive) { if( PctWidth[bar] >= PctWidthMax[bar] ) { SetBackgroundColor( bar, Color.FromArgb(30, Color.Red) ); if( Low[bar] <= Lowest.Series(Low, average)[bar] ) SellAtMarket(bar+1, LastPosition, "Vola expansion"); } } else { if( PctWidth[bar] <= PctWidthMin[bar] ) { SetBackgroundColor( bar, Color.FromArgb(30, Color.Green) ); if( Close[bar] >= SMA.Series(Close, squeezelen)[bar] ) BuyAtStop( bar+1, Highest.Series(High, average)[bar], "Vola contraction" ); } } } } } }