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

How to trigger action on NT startup

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

    How to trigger action on NT startup

    which code should i use to fire a function when NT start-ups (once).
    I've tried this:

    Code:
    namespace NinjaTrader.Gui.NinjaScript
    {
    	
    	public class xyz: AddOnBase
    	{
      		protected override void OnStateChange()
    		{
    			if (State==State.SetDefaults)
    			{
    				System.Windows.Forms.MessageBox.Show("h3");
    			}
    		}
    
    		protected override void OnWindowCreated(Window window)
    		{
    			System.Windows.Forms.MessageBox.Show("h2");
    		}
    	}
    }
    but it fires on each re-compile event.
    Last edited by ttodua; 09-05-2018, 08:41 AM.

    #2
    Hello TazoTodua,

    There is not really a context that would only be loaded one time from NinjaScript, the NinjaScript types would all be reloaded during a compile including add-ons.

    You could potentially use the addon external visual studio project that creates a DLL, and then use the DLL as that would only load once at startup. Potentially a constructor could be used in that context. In NinjaScript, there is not really any cases that should only occur once due to the dynamic nature of the framework being reloaded.

    Can you provide more detail on what the end goal is? You noted at startup, are you wanting to do an action when the control center appears? Perhaps there is a more NT8-correct way to approach the task.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse Thanks as always, smart observations.
      I thought about .dll, but in my case/situation, it's not flexible for me, I want to integrate that code into Addon script.

      Actually, yes, i want to trigger that when Control Center appears, or like that doesnt matter when.

      My case doesnt matter, but for your interest my case is as follows:
      When I have NT open, it generates specific custom files.
      I want on next launch of NT, to delete all previous session-files.

      Comment


        #4
        Hello TazoTodua,

        Thank you for the additional details.

        Yes, this is a difficult item to determine due to the requirements.

        In this case, because you specifically want the startup to delete existing files but want to avoid that for compiles, that will be difficult. If the problem was simply making sure files exist, you could use a file exists check for that. Because the addon and other types will reload from a compile we technically won't know the difference between a startup and compile as you noted.

        I will need to research this further to see if there are any specific ways we can determine the initial start versus a recompile for this use case. The control center window is re-run through the OnWindowCreated from a compile, so checking for that window would not be helpful. Creating an external file to know the platform has started would also be unusable due to the recompiling.

        Once I have more details I will reply back here.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello TazoTodua,

          After further discussion about this question, there is not really anything internal that could be used for this purpose due to how NinjaTrader is expected to work.

          With that being said, you would likely need to come up with a creative solution to this problem. One way that was suggested to me would be to use the process's start time and find if that was within a window of your choosing.

          Perhaps within 10 seconds for startup, reset the file otherwise if the platform has been up for some time, dont do that.

          Here is an example of getting the process's start time:

          Code:
          TimeSpan startTime = DateTime.UtcNow - System.Diagnostics.Process.GetCurrentProcess().StartTime.ToUniversalTime();
          Print(startTime);
          You could then use standard timespan conditions to check how long the process has been running when a change is detected such as OnWindowCreated for the ControlCenter.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Jesse. Outstanding support! that seems the ONLY solution, how you thought about that
            many thanks again!!

            Comment


              #7
              Another question:

              is there like that, to fire when "NT shuts down" ? (that's to happen once too) ?

              Comment


                #8
                Hello TazoTodua,

                There is an exit time property as well but that would only be relevant if you were reading the process information from a context that is still active when NinjaTrader exited. I don't believe there would be a way to compare the exit time like the start time.

                I also tried subscribing to the exited event for the process but that does not seem to fire while NinjaScript is active so I am unsure of a way for that to be possible. Currently, I don't see anything specific that could be used for the exit specifically.

                Perhaps you could add some logic into the startup sequence you have to account for this. For example, the scripts Terminated state would happen prior to exiting. This also happens during a compile but the script activates shortly after. Whatever action is trying to happen on exit could occur in Terminated, and in case that should not have occurred because it was a compile you could revert that action in the startup logic if you are past the target start time.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Have you though about traversing the visual tree and checking for the control center window? Just a thought.

                  Comment


                    #10
                    Jesse, thanks!
                    also, Zeos6 thanks for suggestion- ( however, i just dont know exactly which object/methods to use to achieve that) and not have time to digg into that at this moment.


                    Finally, I just decided to use Addon(which is loaded on startup) in Class- constructor, i do that time-check (if started inside 60 seconds).
                    Last edited by ttodua; 09-28-2018, 07:32 AM.

                    Comment


                      #11
                      Hello TazoTodua,

                      I am not really sure what it is you are trying to accomplish but you can determine that the ControlCenter is up and running inside State.Configure. It is not easy because, as Jesse stated, controls are reloaded multiple times inside a multithreaded environment.. The easiest way to determine what you need is to walk the visual tree looking for the control you want. Just make sure you do it on the both the UI and the non-UI threads. as I said, it is a bit tricky though. Really make sure though you want to go down this route. Perhaps what you need could be refactored into a simpler approach.

                      Comment


                        #12
                        Zeos, many thanks for in-detail answer. I theorically understand what you say and like it, just without any example code i might find it difficult to do what you describe. Are there any example codes/topics you can give the link ?

                        Comment


                          #13
                          Just to get you started, check out the following from ChelseaB. The code will need to be altered a bit to get it working the way you need but you'll get the idea.


                          Also, check out the following. This is the key to making it work.


                          Finally, do not use foreach. The collections are changing as you iterate them, and foreach will not work. Use a for loop instead.

                          If you need more help, you can pm me. Good luck.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by nleitman, Yesterday, 11:46 AM
                          17 responses
                          44 views
                          0 likes
                          Last Post nleitman  
                          Started by ninza33, Today, 12:31 PM
                          0 responses
                          1 view
                          0 likes
                          Last Post ninza33
                          by ninza33
                           
                          Started by tradingnasdaqprueba, Today, 03:42 AM
                          7 responses
                          31 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by kaywai, Today, 11:59 AM
                          1 response
                          5 views
                          0 likes
                          Last Post NinjaTrader_Erick  
                          Started by ETFVoyageur, 04-30-2024, 02:04 PM
                          13 responses
                          110 views
                          0 likes
                          Last Post ETFVoyageur  
                          Working...
                          X