Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Changing Bars.CurrentBar - is it safe?

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

    Changing Bars.CurrentBar - is it safe?

    Due to logic of my indicator sometimes I need to move back to the history and recalculate my indicator values again

    The simplest way how to do it is to change value of Bars.CurrentBar to bar i need to recalculate and then run my calculation again. Something like this:

    Code:
                    
    var aBarsCurrentBar = Bars.CurrentBar;
    try
    {
       Bars.CurrentBar = bar_I_need_to_recalculate;
       Call my calculation engine here
    }
    finally
    {
      //restoring  current bar
      Bars.CurrentBar = aBarsCurrentBar;
    }
    Everything works great, but considering Ninja's multithreading, i'm not sure, that changing of the current bar is safe for other indicators / strategies. Maybe, I need to lock Bars object for the time I'm changing its current bar? Or maybe I need to use some other syncronization methods?

    #2
    Hello rfsettling,

    Thank you for your note.

    Setting the value of CurrentBar would not be supported and could introduce issues.

    Have you considered saving your indicator values to a data series for which you could reference at a future point in time? For example I included an indicator which divides two SMA’s and saves their values to a series, for which at any point in the future you could call the value of that that series, x values ago.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      I need to recalculate previous values, not current (which is [0]). That's a point.
      Let's consider example

      I have some formula to calculate current value of indicator:
      Value[0]=some_function()

      At some time i decide that some previos bar (for example, [10]) must be re-calculated, so, I set CurrentBar=CurrentBar-10 and call my calculation routime once again and then return CurrentBar value to normal. Everything works well, but I'm not sure, whether other indicators at this moment would use normal CurrentBar or my changed current bar. This bothers me

      Comment


        #4
        Hello rfsettling,

        Thank you for your response.

        As my colleague stated setting the CurrentBar is not supported.

        Instead you just need to reference bars you wish to use to recalculate values and then set the values of your Series to those at the needed bar.

        For example, let's say you wish to recalculate the values from 10 bars ago. In my example below I am recalculating the High minus the Low from 10 bars ago.
        Code:
        Value[10] = (High[10] - Low[10]);
        Please let me know if you have any questions.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        576 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        334 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        553 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X