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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    132 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    65 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X