Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Why error in log with NBarsUpEvent in for loop

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

    Why error in log with NBarsUpEvent in for loop

    Hi, I am trying to check if there was an NBarsUp or -Down event during the last 7 bars. I tried this, but it doesn't work and I get an error message in the log: "Error on calling 'OnBarUpdate' method [...] You are accessing an index with a value that is invalid since its out of range.":
    Code:
      protected override void OnBarUpdate()
            {
                if (CurrentBar < 45)
                    return;            
                
                bool bNBarsUpEvent = false;
                bool bNBarsDownEvent = false;
                
                for (int i = 0; i <= 7; i++)
                {
                    double valueUp = NBarsUp(nBarsPeriod, true, true, true)[i]; 
                    double valueDown = NBarsDown(nBarsPeriod, true, true, true)[i]; 
    
                    if (valueUp == 1)                
                    {
                        bNBarsUpEvent = true;    
                        BackColor = Color.Green;                        
                    }                
                    
                    if (valueDown == 1)                
                    {
                        bNBarsDownEvent = true;    
                        BackColor = Color.Red;                        
                    }            
                }            
            }
    I don't understand why it's not working.

    #2
    Hello Lonneman,

    I would first try using our built in method CountIf() for this. Example usage below- checks if there has been any occurrence of NBarsup 3 within the last 7 bars and marks with a diamond.


    if (CountIf(delegate {return NBarsUp(3, true, true, true)[0] == 1;}, 7) > 0)
    {
    DrawDiamond("" + CurrentBar, true, 0, High[0], Color.Red);
    }
    Ryan M.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mongo, Today, 11:05 AM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Tim-c, Today, 10:58 AM
    1 response
    2 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by traderqz, Yesterday, 09:06 AM
    4 responses
    24 views
    0 likes
    Last Post traderqz  
    Started by traderqz, Today, 12:06 AM
    4 responses
    7 views
    0 likes
    Last Post traderqz  
    Started by f.saeidi, Today, 10:19 AM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Working...
    X