Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnStartUp() strange random behavior

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

    OnStartUp() strange random behavior

    Hi there,

    I just have experienced a bunch of similar issues with my live strategies ... here is a high-level description of the situation:

    Strategy S uses Indicator I
    S instantiates I during its OnStartUp()
    What usually happens, is that at the time I is instantiated, its I.OnStartUp() is executed before control returns to execute the remainder of S.OnStartUp()

    However, I have seen today a bunch of instances where I.OnStartUp() is executed only AFTER S.OnStartUp() has completed.

    Of course, this creates issues as S.OnStartUp() reads a bunch of I.properties which underlying values are "initialized" by I.OnStartUp()

    I have no idea if this new & random behavior is "normal", or a bug (in Ninja, likely triggered by my own code ?!).

    Thanks in advance
    Dominique

    #2
    Hello dom993,

    NinjaTrader has specific coding to be smart on how it uses its hosted indicators. Specifically when and when not to calculate values to be as efficient as possible. It is possible that due to race conditions on the indicator starting up that a hosted indicator may not actually calculate anything or properly initialize if there is no data to run on. In this case I would move any of my indicator calls to OnBarUpdate even on the first bar in OnBarUpdate as you could then depend on the indicator making some calculations


    So in that line of thought, an alternative that may help, would be to do the originating script's initial calculations in the first bar of OnBarUpdate.

    For example:

    If (CurrentBar == 0)
    {
    // do calculations
    }

    Another alternative would be to set a public bool in your indicator that will flag when the OnStartUp of the indicator has finished. A further check to only run your code once would be needed.

    For example lets say I am using a custom MyIndicator indicator:

    In the MyIndicator add to the variables region:
    private bool osuComplete = false;

    add to the properties region:
    [Browsable(false)]
    [XmlIgnore()]
    public bool OsuComplete
    {
    get { return osuComplete; }
    }

    Add as the last line of the OnStartUp():
    osuComplete = true;

    Then in the originating strategy variables:
    private bool ranOnce = false;

    and in the OnBarUpdate() add at the beginning:
    If (MyIndicator.OsuComplete == false)
    return;
    If (ranOnce == false)
    {
    // do initial calculations
    }
    ranOnce = true;



    This would cause the strategy to wait until the OnStartUp in the indicator has finished before allowing the OnBarUpdate code to execute. The ranOnce check would ensure that the inital calculations are only done once.


    If this does not resolve your inquiry please let me know how your strategy initiates the indicator and what information you are trying to sync from the indicator to the strategy.

    Any sample code would be useful.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Thank you for all your suggestions. I agree any of these will work around the issue (and I already implemented a test & repeat in OnBarUpdate() should that weird behavior happen again).

      I am still puzzled as to what happened today to trigger that scenario multiple times for the first time in 6+ months of live operations.

      FYI, in S.OnStartUp() I explicitly call I.Update() before trying to use any of its properties - this does always immediately trigger I.OnStartUp() , except a few times today.

      Have a great week-end
      Dominique

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      634 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      364 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      567 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X