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

Reload external libraries during the runtime

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

    Reload external libraries during the runtime

    Hi,

    I am developing all my indicators and strategies outside of NinjaTrader and then copying them as class libraries into ($UserProfile)\Documents\NinjaTrader 8\bin\Custom folder. When I restart the platform then all those assemblies will get successfully loaded and everything works fine. However, NinjaTrader will lock those DLL files and won't allow me to update them without closing the platform. This is expected behavior as Ninja will load those assemblies from files during the startup and lock them for the rest of the application runtime. As you can imagine, the debugging process of those assemblies can be very time-consuming as it needs the platform to be restarted for each build.

    I have attempted to create a workaround by enabling shadow copy files, loading the assembly during the runtime, and then adding the assembly to Core.Globals.AssemblyRegistry. See the strategy attached as a reference. Note that we are placing DLL in the lower directory now to avoid NT loading them on the startup from bin\Custom.

    Code:
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
       public class MyCustomStrategy : Strategy
       {
          private readonly HashSet<string> _externalAssemblies = new HashSet<string>();
    
          protected override void OnStateChange()
          {
             if (State == State.SetDefaults)
             {
                Description = @"Loads external assemblies during the runtime.";
                Name = "Assembly Loader";
             }
             else if (State == State.DataLoaded)
             {
                if (AppDomain.CurrentDomain.ShadowCopyFiles == false)
                   AppDomain.CurrentDomain.SetShadowCopyFiles();
    
                _externalAssemblies.Clear();
    
                var folderPath = Path.Combine(Environment.GetFolderPath(Environment .SpecialFolder.MyDocuments), @"NinjaTrader 8\bin");
                var assemblies = Directory.GetFiles(folderPath, "*.dll");
    
                foreach (var path in assemblies)
                {
                   var file = new FileInfo(path);
                   var assembly = Assembly.UnsafeLoadFrom(file.FullName);
    
                   Core.Globals.AssemblyRegistry.Add(file.Name, assembly);
    
                   _externalAssemblies.Add(file.Name);
    
                   Print("Added: " + file.Name);
                }
             }
             else if (State == State.Terminated)
             {
                foreach (var assembly in _externalAssemblies)
                {
                   Core.Globals.AssemblyRegistry.Remove(assembly);
    
                   Print("Removed: " + assembly);
                }
             }
          }
       }
    }
    This seems to all work. When I start the strategy, all the assemblies are loaded and working, and it does not lock any files. However, the assembly is loaded in the main AppDomain, and even if I load it again after rebuilding the assembly, it won't reflect the changes as it's cached and would require unloading the domain first. Therefore, this solution works only once, then I need to restart the platform again.

    Could someone help me fix that or is there any other solution that would make NinjaTrader reload external libraries without restarting the platform every time?
    Attached Files
    Last edited by @tmc_; 03-12-2021, 05:53 AM.

    #2
    Some related topics:
    NinjaTrader requires that you shut down and restart - NinjaTrader Support Forum
    Reload AddOn without closing NT - NinjaTrader Support Forum
    Does Additional DLL Have to Be in the Custom Folder? - NinjaTrader Support Forum
    Way around restarting NT with change of dll reference? - NinjaTrader Support Forum
    Reloading Custom.dll after compiling in Visual Studio - NinjaTrader Support Forum

    Looks like more people facing the same issue. Frankly speaking, NT8 is probably the best platform publicly available when it comes to customization and extensibility through custom development. However, I have not experienced this limitation in any other trading platform and find it very annoying and inconvenient. There are many serious developers not relying on the built-in IDE and I would love to see NT8 natively supporting Visual Studio or Code so we don't need to go through all this struggle to achieve something as basic as creating an indicator as a separate solution and not inside NinjaTrader.Custom.

    Comment


      #3
      Hello @tmc_,

      All that I could suggest here from a support standpoint is to avoid what you are trying to do and develop your code in the platform first. Later when your code is mature and ready you could migrate it to the external project for whatever build process you needed that for.

      The external visual studio project that is provided in the help guide is expected to require a restart to reload. This is really intended for final release type builds and not active development due to what is required to see changes. You can instead use visual studio with the code in the platform by using the toolbar button in the NinjaScript editor. Visual studio is not used for building in that case and is just used for its advanced code editing features. When you save the file NinjaTrader will compile and reload the new code. that would be the suggestion to combine active development + visual studio.

      I would otherwise not suggest trying to mess with the compile system/ shadow copy/ how objects are loaded as you may inadvertently hurt other items in the process. Our support won't be able to help with anything on that path so if you want to further explore that you can but we would not be able to assist.

      Please let me know if I may be of further assistance.

      JesseNinjaTrader Customer Service

      Comment


        #4
        Hi Jesse,

        Thanks for the response, much appreciated.
        Please, could you make a feature request for reloading DLLs without the restart and add my vote?

        Might I know how NinjaTrader.Custom.dll is reloading? Perhaps it could give me a hint on how to achieve my goal.

        Comment


          #5
          Hello @tmc_,

          Yes I will put in a note about this or add a vote if I can locate any existing requests. If i have any other details I can provide I will post back here.

          The loading of the assemblies is internal code so unfortunately that is not something which we could disclose as it is not available to view.
          JesseNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by funk10101, Today, 12:02 AM
          0 responses
          3 views
          0 likes
          Last Post funk10101  
          Started by gravdigaz6, Yesterday, 11:40 PM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by MarianApalaghiei, Yesterday, 10:49 PM
          3 responses
          10 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by XXtrader, Yesterday, 11:30 PM
          0 responses
          4 views
          0 likes
          Last Post XXtrader  
          Started by love2code2trade, 04-17-2024, 01:45 PM
          4 responses
          28 views
          0 likes
          Last Post love2code2trade  
          Working...
          X