Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple if statement not working in NinjaScript 8

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

    Simple if statement not working in NinjaScript 8

    Hi there,

    I'm trying to put a dot in the chart when a condition is met. For some reason the if statement is not working properly. I'm using NinjaTrader 8.

    In the protected override void OnBarUpdate() function, the following works if it's the only statement:
    Draw.Dot(this, "Bull" + CurrentBar, true, 0, Low[0] - 5, Brushes.Blue); // draw a dot under every price bar

    But if I try to compare the current to the previous bar like this, it's not working:
    if (Low[0] >= Low[1]) // draw a dot whenever the current low is above the previous low
    {
    Draw.Dot(this, "Bull" + CurrentBar, true, 0, Low[0] - 5, Brushes.Blue);
    }

    Any idea what's wrong here? Thanks in advance

    #2
    Hello ieee1294,

    Thanks for your post and welcome to the NinjaTrader forums!

    If your script is not working as expected, always check the "log" tab of the control center for any run time type error messages. If your script is not checking for the CurrentBar to be more than 1 then it is possible that your script would error on bar index error. Reference: https://ninjatrader.com/support/help...currentbar.htm
    So you might want to use:

    if (Currentbar < 1) return; // ensure there are two bars before going further

    If you want your draw statement to work on other instruments I would suggest changing Low[0] - 5 to Low[0] - 5 * TickSize TickSize is an instrument property that represents the smallest movement by that instrument, so in the example the dot would always be 5 ticks below the Low regardless of the applied instrument. Reference: https://ninjatrader.com/support/help.../?ticksize.htm

    Comment


      #3
      Paul, thanks for the extremely prompt reply - did not expect that on a New Year's Eve!

      Didn't know about the log tab until now, and thanks - it's like you said, I didn't check for CurrentBar. That fixed my issue. Thanks for the TickSize tip, I'm also using it already, just showing simplified code to get the point across easily.

      Hope you have a happy new year! Cheers.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      672 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      379 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      111 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      577 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      582 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X