Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting value of previous bar

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

    Getting value of previous bar

    I have an assigned variable

    double marketCount = 0;

    The value marketCount gets updated after each bar closes.

    I want to access the value of the previous bar's marketCount to compare with the value of the current bar.

    How do I access that?

    I tried marketCount[1] but I get an error " Cannot apply Indexing with [] to an expression"

    Thanks!

    #2
    Hello Ousher,

    You would need to make hat variable into a Series if you wanted to be able to get the previous value using a BarsAgo. You can still use that variable to access the previous value so long as you set the variable after whatever logic uses it.

    Here is a simple example:

    Code:
    private double marketCount; 
    
    protected override void OnBarUpdate()
    {
    
        // use the variable first, on each bar this will be the previously set value
       if(marketCount > 0)
       {
    
       }
        //as the last action set the value to the new value for the current bar. 
        marketCount = Close[0];
    
        // you can use the current bars value down here if needed
    
    }
    If you wanted to use a BarsAgo instead you can create a series<double> https://ninjatrader.com/support/help...ightsub=series

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    47 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    23 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    33 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    51 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Working...
    X