Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Good 'Ol On Bar Update Discrepancy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Good 'Ol On Bar Update Discrepancy

    The answer to this is staring me in the face and I can't see it... Classic error on bar zero accessing value of 5 when only 4 bars... This is such a ridiculously simple write-up... Where me go wrong? Sometimes it loads just fine, other times this error pops up...


    public class j2TrendBias : Indicator
    {
    private SMA SMA1;
    private NinjaTrader.NinjaScript.Indicators.j2.j2CumDelta j2CumDelta1;
    private NinjaTrader.NinjaScript.Indicators.j2.j2BarCounter j2BarCounter1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"This indicator identifies higher timeframe bias by price action. Current Beta is for use specifically on a 30 min chart";
    Name = "j2TrendBias";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    soundAlert = false;
    TickDistance = 4;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }

    else if (State == State.DataLoaded)
    {

    SMA1 = SMA(Close, 14);
    j2CumDelta1 = j2CumDelta(Close, Brushes.Red, Brushes.LimeGreen, Brushes.White, 1, 0, false);
    j2BarCounter1 = j2BarCounter(Close, true, Brushes.Gray, 14, 50, true);
    }


    }

    region Codesets
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 0)
    return;

    // Set 1 Opens = Lows
    if (
    // Open = Low
    ((Open[0] == Low[0])
    && (SMA1[0] > SMA1[1])
    && (j2CumDelta1.DeltaOpen[0] > -1000)
    && (j2BarCounter1[0] > 18)
    && (Close[0] > Open[0]))
    // Open = Low - 1 tick
    || ((Close[0] > Open[0])
    && (j2CumDelta1.DeltaOpen[0] > -1000)
    && (Low[0] == (Open[0] + (-1 * TickSize))
    && (j2BarCounter1[0] > 18)
    && (SMA1[0] > SMA1[1]))))
    {
    Draw.ArrowUp(this, "TrendBiasUp"+CurrentBar, false, 0, Low[0] - (TickDistance * TickSize), Brushes.Cyan); // Alert[0] = 1 maybe add later

    if (soundAlert == true)

    PlaySound(@"C:\Program Files\NinjaTrader 8\sounds\j2TrendAlert.wav");
    }

    // Set 2 Opens = Highs
    if (
    // Open = High
    ((Open[0] == High[0])
    && (SMA1[0] < SMA1[1])
    && (j2CumDelta1.DeltaOpen[0] < 1000)
    && (j2BarCounter1[0] > 18)
    && (Close[0] < Open[0]))
    // Open = High + 1 tick
    || ((Close[0] > Open[0])
    &&(j2CumDelta1.DeltaOpen[0] < 1000)
    && (High[0] == (Open[0] + (1 * TickSize))
    && (j2BarCounter1[0] > 18)
    && (SMA1[0] < SMA1[1]))))
    {
    Draw.ArrowDown(this, "TrendBiasDown"+CurrentBar, false, 0, High[0] + (TickDistance * TickSize), Brushes.Cyan);

    if (soundAlert == true)

    PlaySound(@"C:\Program Files\NinjaTrader 8\sounds\j2TrendAlert.wav");
    }

    // Set 3 Reversing bars with equal close/open & reversal bar up
    if (

    ((Open[0] == Close[1])
    && (Close[1] < Open[1])
    && (j2CumDelta1.DeltaOpen[0] > -1000)
    && (Close[0] > Open[0])
    && (Close[0] > Open[1])
    && (SMA1[0] > SMA1[1])
    && (j2BarCounter1[0] > 18)
    && (Low[0] > (Low[1] + (-3 * TickSize)))))

    {
    Draw.ArrowUp(this, "TrendBiasUp"+CurrentBar, false, 0, Low[0] - (TickDistance * TickSize), Brushes.Cyan);

    if (soundAlert == true)

    PlaySound(@"C:\Program Files\NinjaTrader 8\sounds\j2TrendAlert.wav");
    }

    // Set 4 Reversing bars with equal close/open & reversal bar down
    if (

    ((Open[0] == Close[1])
    && (Close[1] > Open[1])
    && (j2CumDelta1.DeltaOpen[0] < 1000)
    && (Close[0] < Open[0])
    && (Close[0] < Open[1])
    && (SMA1[0] < SMA1[1])
    && (j2BarCounter1[0] > 18)
    && (High[0] < (High[1] + (3 * TickSize)))))


    {
    Draw.ArrowDown(this, "TrendBiasDown"+CurrentBar, false, 0, High[0] + (TickDistance * TickSize), Brushes.Cyan);


    if (soundAlert == true)

    PlaySound(@"C:\Program Files\NinjaTrader 8\sounds\j2TrendAlert.wav");
    }

    // Set 5 Massive bars typically reversing and in negative CumDelta territory
    if (
    ((Close[0] > (Open[0] + (51 * TickSize))
    && (SMA1[0] > SMA1[1])
    && (j2BarCounter1[0] > 29)
    && (j2CumDelta1.DeltaOpen[0] < -1000))))

    {
    Draw.ArrowUp(this, "TrendBiasUp"+CurrentBar, false, 0, Low[0] - (TickDistance * TickSize), Brushes.Yellow);

    if (soundAlert == true)

    PlaySound(@"C:\Program Files\NinjaTrader 8\sounds\j2TrendAlert.wav");
    }



    // Set 6 Massive bars typically reversing and in positive CumDelta territory
    if (

    ((Close[0] < (Open[0] - (51 * TickSize))
    && (SMA1[0] < SMA1[1])
    && (j2BarCounter1[0] > 30)
    && (j2CumDelta1.DeltaOpen[0] > 1000))))


    {
    Draw.ArrowDown(this, "TrendBiasDown"+CurrentBar, false, 0, High[0] + (TickDistance * TickSize), Brushes.Yellow);


    if (soundAlert == true)

    PlaySound(@"C:\Program Files\NinjaTrader 8\sounds\j2TrendAlert.wav");
    }

    }
    #endregion
    region Properties
    [NinjaScriptProperty]
    [Display(Name="SoundAlert", Description="plays sound on firing signal", Order=1, GroupName="Parameters")]
    public bool soundAlert
    { get; set; }

    [NinjaScriptProperty]
    [Range(-100, int.MaxValue)]
    [Display(Name="TickDistance", Description="distance from top or bottom of candle for chart arrow position. ", Order=7, GroupName="Parameters")]
    public int TickDistance
    { get; set; }

    #endregion

    }
    }​

    #2
    Hello johnMoss,

    SMA1[1]

    You are attempting to call the previous bars information when there has only been one bar processed.

    Check that CurrentBars[0] is greater than 1.
    Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:


    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you Chelsea may I have another... Thank you Chelsea may I have another... Thank you Chelsea may I have another... Thank you Chelsea may I have another...

      Got it ... Cheers!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      559 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      546 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X