Announcement

Collapse
No announcement yet.

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.

    https://ninjatrader.com/support/help...sub=references

    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, 11:42 AM.

        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, 12: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

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cincai, Today, 07:02 PM
            0 responses
            1 view
            0 likes
            Last Post cincai
            by cincai
             
            Started by AlexsOptions, Today, 05:42 PM
            2 responses
            12 views
            0 likes
            Last Post AlexsOptions  
            Started by philli4093, Today, 05:20 PM
            2 responses
            8 views
            0 likes
            Last Post philli4093  
            Started by memonolog, Today, 04:58 PM
            0 responses
            4 views
            0 likes
            Last Post memonolog  
            Started by robvul, Today, 04:20 PM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_ShawnB  
            Working...
            X