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

Adding References (From Visual Studio)

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

    Adding References (From Visual Studio)

    Problem (What actually happens)

    When using Visual Studio I can `Add References` or install a `Nuget` Package. In my editor there are no red underlines when I am `using` these packages. This suggests that the package is resolving correctly. When I compile in NinjaTrader it can not find said package.

    Another interesting thing is that the package looks resolved until `NinjaTrader` is opened again then the packages `reference` is not longer available and all imports are not working in `Visual Studio` as well.

    Expected

    I would have thought I could install `Nuget` Packages from `Visual Studio` and used them while compiling in `NinjaTrader`.

    Question

    My question is how can I bring in an external package (.dll library) into my project for code use?

    #2
    Hello Zeero,

    The reason you are seeing that the platform cannot find the reference and also why when you open the project the references have changed is that NinjaTrader is re-generating the .csproj on its own.

    When using visual studio with NinjaTrader you will not be able to use many of the features in visual studio directly. It mainly is just a text editor in contrast to NinjaScript. You cannot use nuget or compile from visual studio because NinjaTrader is managing the solution and is in charge of compiling/adding references.

    To use external references you would need to acquire the .dll files and any other files that go with the library in question and place them into the Documents\NinjaTrader 8\bin\Custom folder. Finally you need to use the NinjaScript editors Right click -> References menu to add the references. Once you add references you can compile from inside of NinjaTrader to update the visual studio project.



    If you are using nuget to get the package, you could make an external visual studio .net 4.5 project (not the ninjascript project from the editor) and add the nuget package to it. Finally build the project to collect the dll's from the output folder likely bin/debug. You can delete the project after collecting the files you needed you just need to copy them to the NinjaTrader user folder bin/Custom directory. Otherwise you can look on the developers website/github or wherever is linked with the package you are trying to use to see if manual downloads are available. You need .net desktop 4.5 or less for the package.



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

    Comment


      #3
      NinjaTrader_Jesse Thank you for the amazing reply. That answers my question in full.

      Comment


        #4
        Problem:

        I Get errors when importing and trying to use `.dll` files in `NinjaTrader`. I don't get this same error when importing the .dll into a Visual Studio code project and running a unit test on one of the methods.

        How To Reproduce Issue:

        Create a new `library` project in `Visual Studio` that uses `.NET Framework 4.5`. Build the project and this creates a `dll` in my `Debug` folder for the project. Use the following code for the project

        Code:
        using System;
        
        namespace PeakFinder {
        
            public class PeakFinder {
        
                public static string test() {
                    return "Got to the method";
                }
            }
        }
        Move the `.dll` into `NinjaTrader 8\bin\Custom\references`. Opening the `references` menu in `NinjaTrader` add the `.dll`. Create an `indicator` that
        Code:
        using PeakFinder;
        and calls the following
        Code:
        wasPeak2BarsAgo = PeakFinder.test();
        Open a chart and go to the menu to add an indicator and an error message like the following should show up.
        Indicator 'aaYourIndicator': Error on calling 'OnStateChange' method: Could not load file or assembly 'PeakFinder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
        Questions:

        Is there something in this process I'm doing wrong?
        My project doesn't feel like it has much in dependencies why can it not load the assembly?
        Last edited by Zeero; 12-31-2019, 12:42 PM.

        Comment


          #5
          Hello Zeero,

          Edit: I think I may see the problem, the dll needs to go in the folder bin/Custom and you have used bin/custom/references. All dlls need to be in the root bin/custom folder. you will need to remove the reference from the NinjaScript editor, close NinjaTrader and then move the file and re reference it.


          From the given details that sounds correct, I am not seeing anything missing here. I can see that you are using a same name class/namespace so accessing that would instead look like:

          Code:
          Print(PeakFinder.PeakFinder.test());
          That shouldn't cause the error you are reporting though if you failed to access it correctly, it would look like:
          The type or namespace name 'test' does not exist in the namespace 'PeakFinder' (are you missing an assembly reference?)
          I would likely suggest to try making the class in the same namespace as the type you are using to make it easy to reference/use for example placing your class in the namespace namespace NinjaTrader.NinjaScript.Indicators would result in using it like the following with no added using statements:
          Code:
          Print(PeakFinder.test());
          Code:
          namespace NinjaTrader.NinjaScript.Indicators
          {
              public class PeakFinder
              {
                  public static string test()
                  {
                      return "Got to the method";
                  }
              }
          }

          I can see this working on my end, the steps were as you wrote:
          compile from VS
          Copy DLL to bin/custom folder
          Reference DLL in NinjaTrader
          Compile
          Use code in your script


          If you cant get it working could you try to zip the project and attach it? I don't believe you can attach files with dlls here so clearing the debug folder before you attach it may be required.


          I look forward to being of further assistance.
          Last edited by NinjaTrader_Jesse; 12-31-2019, 01:11 PM.
          JesseNinjaTrader Customer Service

          Comment


            #6
            It worked! Thank you I've been looking at both these issues for many hours and could not figure them out.

            Sorry about the namespace name. I changed it in my post to attempt to make it simpler to read.

            ​​​​​​​You have been most helpful NinjaTrader_Jesse

            Comment


              #7
              Hello Jesse,

              To use external references you would need to acquire the .dll files and any other files that go with the library in question and place them into the Documents\NinjaTrader 8\bin\Custom folder.
              How do I know which ones of other files to add that go with the .dll?

              I'm trying to get this Nuget package content into the NT8 Custom folder
              MathNet.Numerics

              (I've downloaded version 4.15.0 from the page/no visual studio)

              The root folder Content
              C:\Users\user\Desktop\New folder (8)\_rels
              C:\Users\user\Desktop\New folder (8)\lib
              C:\Users\user\Desktop\New folder (8)\package
              C:\Users\user\Desktop\New folder (8)\.signature.p7s
              C:\Users\user\Desktop\New folder (8)\[Content_Types].xml
              C:\Users\user\Desktop\New folder (8)\icon.png
              C:\Users\user\Desktop\New folder (8)\MathNet.Numerics.nuspec
              Click image for larger version  Name:	7zFM_L7RnxOY2NS.png Views:	0 Size:	38.9 KB ID:	1193175
              The lib folder content
              C:\Users\user\Desktop\New folder (8)\lib\net40\MathNet.Numerics.dll
              C:\Users\user\Desktop\New folder (8)\lib\net40\MathNet.Numerics.xml
              Click image for larger version  Name:	chrome_NEOpZTJEu0.png Views:	0 Size:	30.9 KB ID:	1193174
              The package folder content
              C:\Users\user\Desktop\New folder (8)\package\services\metadata\core-properties\2a33219537e2439e9b6680f04f051bed.psmdcp
              Click image for larger version  Name:	7zFM_sAbGh1iri8.png Views:	0 Size:	28.0 KB ID:	1193173
              the .rels folder content
              C:\Users\user\Desktop\New folder (8)\_rels\.rels
              Click image for larger version  Name:	7zFM_mc69cIqf8w.png Views:	0 Size:	25.1 KB ID:	1193172
              Thanks!
              Last edited by PaulMohn; 03-10-2022, 08:20 AM.

              Comment


                #8
                Hello PaulMohn,

                I wouldn't really be able to say because I am not familiar with that library, you can however find out by using visual studio. The easiest way to get nuget references for NinjaTrader would be the following process:
                1. Go to the following link and download the addon visual studio project: https://ninjatrader.com/support/help...t_overview.htm
                2. Open the project in visual studio
                3. Add the nuget reference that you wanted to use by using visual studios nuget manager.
                4. F6 build the solution
                5. You can now view the bin/debug folder from that solution to see what files got copied, some nuget packages have extra files or build actions that require copying or creating specific files/structures.
                6. Copy the files you need to the bin/custom folder so you can reference any needed dlls.

                This process does copy the addon dll to the bin/custom folder as well, to delete that open the folder Documents\NinjaTrader 8\bin\Custom and remove the addon dll that was copied.
                This also makes sure the target .net version is the correct one for NT8.


                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thanks Jesse,

                  I've downloaded the Addon archive, unziped on my desktop and double clicked the .sln file.
                  I'm getting this
                  Click image for larger version  Name:	vmplayer_4dVwh7aGFW.png Views:	0 Size:	13.3 KB ID:	1193269
                  What should I do next? Isn't NT8 4.5? Thanks!

                  Comment


                    #10
                    Hello PaulMohn,

                    NT8 is now 4.8, to develop 4.8 assemblies you would need to install the targeting pack.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks.
                      I had to download 4.8, install it, then close visual studio and relaunch it for it to load the addon project.

                      1. Go to the following link and download the addon visual studio project:
                      https://ninjatrader.com/support/help...t_overview.htm
                      Click image for larger version  Name:	0download.png Views:	0 Size:	1.02 MB ID:	1193290
                      Click image for larger version  Name:	Extract.png Views:	0 Size:	59.3 KB ID:	1193291

                      2. Open the project in visual studio
                      Click image for larger version  Name:	0.1Open.png Views:	0 Size:	223.1 KB ID:	1193292

                      3. Add the nuget reference that you wanted to use by using visual studios nuget manager.
                      Click image for larger version  Name:	2.png Views:	0 Size:	1.37 MB ID:	1193294
                      Click image for larger version  Name:	3.png Views:	0 Size:	1.14 MB ID:	1193293
                      Last edited by PaulMohn; 03-10-2022, 04:42 PM.

                      Comment


                        #12
                        Click image for larger version  Name:	4.png Views:	0 Size:	1.41 MB ID:	1193318


                        Click image for larger version  Name:	5.png Views:	0 Size:	1.22 MB ID:	1193319

                        4. F6 build the solution

                        Click image for larger version  Name:	Build.png Views:	0 Size:	1.43 MB ID:	1193320

                        Click image for larger version  Name:	7.png Views:	0 Size:	1.36 MB ID:	1193321

                        5. You can now view the bin/debug folder from that solution to see what files got copied, some nuget packages have extra files or build actions that require copying or creating specific files/structures.
                        I found there are only 2 new files (the left window is the new folder bin/debug folder, while the right window is the folder still in the archive)

                        Click image for larger version  Name:	new files.png Views:	0 Size:	475.6 KB ID:	1193322
                        Last edited by PaulMohn; 03-10-2022, 04:45 PM. Reason: I found there are only 2 new files (the left window is the new folder bin/debug folder, while the right window is the folder still in the archive)

                        Comment


                          #13
                          6.Copy the files you need to the bin/custom folder so you can reference any needed dlls.
                          Click image for larger version

Name:	copy.png
Views:	560
Size:	286.1 KB
ID:	1193323​​

                          Are the ones needed only the new files from the bin/Custom or are there new files elsewhere I should also copy to Documents\NinjaTrader 8\bin\Custom? Thanks!
                          Last edited by PaulMohn; 03-10-2022, 04:39 PM.

                          Comment


                            #14
                            I've added
                            PHP Code:
                            using MathNet.Numerics.Statistics
                            to my declarations region but I get

                            NinjaScript File Error Code Line Column
                            Testa.cs The type or namespace name 'MathNet' could not be found (are you missing a using directive or an assembly reference?) CS0246 25 7
                            Why isn't it detecting it?

                            Comment


                              #15
                              Hello PaulMohn,

                              Not all of your images loaded from post 12 on. From the details I can see you would need to copy the dlls from the addon projects build folder into to the bin/custom folder and then reference any dlls which are needed to use that namespace. The addon project has a bin/debug sub folders and the build data for visual studio exists there.



                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by XXtrader, Yesterday, 11:30 PM
                              2 responses
                              11 views
                              0 likes
                              Last Post XXtrader  
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by TradeForge, Today, 02:09 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post TradeForge  
                              Started by Waxavi, Today, 02:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by elirion, Today, 01:36 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Working...
                              X