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

Long running function in State.Terminated

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

    Long running function in State.Terminated

    Dear experts!

    My indicator need to execute long running function (~3000 ms) on Bar 0 in OnBarUpdate (OBU). If that function in OBU has finished then upon indicator termination there is a need to execute another long running function in State.Terminated. When indicator is added to the charts while data feed has already been connected, no issues were observed. Problems start on NT8 startup with 10 and more charts using that indicator and brokers connecting on startup. State.Terminated appears during an execution of OBU (I’ve used 3 brokers: Kinetic, FXCM and Continuum)
    After studying the posts

    https://ninjatrader.com/support/foru...ck+termination
    https://ninjatrader.com/support/foru...ination&page=3

    the Lock was implemented in OBU and in State.Terminated. This has resolved an issue with startup of NT8, but has created another one: On exit from NT8 or on connection recovery after connection loss State.Terminated is not called for some of the charts.
    Moreover, in some situations (when function’s running time is increased), State.Terminated of some indicators is not even finished.
    An indicator, demonstrating that behavior is in attachment.

    Changing both long running functions to be executed in the separate task or even in separate thread are is not helping.

    In attempts to find the resolution, I’ve added AddOn with OnWindowDestroyed, printing out titles of windows to be closed and again, some Charts (including ControlCenter window) were missing from the logs

    Code:
    	 protected override void 					OnWindowDestroyed									(Window cWindow)
    		{														 										
    		 if (cWindow == null)
    		 	{
    			 return;	
    			}
    			
    Log ("TestNT8AddOn :: OnWindowDestroyed : Destroying window. State = " + State.ToString() + " Title = " + cWindow.Title + " Window count = " + Globals.AllWindows.Count, LogLevel.Information);		 	
    						
    		}
    Thinking that logging functionality can be terminated before all windows are closed, I’ve created simple unmanaged DLL (with few functions, increasing the count (separate for each chart) after executing long running function in OnBarUpdate and decreasing count after executing long running function in State.Terminated (if long running function in OnBarUpdate has already ran prior to the State.Terminated)) , loaded that DLL in indicator’s State.Configure and unloaded it on State.Terminated. Logging of DLL counters after NT8 startup has shown correct values (1 count for each open chart), After closing NT8, DLL_PROCESS_DETACH has been received by DLL while not all counters were set to 0. This has confirmed the fact that not all charts have received State.Terminated, what, on my understanding, should not be the case before DLL was unloaded from all the Charts.
    It looks so that chart threads are terminated based on timeout (approximately 2000 - 3000 ms) without waiting for the end of State.Terminated results or even without calling some.

    From all above, there are few questions:
    1. What is the correct way to execute long running tasks in OnBarUpdate and State.Terminated to avoid all mentioned above problems?
    2. Is that a correct behavior of NT8 to terminate an application prior to the completeness of State.Terminated of all charts with an indicators?
    3. If this is a correct behavior , is there any way to increase the termination timeout (if it does exist) from AddOn or Indicator itself?

    Thank you in advance and
    GmA
    Attached Files

    #2
    Hello,

    Thank you for the post.

    I can only suggest implementing a different approach that does not require using async tasks. We do not recommend or support using a task like this in the NinjaTrader event-driven model. The async functions you wrote are not going to be called asynchronously the way you have them implemented in the script. Please see this publicly available link on how to call an asynchronous task:

    Learn about the C# language-level asynchronous programming model provided by .NET Core.


    OnBarUpdate task:
    If you called a task that takes 3 seconds to complete on each tick, for example, the OnBarUpdate function can be called many times before the first task finishes, this means you missed data points that are likely important to this task.

    Terminate task:
    This task will fail because if you start up the long-running asynchronous task in Terminate, the thread that was running Terminate is likely already finished. You would need to run whatever task this is on the UI thread to lock up NinjaTrader until it is done since there is no way for NinjaTrader to know when the async task is done.

    There is a help guide page on considerations that must be taken while developing scripts for NinjaTrader 8:


    Please let us know if you have any questions.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello Chris!

      Thank you for the fast reply.
      Just to clarify few points :
      1. Long running function is called only once on Bar 0 of OnBarUpdate. If it has run (and only then) long running function in the next State.Terminated need to be executed.
      2. I understand that provided code will not be running asynchronously. I've tried to run it using separate thread or separate task (this is where async functions were coming from. Sorry), but as you've mentioned without
      having no way for NinjaTrader to know when the async task is done
      the result was the same.

      In any case, the point is taken and I'll try to find the solution to use those long running functions in UI thread.

      Thank you very much again
      GmA

      Comment


        #4
        Hello Chris

        Sorry if I am asking here some basics, but still can’t get my head around the termination/resource cleaning on exit from NT8 application.
        I’ve tried to log termination of Control Center window on exit from NT8 Application in OnWindowDestroyed, but can’t catch it.

        Just used very simple AddOn below :

        Code:
        namespace NinjaTrader.NinjaScript.AddOns
        {
         public partial class TestOfAddOn : NinjaTrader.NinjaScript.AddOnBase
        	{		
        	 protected override void 					OnStateChange										()
        		{
        		 if (State == State.SetDefaults)
        			{				
        			 Description																			= @"Add on Test";
        			 Name																				= "TestOfAddOn";								
        			}
        		 else
        			{
        			 if (State == State.Configure)
        			 	{
        				}
        			}	
        		}
        
        	 protected override void 					OnWindowDestroyed									(Window cWindow)
        		{		 
        		 TextWriter tw 																			= null;						
        		 if (cWindow == null)
        		 	{
        			 return;	
        			}
        								
        		try
        			{
        			 Log ("AddOn :: OnWindowDestroyed : Destroying window. " + " Title = " + cWindow.Title + " Window count = " + Globals.AllWindows.Count, LogLevel.Information);		 									
        			
        		 	 if (cWindow is ControlCenter)
        		 		{					
        				 Log ("AddOn :: OnWindowDestroyed : Window is ControlCenter", LogLevel.Information);   			 
        			 	 tw 																			= new StreamWriter(@"C:\Temp\NT8Test.txt", true);
        			 	 if (tw != null)
        					{
        				 	 tw.WriteLine("OnWindowDestroyed :: Window is ControlCenter");
        					}																										
        				}
        			 
        			 Log ("AddOn :: OnWindowDestroyed : Finished", LogLevel.Information);			
        			}
        		 catch (Exception eE)
        			{
        			 Log ("AddOn :: OnWindowDestroyed : Exception is found. " + eE.Message, LogLevel.Information);				
        			}
        		 finally
        			{
        			 if (tw != null)
        			 	{
        				 tw.Close();
        				 tw.Dispose();
        				}
        			}	
        		}			
        	}	
        }
        I am not sure if logging functionality is still working when Control Center is under destroying, hence simple output to the external text file was added.
        When AddOn is re-compiled, NT8 logs and recording into the text file for Control Center Window are done as expected.
        At the same time, on exit from NT8 application, neither logs nor text file are updated for Control Center window.
        It looks OnWindowDestroyed for Control Center window is not called at all.
        If this is really the case, how to clean AddOn resources on exit from NT8 application? If it is not the case, what Am I doing wrong ?

        Thank you in advance
        GmA

        Comment


          #5
          Hello,

          Thank you for the reply.

          You can write to your file in State.Terminated of the addon if you wanted to log something at the end of its runtime. Addons share the same lifecycle schema as strategies and indicators.

          Code:
          if (State == State.Terminated)
                      {
                          Print("Terminating addon");
          				
          				string[] lines = { "First line", "Second line", "Third line" };
          		        // WriteAllLines creates a file, writes a collection of strings to the file,
          		        // and then closes the file.  You do NOT need to call Flush() or Close().
          		        System.IO.File.WriteAllLines(@"C:\Users\User\Documents\test.txt", lines);
                      }

          Please let us know if we may be of any further assistance.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Hello Chris!

            Thank you for the reply.
            I am not just looking for the termination of AddOn. I am trying to find an appropriate (hopefully only one) place to clean up the resources in different stages of NT8 (closing chart, closing workspace, exit from NT8 application).
            In NT8 docummentation you are always referring to perform claenup of AddOn in OnWindowDestroyed of Control Center window.
            Sorry, but I still can't understand if OnWindowDestroyed of Control Center is called on exit from NT8, why can't I catch it in the logs/file, or if it is not called then how the cleanup, recommended in all Ninjatrader AddOn examples have to be properly done.

            Could you please clarify?

            Comment


              #7
              Hello Chris!

              Just to add to the previous post.
              For the test purpose I've added State.Terminated into AddOn and have copied the same code you've provided (just changed the file path and name).
              During AddOn compilation the file was created and lines were written. On exit from NT8 application, file has not been created what on my understanding mean that State.Terminated has not been called.
              NT8 has been started and closed several times. File has not been created. No exceptions in the log file.

              What am I doing wrongly?

              Thank you in advance
              GmA

              Comment


                #8
                Hello,

                Thank you for the reply.

                I am going to ask our product management team if it is expected for OnWindowDestroy and OnTerminate to be ignored when NinjaTrader is closing.

                While we wait for their response, what resources are you trying to clean up at the close of NinjaTrader?

                I look forward to your reply.
                Last edited by NinjaTrader_ChrisL; 01-18-2018, 07:44 AM.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Chris!

                  Thank you very much for the response.

                  AddOn is using functions from external unmanaged DLL, what allocates own memory, mutexes, events, windows etc. DLL is loaded (and creates those own resources) on the first request from NT8 Indicator using that AddOn and supposed to be released (including unload of DLL after cleanup) on termination request from the last NT8 Indicator using that AddOn.

                  Due to the fact that in some situations State.Terminated is not arriving to the indicators at all (when we have 10+ charts open even without long running functions inside State.Terminated and OBU), I am considering to release those resources during either AddOn termination or in the Control Center OnWindowDestroy

                  Hope it clarifies.
                  Will be waiting for the feedback from product management team.
                  GmA

                  Comment


                    #10
                    Hello,

                    Thank you for your patience.

                    After testing, I found that the Control Center window is not passed to OnWindowDestroyed during shut down of NinjaTrader nor is OnTerminated reached for the Addon itself during shutdown. I have submitted this to our development team and will follow up when I have further details. The AddonWindow itself and all other windows are passed to OnWindowDestroyed during shutdown of NinjaTrader.

                    Please let us know if you have any questions.
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Chris!

                      Thank you very much for your response.
                      Just to clarify one thing:
                      With the indicator from the first post preset on 25+ charts, OnWindowDestroy (as well as the State.Terminated) is not always called for all chart windows on exit from NT8 application even when the delay (Long running function) is very small (iDelay is between 50 and 100). Possibly this information can help developers.

                      Will be waiting for the your response.
                      Thank you again
                      GmA

                      Comment


                        #12
                        Hello Chris!

                        Any update from development team?

                        Comment


                          #13
                          Hello,

                          Thank you for the follow-up.

                          This has been addressed by our product management team and has been fixed.
                          Chris L.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Chris!

                            Thank you very much for the good news.
                            When that correction will become available?

                            Comment


                              #15
                              Hello,

                              It looks like this will be fixed in the next update. I can not 100% confirm this though.
                              Chris L.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, 04-16-2024, 06:09 PM
                              4 responses
                              12 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by terofs, Today, 04:18 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post terofs
                              by terofs
                               
                              Started by nandhumca, Today, 03:41 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post nandhumca  
                              Started by The_Sec, Today, 03:37 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post The_Sec
                              by The_Sec
                               
                              Started by GwFutures1988, Today, 02:48 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Working...
                              X