using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using TASCIndicators;namespace WealthLab.Strategies { public class SVEVolatilityBands : WealthScript { private StrategyParameter paramBandAvg; private StrategyParameter paramVolaSum; private StrategyParameter paramDevFact; private StrategyParameter paramLowBAdj; public SVEVolatilityBands() { paramBandAvg = CreateParameter("Band average", 8, 2, 100, 1); paramVolaSum = CreateParameter("Vola Sum period", 13, 2, 100, 1); paramDevFact = CreateParameter("Deviation factor", 3.55, 2, 10, 0.1); paramLowBAdj = CreateParameter("Low Band Adj.", 0.9, 0.1, 3.0, 0.1); } protected override void Execute() { int average = paramBandAvg.ValueInt; int volperiod = paramVolaSum.ValueInt; double devfact = paramDevFact.Value; double lowbandadjust = paramLowBAdj.Value; EMACalculation m = EMACalculation.Modern; AveragePriceC TypicalPrice = AveragePriceC.Series( Bars ); EMA emaMedianAvg = EMA.Series(EMA.Series(TypicalPrice,average,m),average,m); SVEVolatilityBandUpper UpperBand = SVEVolatilityBandUpper.Series( Bars, average, volperiod, devfact, lowbandadjust ); SVEVolatilityBandLower LowerBand = SVEVolatilityBandLower.Series( Bars, average, volperiod, devfact, lowbandadjust ); for(int bar = GetTradingLoopStartBar(average * 3); bar < Bars.Count; bar++) { // Detect crossover/crossunder and store state in a variable bool xo = CrossOver(bar, Close, UpperBand); bool xu = CrossUnder(bar, Close, LowerBand); // The first trade if (Positions.Count == 0){ if ( xo ) BuyAtClose( bar ); else if( xu ) ShortAtClose( bar ); } // Subsequent trades else { Position p = LastPosition; if ( p.PositionType == PositionType.Long ) { if ( xu ) { SellAtClose( bar, p ); ShortAtClose( bar ); } } else if ( xo ) { CoverAtClose( bar, p ); BuyAtClose( bar ); } } } PlotSeries(PricePane, emaMedianAvg, Color.Silver, LineStyle.Solid, 2); PlotSeriesFillBand(PricePane, UpperBand, LowerBand, Color.Silver, Color.Transparent, LineStyle.Solid, 2); HideVolume(); } } }