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 PZOStrategy : WealthScript { private StrategyParameter paramPeriod; public PZOStrategy() { paramPeriod = CreateParameter("PZO Period", 14, 2, 252, 2); } protected override void Execute() { int period = paramPeriod.ValueInt; DataSeries R = new DataSeries( Bars, "R" ); DataSeries TV = EMA.Series( Close, period, EMACalculation.Modern ); DataSeries PZO = new DataSeries( Bars, "PZO" ); ADX adx = ADX.Series( Bars,14 ); EMA ema = EMA.Series( Close, 60, EMACalculation.Modern ); for(int bar = period; bar < Bars.Count; bar++) { R[bar] = Math.Sign( Close[bar] - Close[bar-1] ) * Close[bar]; } DataSeries VP = EMA.Series( R, period, EMACalculation.Modern ); for(int bar = period; bar < Bars.Count; bar++) { if( TV[bar] != 0 ) PZO[bar] = 100 * VP[bar] / TV[bar]; } ChartPane pzoPane = CreatePane( 30, true, true ); PlotSeriesOscillator( pzoPane, PZO, 60, -60, Color.Red, Color.Blue, Color.Black, LineStyle.Solid, 1 ); DrawHorzLine( pzoPane, 60, Color.DarkGreen, LineStyle.Dotted, 2 ); DrawHorzLine( pzoPane, -60, Color.Red, LineStyle.Dotted, 2 ); DrawHorzLine( pzoPane, 40, Color.DarkGreen, LineStyle.Solid, 1 ); DrawHorzLine( pzoPane, -40, Color.Red, LineStyle.Solid, 1 ); DrawHorzLine( pzoPane, 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, pzoPane, PZO, 4d); PlotSeries(divPane, pd, Color.Blue, LineStyle.Solid, 2); DataSeries td = sh.PlotTroughDivergence(3, PricePane, Low, 4d, pzoPane, PZO, 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 = adx[bar] > 18 && Close[bar] > ema[bar]; bool bear = adx[bar] > 18 && Close[bar] < ema[bar]; bool osc = adx[bar] < 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( PZO[bar] > 60 && TurnDown( bar, PZO ) || ( Close[bar] < ema[bar] && PZO[bar] < 0 ) || (pd[bar] <= -1.0 && PZO[bar] < 40.0) ) SellAtMarket( bar+1, p, "trend sell" ); } else if( p.EntrySignal.ToLower().Contains("buy nontrend") ) { if( PZO[bar] > 40.0 ) { if( adx[bar] > 18 ) SellAtMarket( bar+1, p, "nontrend sell rule #1" ); } else { if( PZO[bar] < -5 ) SellAtMarket( bar+1, p, "nontrend sell rule #2" ); } } } else { if( p.EntrySignal.ToLower().Contains("downtrend") ) { if( PZO[bar] < -60 && TurnUp( bar, PZO ) || ( Close[bar] > ema[bar] && PZO[bar] > 0 ) || (td[bar] <= -1.0 && PZO[bar] > -40.0) ) CoverAtMarket( bar+1, p, "trend cover" ); } else if( p.EntrySignal.ToLower().Contains("short nontrend") ) { if( PZO[bar] < -40.0 ) { if( adx[bar] > 18 ) CoverAtMarket( bar+1, p, "nontrend cover rule #1" ); } else { if( PZO[bar] < -5 && CrossOver( bar, PZO, 15 ) ) CoverAtMarket( bar+1, p, "nontrend cover rule #2" ); } } } } else { bool buy = bull && ( CrossOver( bar, PZO, -40 ) || CrossOver( bar, PZO, 0 ) ); bool shrt = bear && ( CrossUnder( bar, PZO, 40 ) || CrossUnder( bar, PZO, 0 ) ); bool nt_buy = osc && (CrossOver( bar, PZO, -40 ) || CrossOver( bar, PZO, 15 )); bool nt_shrt = osc && (CrossUnder( bar, PZO, 40 ) || CrossUnder( bar, PZO, -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" ); } } } } } }