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

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
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Ndakotan1313, 03-14-2024, 05:02 PM
    2 responses
    58 views
    0 likes
    Last Post blaise_code  
    Started by claxxical, 05-30-2017, 12:30 PM
    37 responses
    4,454 views
    0 likes
    Last Post Padan
    by Padan
     
    Started by SugarDefwebsite, Today, 02:18 AM
    0 responses
    4 views
    0 likes
    Last Post SugarDefwebsite  
    Started by usglucofreeze, Today, 01:19 AM
    0 responses
    7 views
    0 likes
    Last Post usglucofreeze  
    Started by f.saeidi, Today, 01:12 AM
    0 responses
    16 views
    0 likes
    Last Post f.saeidi  
    Working...
    X