using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components; // Divergence Between Two DataSeries (Detect, Plot) namespace WealthLab.Strategies { public class VZOStrategy : WealthScript { private StrategyParameter paramPeriod; public VZOStrategy() { paramPeriod = CreateParameter("VZO Period", 14, 2, 252, 2); } protected override void Execute() { int period = paramPeriod.ValueInt; DataSeries R = new DataSeries( Bars, "R" ); DataSeries TV = EMA.Series( Volume, period, EMACalculation.Modern ); DataSeries VZO = new DataSeries( Bars, "VZO" ); ADX adx = ADX.Series( Bars,14 ); EMA ema = EMA.Series( Close, 60, EMACalculation.Modern ); for(int bar = period; bar < Bars.Count; bar++) { Rbar = Math.Sign( Closebar - Closebar-1 ) * Volumebar; } DataSeries VP = EMA.Series( R, period, EMACalculation.Modern ); for(int bar = period; bar < Bars.Count; bar++) { if( TVbar != 0 ) VZObar = 100 * VPbar / TVbar; } ChartPane vzoPane = CreatePane( 30, true, true ); PlotSeriesOscillator( vzoPane, VZO, 60, -60, Color.Red, Color.Blue, Color.Black, LineStyle.Solid, 1 ); DrawHorzLine( vzoPane, 60, Color.DarkGreen, LineStyle.Dotted, 2 ); DrawHorzLine( vzoPane, -60, Color.Red, LineStyle.Dotted, 2 ); DrawHorzLine( vzoPane, 40, Color.DarkGreen, LineStyle.Solid, 1 ); DrawHorzLine( vzoPane, -40, Color.Red, LineStyle.Solid, 1 ); DrawHorzLine( vzoPane, 0, Color.DarkBlue, LineStyle.Solid, 1 ); ChartPane divPane = CreatePane( 30, true, true ); SeriesHelper sh = new SeriesHelper(this); DataSeries pd = sh.PlotPeakDivergence(3, PricePane, High, 4d, vzoPane, VZO, 4d); PlotSeries(divPane, pd, Color.Blue, LineStyle.Solid, 2); DataSeries td = sh.PlotTroughDivergence(3, PricePane, Low, 4d, vzoPane, VZO, 4d); PlotSeries(divPane, td, Color.Red, LineStyle.Solid, 2); ChartPane adxPane = CreatePane( 30, true, true ); PlotSeries(adxPane, adx, Color.Purple, LineStyle.Histogram, 2); DrawHorzLine(adxPane, 18, Color.Red, LineStyle.Dashed, 2 ); PlotSeries(PricePane, ema, Color.Blue, LineStyle.Solid, 1); int start = Math.Max(adx.FirstValidValue,period); start = Math.Max( start, ema.FirstValidValue ); for(int bar = start; bar < Bars.Count; bar++) { bool bull = adxbar > 18 && Closebar > emabar; bool bear = adxbar > 18 && Closebar < emabar; bool osc = adxbar < 18; if( bull ) SetBackgroundColor( bar, Color.FromArgb( 30, Color.Blue ) ); if( bear ) SetBackgroundColor( bar, Color.FromArgb( 30, Color.Red ) ); if( osc ) SetBackgroundColor( bar, Color.FromArgb( 30, Color.Green ) ); if (IsLastPositionActive) { Position p = LastPosition; if( p.PositionType == PositionType.Long ) { if( p.EntrySignal.ToLower().Contains("uptrend") ) { if( VZObar > 60 && TurnDown( bar, VZO ) || ( Closebar < emabar && VZObar < 0 ) || (pdbar <= -1.0 && VZObar < 40.0) ) SellAtMarket( bar+1, p, "trend sell" ); } else if( p.EntrySignal.ToLower().Contains("buy nontrend") ) { if( VZObar > 40.0 ) { if( adxbar > 18 ) SellAtMarket( bar+1, p, "nontrend sell rule #1" ); } else { if( VZObar < -5 ) SellAtMarket( bar+1, p, "nontrend sell rule #2" ); } } } else { if( p.EntrySignal.ToLower().Contains("downtrend") ) { if( VZObar < -60 && TurnUp( bar, VZO ) || ( Closebar > emabar && VZObar > 0 ) || (tdbar <= -1.0 && VZObar > -40.0) ) CoverAtMarket( bar+1, p, "trend cover" ); } else if( p.EntrySignal.ToLower().Contains("short nontrend") ) { if( VZObar < -40.0 ) { if( adxbar > 18 ) CoverAtMarket( bar+1, p, "nontrend cover rule #1" ); } else { if( VZObar < -5 && CrossOver( bar, VZO, 15 ) ) CoverAtMarket( bar+1, p, "nontrend cover rule #2" ); } } } } else { bool buy = bull && ( CrossOver( bar, VZO, -40 ) || CrossOver( bar, VZO, 0 ) ); bool shrt = bear && ( CrossUnder( bar, VZO, 40 ) || CrossUnder( bar, VZO, 0 ) ); bool nt_buy = osc && (CrossOver( bar, VZO, -40 ) || CrossOver( bar, VZO, 15 )); bool nt_shrt = osc && (CrossUnder( bar, VZO, 40 ) || CrossUnder( bar, VZO, -5 )); if( buy ) { SetBarColor( bar, Color.Blue ); BuyAtMarket( bar+1, "buy uptrend" ); } else if( shrt ) { SetBarColor( bar, Color.Red ); ShortAtMarket( bar+1, "short downtrend" ); } else if( nt_buy ) { SetBarColor( bar, Color.Cyan ); BuyAtMarket( bar+1, "buy nontrend" ); } else if( nt_shrt ) { SetBarColor( bar, Color.Orange ); ShortAtMarket( bar+1, "short nontrend" ); } } } } } }