I found this great DynamicSRLines indicator, it's using pivot points to draw SnR zones/lines. However, it doesn't work/loop continuously. It has MaxLookBackBars variable that also works as a stop. What I would like to achieve is that, use this MaxLookBackBars variable to draw SnR lines and then repeat the code, so that the indicator is going to draw a new SnR zone after getting the new MaxLookBackBars(50bars e.g).
I would be very grateful if someone would help me or point me in the right direction.
protected override void OnBarUpdate() { if ( CurrentBar <= MaxLookBackBars ) return; if ( LastBar != CurrentBar ) { x = HighestBar(High, (PivotStrength*2)+1); Print("x on: " + x); if ( x == PivotStrength ) { listEntry.Type = +1; listEntry.Price = High[x]; listEntry.Bar = CurrentBar - x; pivot.Add(listEntry); if ( pivot.Count > MaxLookBackBars ) { pivot.RemoveAt(0); } } x = LowestBar(Low, (PivotStrength*2)+1); if ( x == PivotStrength ) { listEntry.Type = -1; listEntry.Price = Low[x]; listEntry.Bar = CurrentBar - x; pivot.Add(listEntry); if ( pivot.Count > MaxLookBackBars ) { pivot.RemoveAt(0); } } if ( CurrentBar >= Bars.Count-2 && CurrentBar > MaxLookBackBars ) { p = Close[0]; p1 = 0; str = ""; for ( level=1; level <= MaxLevels ; level++) { p = get_sr_level (p,+1); Draw.Rectangle(this, "resZone"+level, false, 0, p-((ZoneTickSize*TickSize)/2), MaxLookBackBars, p+((ZoneTickSize*TickSize)/2), ColorAbove, ColorAbove, 30 ); Draw.Line (this, "resLine"+level, false, 0, p-((ZoneTickSize*TickSize)/2), MaxLookBackBars, p-((ZoneTickSize*TickSize)/2), ColorAbove, DashStyleHelper.Solid, 1); str = ( p == p1 ) ? str+" L"+level : "L"+level; Draw.Text (this, "resTag"+level, false , "\t "+str , -5 , p , 0, ColorAbove, boldFont, TextAlignment.Right, Brushes.Transparent , Brushes.Transparent , 0 ); p1 = p; p += (PivotTickDiff*TickSize); } p = Close[0]; p1 = 0; str = ""; for ( level = 1; level <= MaxLevels; level++) { p = get_sr_level (p, -1); Draw.Rectangle(this, "supZone"+level, false, 0, p-((ZoneTickSize*TickSize)/2), MaxLookBackBars, p+((ZoneTickSize*TickSize)/2), ColorBelow, ColorBelow, 30 ); Draw.Line (this, "supLine"+level, false, 0, p-((ZoneTickSize*TickSize)/2), MaxLookBackBars, p-((ZoneTickSize*TickSize)/2), ColorBelow, DashStyleHelper.Solid, 1); str = ( p == p1 ) ? str+" L"+level : "L"+level; Draw.Text (this, "supTag"+level, false , "\t "+str , -5 , p , 0, ColorBelow, boldFont, TextAlignment.Right, Brushes.Transparent , Brushes.Transparent , 0 ); p1 = p; p -= (PivotTickDiff*TickSize); } } } LastBar= CurrentBar; } double get_sr_level ( double refPrice, int pos) { int i=0, j=0; double levelPrice=0; if ( CurrentBar >= Bars.Count-2 && CurrentBar > MaxLookBackBars ) { maxCountAbove=0; maxCountBelow=0; pxAbove=-1; pxBelow=-1; for ( i = pivot.Count-1; i >= 0; i--) { countAbove = 0; countBelow = 0; if ( pivot[i].Bar < CurrentBar-MaxLookBackBars ) break; for ( j=0; j < pivot.Count-1; j++) { if ( pivot[j].Bar > CurrentBar-MaxLookBackBars ) { if ( pos > 0 && pivot[i].Price >= refPrice ) { if( Math.Abs(pivot[i].Price-pivot[j].Price)/TickSize <= PivotTickDiff ) { countAbove++; } if ( countAbove > maxCountAbove) { maxCountAbove = countAbove; levelPrice = pivot[i].Price; pxAbove = i; } } else if ( pos < 0 && pivot[i].Price <= refPrice ) { if ( Math.Abs(pivot[i].Price-pivot[j].Price)/TickSize <= PivotTickDiff) { countBelow++; } if ( countBelow > maxCountBelow ) { maxCountBelow = countBelow; levelPrice = pivot[i].Price; pxBelow = i; } } } } } if ( pos > 0 ) { levelPrice = ( pxAbove >= 0 ) ? pivot[pxAbove].Price : High[HighestBar(High,MaxLookBackBars)]; } if ( pos < 0 ) { levelPrice = ( pxBelow >= 0 ) ? pivot[pxBelow].Price : Low[LowestBar(Low,MaxLookBackBars)]; } } return Instrument.MasterInstrument.RoundToTickSize( levelPrice ); }
Comment