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 NullPointStrategies, Today, 05:17 AM
    0 responses
    41 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    124 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    64 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X