Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using a C# class library in a script

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

    Using a C# class library in a script

    Hi,

    I'm trying to use an external C# class library as part of a strategy development.

    Initially, the code was built using .NET 5.0 and when adding the reference to the library I got the following compilation error:
    The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
    So, I rebuilt the library with .NET Framework 4.8 and now I get the following load error:
    Unable to load custom assembly: The given key was not present in the dictionary.
    Any help on how to create the external library will be greatly appreciated.

    Thanks.

    #2
    Hello haimcn,

    If the assembly targets .NET 4.8 adding a reference should be all that is necessary to access the namespace.
    There might be an issue with the assembly.

    If you restart NinjaTrader does the error persist?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Thanks for the reply.
      Yes, it persists, and I cannot open the Ninja anymore.

      The error message is:
      Unable to load custom assembly. Original version has been restored. You need to recompile your custom NinjaScript files.
      Confirming that brings the following error message:
      Unable to load NinjaTrader custom assembly. Please reinstall NinjaTrader: Could not find a part of the path.
      Thanks.
      Last edited by haimcn; 01-02-2024, 03:40 PM.

      Comment


        #4
        Hello haimcn,

        It sounds like an issue with the assembly file.

        Remove this and remove the reference from the Config.xml file and you should be able to restart.

        If you have the open source code, can you access the namespace classes in the file?
        Cn you export the assembly from NinjaTrader and see if the assembly works when re-iimported?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I cannot export the strategy.
          Unable to generate namespace file: The given key was not present in the dictionary.

          Comment


            #6
            Hello haimcn,

            Do you have the open source code of the C# library?

            Can you access the namespace classes when using the open source code in an addon file?

            Can you export the custom namespace and classes as an addon from NinjaTrader?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I don't understand the question, the library I'm currently trying to integrate is very simple, it's mostly empty library and all that I'm doing in the strategy is to inherit a mostly empty class and instantiate it.
              In the editor I can access the classes in my library and it passes compilation.
              I cannot export my strategy as I wrote above.

              I guess I'm missing some basic knowledge of how to build a library for Ninja?

              Thanks

              Comment


                #8
                If I understand your question correctly, I have placed the source files of the library in the strategy folder and rebuilt it - it worked fine.
                However, this is an empty library that I'm using to overcome the integration problem - the real library is much bigger and will be harder to be placed there.

                Thanks

                Comment


                  #9
                  Hello haimcn,

                  "all that I'm doing in the strategy is to inherit a mostly empty class and instantiate it."

                  A strategy cannot inherit from a custom class. It can only inherit directly from the Strategy class.

                  You can use a partial class instead.
                  Explanation: I wrote a base class Indicator class that I'm using to inherit all my other indicators from. So this baseclass is defined as: namespace NinjaTrader.NinjaScript.Indicators.AssistedTrades { public class ATBaseIndicator: Indicator { ... } } And any other indicator is defined as: namespace NinjaTrader.NinjaScr
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    I'm not inheriting the strategy, it's an internal class within it.

                    Thanks

                    Comment


                      #11
                      Hello haimcn,

                      If these are classes you are constructing with the new keyword, in a class in a custom namespace, I don't see any violation there.

                      "However, this is an empty library that I'm using to overcome the integration problem - the real library is much bigger and will be harder to be placed there."

                      Can you clarify this?

                      The amount of code in an open source file or set of open source files shouldn't cause an issue.. even if it was thousands of lines it should eventually compile and export as long as there isn't some specific code causing an issue.
                      Are the libraries calling other libraries? Are there dependencies on dependencies?


                      Just to confirm, where I have asked:
                      "Can you access the namespace classes when using the open source code in an addon file?"

                      You are saying yes, correct? To the full code?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        It's not matter of size, it's more code management - I'm working on the library in a separate git and test environment and don't want to mix the library in the NinjaScript solution.
                        To help me resolve the integration problem, I created a small library that don't really do anything, just emulate the same function calls from the strategy - since I get the same errors in the small library it will be good enough to solve the problem there.
                        Both libraries don't call any other library, I'm making sure they are self-contained and don't have external dependencies.
                        And I used the full open source of the small library (but, again, it's the same problem).

                        Thanks again.

                        Comment


                          #13
                          Hello haimcn,

                          If you have empty methods and the same namespace -> class structure and you would be ok sharing the open source code I'd be happy to take a look.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi,

                            Thanks for helping, I attached the strategy and the test library - as you can see they are very simple.
                            (I may have bugs in the strategy as I couldn't run it in Ninja)

                            TestLibrary.zip

                            Thanks again.

                            Comment


                              #15
                              Hello haimcn,

                              I was not able to reproduce. The files you have provided export and import as open source or as assembly without any errors or issues.

                              Below is a link to a video of testing.
                              https://drive.google.com/file/d/11rG...w?usp=drivesdk

                              Possibly you have something else imported into NinjaTrader that is causing an issue.

                              Try creating a clean environment folder.
                              https://ninjatrader.com/support/foru...683#post783683
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              587 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              341 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              555 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              552 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X