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

Customized indicator with dll

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

    Customized indicator with dll

    Hi,

    I have some troubles while editing a new indicator.

    My purpose would be to create a customized indicator with a function called from a dll.
    Here is a simple example :

    Code:
     *** Variables region ***
            
            [COLOR=DarkOrange][DllImport("MyCDll.dll")] 
            static extern double Identity(double value);[/COLOR]
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Aquamarine), PlotStyle.Line, "Plot0"));
                Overlay                = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
                [COLOR=DarkOrange]Plot0.Set(Identity(Close[0]));[/COLOR]
            }
    
    *** Properties region ***
    My function Identity is a basic function which returns the value passed in parameter (just for testing) :

    Code:
    extern "C" double Identity(double value)
    {
        return value;
    }
    A problem occurs as I try to add a reference (right click ->References)
    I select MyCDll.dll which contains the Identity function code and then I confirm.

    This message appears a the bottom of the ninja script window :

    Impossible d'ouvrir le fichier de métadonnées 'MyCdll.ddl' Tentaive de chargemet d'un programme de format incorrect
    which means that the dll can't be read.

    The dll is generated under Visual Studio 2010 for both 32 and 64 bits platforms.
    The message is the same no matter what is the platform selected.

    Can someone rescue me?

    The error seems coming from the C# compiler but nothing in the web was useful from me.

    Thanks,
    Nasire.
    Last edited by Nasire; 08-17-2012, 09:33 AM.

    #2
    Hello Nasire,
    NinjaScript is all C# and you can call the dll using normal pinvoke. This is however beyond what we can support however I will leave the thread open for any of our forum member who give their valuable inputs.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Check out this Dr Dobbs article and look at listing one

      Need to utilize legacy software? Here are techniques for calling unmanaged code written in C from C#.


      Are you using __declspec(dllexport) in your C function?

      Comment


        #4
        Hi,

        I have made some correction.

        Now, my dll looks like :

        In the header :

        Code:
        extern "C" __declspec(dllexport) double Identity(double value);
        In the body :

        Code:
        #include "Identity.h"
        
        extern "C" __declspec(dllexport) double Identity(double value)
        {
            return value;
        }
        As you can see, I have added the directive __declspec(dllexport) (thanks for the piece of advice)

        In NinjaTrader, I go to edit->indicator->MyCustomIndicator

        I make reference-> Add -> MyCDll -> Ok

        It does compile (before it doesn't)

        I write the same code than I did before :

        Code:
         *** Variables region *** 
                [COLOR=DarkOrange][DllImport("MyCDll.dll")] 
                static extern double Identity(double value);
        [/COLOR]
                /// <summary>
                /// This method is used to configure the indicator and is called once before any bar data is loaded.
                /// </summary>
                protected override void Initialize()
                {
                    Add(new Plot(Color.FromKnownColor(KnownColor.Aquamarine), PlotStyle.Line, "Plot0"));
                    Overlay                = true;
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                    // Use this method for calculating your indicator values. Assign a value to each
                    // plot below by replacing 'Close[0]' with your own formula.
                    [COLOR=DarkOrange]Plot0.Set(Identity(Close[0]));[/COLOR]
                }
                         
        *** Properties region ***
        Theorically, it should works.

        I create a new chart, I load some historical data and I import my indicator...

        And surprise..NOTHING. (no plot)

        I tried to write a C# programme under VS2010 calling the same dll, and it works.

        When I try to enable the debug mode, NinjaTrader just crash.

        And I was unable to find some log or trace that can help me.

        So, can someone help me?

        Nasire.
        Last edited by Nasire; 08-21-2012, 09:15 AM.

        Comment


          #5
          Hi again,

          passing the details, I can debug now.

          While debugging using VS2010, I realized that the dll is not found (which was what expected, but now I know for sure that here is the problem)

          Therefore, my question is, to putt it in a nutshell :

          How to call a dll inside an indicator????????
          Reminder :

          In my indicator file I have added those two lines

          Code:
                  [DllImport("MyCDll.dll")] 
                  static extern double Identity(double value);
          and I have added the dll in the list of references (right click -> add references).

          In others words,
          Can someone tell me what is missing for NinjaScript to find the dll?

          Thanks.
          Nasire.

          PS : I have also read the NinjaTrader Tutorial about 3rd party, and consequently, I have copied my dll into the designated folder, without results.
          Last edited by Nasire; 08-21-2012, 10:15 AM.

          Comment


            #6
            is your dll located here:
            C:\Users\{your-user-name}\Documents\NinjaTrader 7\bin\Custom

            (path may be slightly different depending on your windows version)

            i wonder if NinjaTrader can read the dll if it's in another program location (permissions, etc.) - just a thought.

            cheers,
            -e

            Comment


              #7
              My dll was copied to the folder :
              C:\Users\{your-user-name}\Documents\NinjaTrader 7\bin\Custom

              (the same you mentionned)

              But I think the problem is not here as I have added my dll to the list of references.

              Under VS2010 the same operations (adding the two lines with dllimport and the function declaration & adding the dll as ref.) are enough.
              So I think, there is something specific to NinjaScript, but what???!!

              Comment


                #8
                last time i worked with DLL, i did find some benefit in restarting NT after each build.

                this thread mentions "using System.Runtime.InteropServices;":


                there are a couple other threads dealing with DLLImport and a few specific to unmanaged C code ... but this one caught my eye:


                have you tried removing the references and just using the DLLImport directive?

                cheers,
                -e

                Comment


                  #9
                  (The ddl is added in the list of references)

                  I build my indicator. I restart NT. I open a chart and I test : nothing (no plot)

                  I remove the dll from the list of references then :

                  I build my indicator. I restart NT. I open a chart and I test : nothing (no plot)

                  After reading the second link you posted, something appears being strange :
                  I have tested my dll under a C# program written under VS2010 before moving to NT.
                  My dll is managed and I need to use dllimport directive (contrary to what is said) in the program writen under VS2010.

                  Do you have other suggestions?

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by jaybedreamin, Today, 05:56 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post jaybedreamin  
                  Started by DJ888, 04-16-2024, 06:09 PM
                  6 responses
                  18 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by Jon17, Today, 04:33 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post Jon17
                  by Jon17
                   
                  Started by Javierw.ok, Today, 04:12 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post Javierw.ok  
                  Started by timmbbo, Today, 08:59 AM
                  2 responses
                  10 views
                  0 likes
                  Last Post bltdavid  
                  Working...
                  X