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 Can AddOn Detect Recompile?

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

    How Can AddOn Detect Recompile?

    I have some AddOn functionality that is provided with some statics. All works great, but would be nice to have a way to detect a recompile, so could trigger some cleanup of the previous instance and allow the new instance to take over. Occasionally, I end up with two instances still running.

    Is there a way to detect a recompile or something like a Terminate state for a non-window based AddOn?

    #2
    Hello aslane,

    I'm not aware of any property or event accessible from NinjaScript that would change when a compile happens.

    I will submit a feature request for you. Once I have a tracking ID for this request I will post in this thread for future reference.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      It would be nice and probably not very difficult to add something like an OnCompileFinished event that can be subscribed to.

      A different solution is to have code that uses static variables in a separate DLL, therefore it won't be affected by NT code recompilations. Actually the same thing applies addons that are imported, so it only matters during development.

      Code:
      namespace NTDataStore {
          public static class StaticCounter {
              public static int counter = 0;
          }
      }
      Indicator

      Code:
              protected override void OnBarUpdate()
              {
                  if(CurrentBar != 0) {
                      return;
                  }
                  Print(NTDataStore.StaticCounter.counter++);
              }
      It was also necessary to change the TargetFramework to net452 by manually editing the csproj and add the dll reference in NT via the code editor References option.

      Without these steps using a .netstandard 1.0 DLL, I was only able to load it via reflection E.g.
      Code:
      Assembly.LoadFile
      Last edited by MojoJojo; 06-16-2020, 06:48 AM.

      Comment


        #4
        The best I have been able to come up with is to use the AddOn State transitions. The AddOn will go thru Terminated state on a recompile, and then the new instance is created and goes thru normal state progression. You still need to deal with cleanup properly, as some other user may be using the static instance, but that would be true of a recompile event also.

        Comment


          #5
          I've tested this code

          Code:
                  protected override void OnStateChange() {
                      if (State == State.SetDefaults) {
                          Description                                    = @"NinjaTrader Data Store";
                          Name                                        = "NinjaTraderDataStore";
                      } else if (State == State.Configure) {
                          AppDomain.CurrentDomain.AssemblyLoad += MyAssemblyLoadEventHandler;
                      } else if (State == State.Transition) {
                          Print("Transitioning");
                      }
                  }
          
                  private void MyAssemblyLoadEventHandler(object sender, AssemblyLoadEventArgs args) {
                      Print("ASSEMBLY LOADED: " + args.LoadedAssembly.FullName);
                 }
          When recompiling I don't get a "Transitioning" print, but the assembly load event spits out a bunch of stuff

          The imported addon because it is loaded from a DLL will only output information once on each recompilation, because NT does not create a new instance of it. While code that is merely in the addon folder will create a new instance of that class and therefore also a new AssemblyLoad listener increasing output on each compilation.
          Last edited by MojoJojo; 06-16-2020, 10:05 AM.

          Comment


            #6
            Right, AddOn only goes thru SetDefaults, Configure, Active, and Terminated states.Just add a Print(State) to your OnStateChange and you will see.

            Comment


              #7
              You obviously know your stuff. My only idea is to use an external dll that will be unaffected by recompilation and therefore keep its static values to communicate between instances. This only matters if the addon is not residing in a DLL itself.

              Otherwise I hope the NT will be able to help you out .

              Comment


                #8
                Hello aslane,

                This request for an event that runs on compile which can reload NinjaScripts is being tracked with ID# SFT-480.

                As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted in the Release Notes page of the Help Guide.

                Release Notes — https://ninjatrader.com/support/help...ease_notes.htm
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  This would be a most useful state to be able to detect. Did anyone come up with a decent solution for this? It is a right pain for me when developing. If you store data in an add-on and then recompile, the need to go and manually refresh every single chart etc., to try and make sure they all load the same new code is a royal pain! I am currently looking at an external data store, but with all the performance implications,etc.

                  Comment


                    #10
                    +1

                    Please add my vote.

                    Comment


                      #11
                      My vote too.

                      And some relevant additional reading here: https://ninjatrader.com/support/foru...ning-instances

                      My own approach is as follows.

                      In my AddOnBase class:
                      bool IsRecompilation = false; bool IsNinjaTraderStartup = false;

                      In my AddOnBase class during invocation:
                      IsRecompilation = false; IsNinjaTraderStartup = false;

                      In OnWindowCreated, near the top:
                      if (Globals.AllWindows.Count == 1) IsNinjaTraderStartup = true;

                      In OnWindowCreated, when it's the ControlCenter window:
                      IsRecompilation = !IsNinjaTraderStartup;
                      if (IsRecompilation) { /* Do whatever you want, then ... */ IsRecompilation = false; }


                      In OnWindowDestroyed, when it's the ControlCenter window:
                      IsRecompilation |= Globals.AllWindows.Count > 0;

                      Completely unsupported, not "perfect", subject to change without notice, of course ... but it works for now. A supported mechanism is preferred.

                      Thanks.
                      Last edited by jeronymite; 01-14-2023, 08:38 PM. Reason: Additional detail
                      Multi-Dimensional Managed Trading
                      jeronymite
                      NinjaTrader Ecosystem Vendor - Mizpah Software

                      Comment


                        #12
                        Hello,

                        Thanks for your notes.

                        Your votes have been added to this feature request.

                        Please let me know if I may assist further.
                        Brandon H.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by SantoshXX, Today, 03:09 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post SantoshXX  
                        Started by DanielTynera, Today, 01:14 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post DanielTynera  
                        Started by yertle, 04-18-2024, 08:38 AM
                        9 responses
                        41 views
                        0 likes
                        Last Post yertle
                        by yertle
                         
                        Started by techgetgame, Yesterday, 11:42 PM
                        0 responses
                        12 views
                        0 likes
                        Last Post techgetgame  
                        Started by sephichapdson, Yesterday, 11:36 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post sephichapdson  
                        Working...
                        X