Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Newbie question

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

    Newbie question

    Hi,

    Just starting out with ninja C#. I have writen the following code,

    {
    if((High[0] < High[1]) && (High[1] > High[2]));

    DrawDot("high", true, 1, High[1], Color.Black);
    Print("Condition True");
    }


    and am getting the following error,


    Error on calling 'OnBarUpdate' method for indicator 'HighsTest' on bar 0: Object reference not set to an instance of an object.

    #2
    Originally posted by GKonheiser View Post
    Hi,

    Just starting out with ninja C#. I have writen the following code,

    {
    if((High[0] < High[1]) && (High[1] > High[2]));

    DrawDot("high", true, 1, High[1], Color.Black);
    Print("Condition True");
    }


    and am getting the following error,


    Error on calling 'OnBarUpdate' method for indicator 'HighsTest' on bar 0: Object reference not set to an instance of an object.
    You cannot call High[1] for the first bar, because the first bar has no bar that preceeds it. You can only run your code starting with the second bar.

    Code:
    {
         if(CurrentBar == 0)
              return;
         if(High[0] < High[1]) && (High[1] > High[2])
         {   
                DrawDot("high", true, 1, High[1], Color.Black);
                Print("Condition True");
         }
    }
    Further pay attention what you do with your brackets and semicolons. The line

    Code:
    if((High[0] < High[1]) && (High[1] > High[2]));
    is just an empty statement, which translates to "if the condition is true, do nothing".

    The code

    Code:
    {
              if((High[0] < High[1]) && (High[1] > High[2]))  // semicolon removed
            
                DrawDot("high", true, 1, High[1], Color.Black);
                Print("Condition True");
    }
    is incorrect, because you forgot to set the bracket to include the print line. Without the bracket the print line will be executed all the time, as it is not linked to the condition.

    Comment


      #3
      Thanks that's a big help. I'm trying to build an indicator form the ground up that effectively plots Highs and Lows and this is the first little step. ; )

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      152 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      87 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      131 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      127 views
      1 like
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      106 views
      0 likes
      Last Post CarlTrading  
      Working...
      X