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

Compile of AddOn Clears Instance Variables in Running Instances

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

    Compile of AddOn Clears Instance Variables in Running Instances

    When developing an AddOn, if one recompiles the code whilst any instances are running, all instance variables in the running instances are reinitialized.

    This is evidenced by the associated windows being destroyed and recreated, although with no visible change to the open windows of the instances.

    Furthermore, it seems AddOn instance variables are initialized only once, at compilation. Any changes to them in the code remain as changed on the next invocation of the AddOn. For example, an instance string initialized to "Fred" and subsequently set to "Harry" in the code will be "Harry" on the next invocation of the AddOn. They seem to be treated as static. Is this "unusual" behaviour documented?

    Although the re-initialization is not likely to eventuate in production as recompilation is unlikely to occur in a production environment, surely this can be avoided in development too? And it is imperative to know that instance variables are not really instance at all in the context of an AddOn. Essentially, one must explicitly initialize all instance variables at runtime to ensure they start out with the expected values.

    Thanks.
    Last edited by jeronymite; 10-07-2020, 01:55 AM.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    #2
    Hello jeronymite,

    Thanks for your post.

    We have to keep in mind that when we recompile, we build a new assembly and use scripts from the new assembly. In more common NinjaScripts reflection would be needed in the script's CopyTo method to be able to copy variables from an old instance to a new instance upon compilation. (This can be seen in the SampleIndicatorTypeConverter example and the PriceLevels DrawingTool script.)

    AddOn's do not offer a CopyTo or Clone override, so I am not sure yet on how that can be approached in an AddOn. I would need a use case/test script to present the question to the development team that demonstrates these symptoms and how they are hit when building AddOns similar to our examples.

    Could you modify the basic AddOn example here and provide some steps I can follow to recreate the re-initialization issue, as well as how I can see the initialized only once issue? If you can make separate scripts and steps, that would be preferred. (I understand the steps may be simple to just re-compile, but some clear steps will help to give me a clear demonstration right away.)

    I look forward to hearing back.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks, Jim. I appreciate you looking into this.

      I attach a version of your AddOnShell with minor additions to demonstrate the issues. I have simply added an instance integer variable that is initialized to 1 and subsequently output at various points in the AddOn. It is incremented by 1 at any Window creation. So, as windows are created it increments, but when the AddOnShell is run, one would expect it to reset to 1 -- it does not reset. A recompile resets it, but as various windows are created it increments during what should be a compile of the AddOnShell alone.

      The behaviour is "unusual" to say the least. That does not mean it cannot be understood and explained, but it does mean it is not what one might typically expect.

      Happy to explore further with you, if you need additional input.

      Thanks!
      Attached Files
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Hello jeronymite,

        Thanks for demonstrating with an example.

        We should note that when we inherit the AddOnBase class, we wire into some general platform functions that is not specific to the AddOn. OnWindowCreated is called for every window creation (not just the AddOn's own window,) and this is used when we notice the Control Center is opening so we can add a menu item which opens our AddOn window.

        For persistence specific to the AddOn window, I would suggest creating private variables in the AddonShellWindow class, or the associated window class in your AddOn. When I add the integer in the NTWindow derived class and increment/print in the constructor for this class, the variable prints as I would expect and then gets reinitialized as I would expect.

        Please let me know if there is anything else I can assist with.
        Attached Files
        JimNinjaTrader Customer Service

        Comment


          #5
          Thanks, Jim. Much appreciated.

          I do use the approach you suggest already for a variety of things. And I do understand the more global nature of OnWindowCreated etc.

          Some use cases require I use the "outer" environment. For example, I apply various validity criteria to invocations, particularly around licensing, that may result in no window being created. So in those cases I will ensure I manually manage instance variables in that outer environment to be as expected.

          Grateful for this set of behaviours to be documented so other programmers are made aware of the situation. It may save others from spending time tracking down oddities that are "works as designed, but you didn't know that". Please include commentary about recompiling while instances are running.

          Thanks again for looking at this with me.
          Last edited by jeronymite; 10-08-2020, 03:58 PM.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Vitamite, Yesterday, 12:48 PM
          3 responses
          17 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by aligator, Today, 02:17 PM
          0 responses
          8 views
          0 likes
          Last Post aligator  
          Started by lorem, 04-25-2024, 09:18 AM
          22 responses
          94 views
          0 likes
          Last Post lorem
          by lorem
           
          Started by sofortune, Today, 10:05 AM
          3 responses
          16 views
          0 likes
          Last Post NinjaTrader_ChristopherJ  
          Started by ETFVoyageur, 05-07-2024, 07:05 PM
          23 responses
          185 views
          0 likes
          Last Post ETFVoyageur  
          Working...
          X