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

NT Does Not Invoke OnTermination In Replay Mode (Leaving ToolStrip Items Behind)

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

    NT Does Not Invoke OnTermination In Replay Mode (Leaving ToolStrip Items Behind)

    I have found that in replay NT will not invoke OnTermination when the indicator is refreshed. When connected to a live feed, it does get invoked.

    Below is the console output showing the sequence of invocations when I create a fresh chart, then add the indicator, then hit F5:

    Initialize
    Initialize
    Initialize
    Initialize
    OnStartUp
    OnBarUpdate
    OnTermination // On the replay connection, this is missing.
    Initialize
    OnStartUp
    OnBarUpdate

    I am having trouble with abandoned resources. Particularly, an unsupported feature: custom tools in the ToolStrip. And in fact, that is particularly vexing: I am trying to explicitly remove these objects during initialization, but they still do not get removed from the ToolStrip. This is the code that is called from OnStartUp:

    Code:
    		private void initializeToolStrip() {
    			// TODO: In replay, the strip is being left behind even with a null check on the ninjaToolStrip field
    			if (ninjaToolStrip != null) {
    				ninjaToolStrip.Items.Remove(pcvToolStripSeparator);
    				ninjaToolStrip.Items.Remove(pcvToolStripDropDownButton);
    			}
    			ninjaToolStrip = (ToolStrip) ChartControl.Controls.Find("tsrTool", false)[0];
    			ninjaToolStrip.Items.Remove(pcvToolStripSeparator);
    			ninjaToolStrip.Items.Remove(pcvToolStripDropDownButton);
    			
    			pcvToolStripSeparator = new ToolStripSeparator();
    			pcvToolStripSeparator.Name = "pcvToolStripSeparator";
    			ninjaToolStrip.Items.Add(pcvToolStripSeparator);
    			
    			// Add other tools ...
    Trying to explicitly remove my ToolStripItems does not work! It seems impossible! Perhaps NT is making clones? I'll try removing by name.

    In any case it seems inconsistent that OnTermination IS invoked on a live connection but not on Replay ...

    #2
    Hi steevcoco, I would not expect that and also I cannot reproduce your findings here. I have run a few tests running prints from OnTermination and it gets called as expected in my replay session as well when the script is removed or the indicator refreshed via F5.

    You can compare yours in replay for example to this MAToggle script shared on the forums - http://www.ninjatrader.com/support/f...ad.php?t=38132

    In a quick test on live and replay I don't see any inconsistencies.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks Bertrand.

      This IS the sequence on replay:

      Initialize
      Initialize
      Initialize
      Initialize
      OnStartUp
      OnBarUpdate
      Initialize
      OnStartUp
      OnBarUpdate

      (No OnTermination event.) It also DOES happen inconsistently: sometimes after a restart I do see the event. After some event, which is not reproducible, but perhaps queueing the replay to a new time, then the OnTermination event disappears.

      I have seen other inconsistent behavior with the replay: other sequences of events -- I have seen OnBarUpdate not get invoked while historically loading the chart. I have seen gaps and strange formations in candles like one giant bar: my indicator actually loads "footprint" data, and I have seen the data that would normally form several prior bars, seemingly timestamped all into one later bar (the historical filling is coming from an added 1-tick data series, and in OnBarUpdate I assign the ticks to bars in the primary series based on BarsArray.GetBar(time)) -- for example, specifically: BarsArray[0].GetBar(Times[1][0]).

      And this would be consistent with the other thread you pointed me to: if NT does not invoke OnTerminate as expected, then resources could be abandoned.

      In fact I also solved my ToolStrip problem: in the initialize sequence I add items to the NT strip. In OnTerminate I checked the fields for null and removed them. I repeated that code in the initialize sequence, but was still seeing the items stay in the strip. I changed that to find the items by name, and now they are being removed. It seems that aside from not invoking OnTerminate, the fields are being set by reflection (or another process, perhaps just a new indicator), so those checks for the items by reference were not finding the items. So I look for the items by name and remove them IN the initialization sequence; and that is working.

      The initialize sequence for a non-trivial indicator was a little vexing to understand. Any references to BarsArray or ChartPanel require using OnStartUp; and then there is some making sure about the sequence of method reentrancy. Reflection is setting fields, Initialize is being invoked, and with OnStartUp there is another place to find initialization code. And then knowing not to rely on OnTermination consistently requires something like I've done -- disposing resources in the Initialization sequence. So it took some holding hands with NT through testing to find some of that out.

      Thanks again. I would send you trace files if you thought it was worth looking into.
      Last edited by steevcoco; 08-12-2014, 01:50 PM.

      Comment


        #4
        I can start another thread, but I wanted to note in this thread that when I enable / disable a strategy I also do not see OnTerminate getting called. It only gets called if you delete the chart or delete the strategy from the chart. Is there anything that gets called on disabling a strategy and could it be related to steevcoco's issue?

        Comment


          #5
          Originally posted by tradetree View Post
          I can start another thread, but I wanted to note in this thread that when I enable / disable a strategy I also do not see OnTerminate getting called. It only gets called if you delete the chart or delete the strategy from the chart. Is there anything that gets called on disabling a strategy and could it be related to steevcoco's issue?
          OnTermination() should get called. I see it happen consistently either reloading NinjaScripts or running through enable / disable cycle. Do you get any error releasing custom resources used there perhaps?
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Originally posted by steevcoco View Post
            Thanks Bertrand.

            This IS the sequence on replay:

            Initialize
            Initialize
            Initialize
            Initialize
            OnStartUp
            OnBarUpdate
            Initialize
            OnStartUp
            OnBarUpdate

            (No OnTermination event.) It also DOES happen inconsistently: sometimes after a restart I do see the event. After some event, which is not reproducible, but perhaps queueing the replay to a new time, then the OnTermination event disappears.

            I have seen other inconsistent behavior with the replay: other sequences of events -- I have seen OnBarUpdate not get invoked while historically loading the chart. I have seen gaps and strange formations in candles like one giant bar: my indicator actually loads "footprint" data, and I have seen the data that would normally form several prior bars, seemingly timestamped all into one later bar (the historical filling is coming from an added 1-tick data series, and in OnBarUpdate I assign the ticks to bars in the primary series based on BarsArray.GetBar(time)) -- for example, specifically: BarsArray[0].GetBar(Times[1][0]).

            And this would be consistent with the other thread you pointed me to: if NT does not invoke OnTerminate as expected, then resources could be abandoned.

            In fact I also solved my ToolStrip problem: in the initialize sequence I add items to the NT strip. In OnTerminate I checked the fields for null and removed them. I repeated that code in the initialize sequence, but was still seeing the items stay in the strip. I changed that to find the items by name, and now they are being removed. It seems that aside from not invoking OnTerminate, the fields are being set by reflection (or another process, perhaps just a new indicator), so those checks for the items by reference were not finding the items. So I look for the items by name and remove them IN the initialization sequence; and that is working.

            The initialize sequence for a non-trivial indicator was a little vexing to understand. Any references to BarsArray or ChartPanel require using OnStartUp; and then there is some making sure about the sequence of method reentrancy. Reflection is setting fields, Initialize is being invoked, and with OnStartUp there is another place to find initialization code. And then knowing not to rely on OnTermination consistently requires something like I've done -- disposing resources in the Initialization sequence. So it took some holding hands with NT through testing to find some of that out.

            Thanks again. I would send you trace files if you thought it was worth looking into.
            Thanks Steve, logs and traces would be good to review for the time period where you saw the issues. If this happens reproducibly on a study for you, we would appreciate a simple version of the script to test and ensure this could be improved potentially. With all variants I tested so far (also on replay) I could not see OnTermination missed so far.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              tradetree:

              I don't know what your specific problem is, but I just wanted to point out again as I posted, that: if you are actually having trouble with abandoned resources because of missing the OnTerminate event, then it may be due to the way NT is setting the fields.

              I did discover that when that event it missing, NT is either re-populating the fields in the indicator (more likely), or creating a whole new instance; so if you are checking for references to objects, they may not get discovered (my particular problem was overcome because the Controls I was looking for have Names and I can search by name and do not need to count on the actual instances that I created to still be alive ... that might not be so easy for other code that would expect to need to find objects that you have created, but are now garbage ...)

              Anyway, good luck!

              Comment


                #8
                OnTermination() is not called on changing of time-frame

                This is old thread, Anyway..
                I've found out a similar issue: OnTermination() is not called when I change the period (time-frame) of a chart. At the same time Ninja creates new instance of the indicator. (NT 7.0.1000.27)
                In such circumstances I'm forced to resort to various tricks to dispose of all that stuff that was created in the old instance of the indicator.

                Comment


                  #9
                  leoxander,

                  Thank you for your post.

                  Do you have a working sample that will reproduce this scenario?

                  How are you changing the timeframe?
                  Cal H.NinjaTrader Customer Service

                  Comment


                    #10
                    Well, I change the periodicity using drop-down button on Chart's tool bar.
                    However, all this may longer be of no significance, just because I've discovered that the problem happens in a very very very specific conditions. The problem occurs only in the market replay mode, moreover it occurs only after (Re)player has reached the end of a replaying period. It's very strange.
                    And just to make sure — there were no any exception in my OnTermination() method!

                    I've noticed that all works fine in simple scripts, OnTermination is called when it should be. So, it's hard to reproduce this issue. Sorry to trouble you.

                    Comment


                      #11
                      Thanks leoxander

                      I did a quick test on that scenario, got the expected call for OnTermination()

                      If you are able to get this reproducible, let me know and I would be happy to take a deeper look
                      Cal H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Jon17, Today, 04:33 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post Jon17
                      by Jon17
                       
                      Started by Javierw.ok, Today, 04:12 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post Javierw.ok  
                      Started by timmbbo, Today, 08:59 AM
                      2 responses
                      10 views
                      0 likes
                      Last Post bltdavid  
                      Started by alifarahani, Today, 09:40 AM
                      6 responses
                      41 views
                      0 likes
                      Last Post alifarahani  
                      Started by Waxavi, Today, 02:10 AM
                      1 response
                      20 views
                      0 likes
                      Last Post NinjaTrader_LuisH  
                      Working...
                      X