• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Partial Class does not work!

  • Filter
  • Time
  • Show
Clear All
new posts

    Partial Class does not work!

    From here:

    I copied the partial class code into AddOns directory. Ninja detected the file and pulled it up just fine. However, when I compile, I get

    The type or namesapce 'Position' could not be found (are you missing a using directive or assembly reference?)

    Please help. Really need this to work
    Last edited by stevenev; 01-04-2016, 10:58 AM.

    Hello stevenev,

    The Position is not part of the Indicators namespace.

    To use Position, this code would need to be in the Strategies namespace.

    Attached is an example of how this code would be used between namespaces.

    Also, print cannot be used in a static method.
    Instead the NinjaTrader.Code.Output.Process(string message, PrintTo output tab); would be used.
    Attached Files
    Chelsea B.NinjaTrader Customer Service


      Hi Chelsea, your code is working great. I can call a method in the AddOns > MySharedMethods from my main strategy script.

      NOW, can you please help me with the reverse. In the AddOns > MySharedMethods, I need to use:

      for example in the MySharedMethods, I would have:
      public static double Range( int BarsAgo )
      return High[BarsAgo] - Low[BarsAgo]; // but this does not work

      public static EMA_val( int span, int BarsAgo )
      return EMA(span)[BarsAgo];

      How would I access them. I believe there is namespace prefix to be used, I just don't know how. Please help.


      Last edited by stevenev; 01-04-2016, 07:54 PM.


        Hello Steve,

        An addon is not applied to a chart the way an indicator or a strategy is applied to the chart. Because of this there is not an automatic creation of any bar data the way there is in an indicator that has been added to a chart.

        You would need to request bar data from the addon. You can also subscribe to bar updates and market data updates from an indicator.

        BarRequest - http://ninjatrader.com/support/helpG...arsrequest.htm

        MarketData - http://ninjatrader.com/support/helpG...marketdata.htm
        Chelsea B.NinjaTrader Customer Service


          Hi Chelsea,

          In NT7, I put all my 'helper' functions in 'UserDefinedMethods' script and everything works. I have access to bar open/close/etc and indicators.

          In NT8, from the solution provided, doing the same thing is extremely complex - actually the c# part is hard for me.

          I am assuming any NT programmer would have a need for an easy way to replace methods they had in UserDefinedMethods. What is the solution.

          Thanks, steve


            Hello steve,

            With NinjaTrader 7, the High, Close, Open, etc series are not available to the UserDefinedMethods.cs file either. A screenshot is attached to demonstrate what happens.

            Are you passing these dataseries to your method?

            If so, you can do the same in NT8.

            I've modified the indicator script for to demonstrate passing in a dataseries and doing a calculation.

            I've modified the addon script because i realized that adding classes that extend the Indicator class or Strategy class will cause those classes to appear in the Indicator / Strategy windows.
            To hide these from the window, instead of an extended partial class, its a partial class of Indicator (without extending it). Then I put a nested class called MySharedMethods within this for organization reasons, however, this is optional. (I've left a method outside of that inner class so we can we how to use a method directly without a nested container class).

            As a heads up, static classes and methods are outside of what is supported by NinjaTrader Support and is not documented in the NinjaTrader 8 Help Guide.
            Attached Files
            Last edited by NinjaTrader_ChelseaB; 04-23-2019, 09:00 AM.
            Chelsea B.NinjaTrader Customer Service


              Hi Chelsea,

              In NT7, it actually works. With the code provided above, I think STATIC is the problem, as that requires objects instantiation. Also the namespace in UserDefinedMethods is the same as for strategies, making things simple (you can tell I am not a c#'ian...).

              Here is my code snippet in UserDefinedMethods. I just call it in my strategies with fGreen(0) and that is all I have to do

              I looked at your code and it works great. But when using "static" the code has to pass ISeries all the time, gets repetitive when one has to call multiple methods in the helper. If you don't make the methods static, because it is in the same namespace, would it not be available just the same way I am calling it?

              // my UserDefinedMethods

              namespace NinjaTrader.Strategy
              /// <summary>
              /// This file holds all user defined strategy methods.
              /// </summary>
              partial class Strategy

              public bool fGreen( int BarsBack )

              return Close[BarsBack] > Open[BarsBack];

              Last edited by stevenev; 01-07-2016, 10:43 AM.


                Hi stevenv,

                Yes, you are correct it is the static modifier that prevents the internal data series from an instance of indicator or strategy from being shared.

                The same is true of NT8.

                The choice of making these static is up to you, however, keep in mind the following note from the help guide:
                Note: Methods within partial classes should be use the "public" and "static" modifiers, to allow for any other classes to invoke the methods without requiring an instance of the partial class.
                Chelsea B.NinjaTrader Customer Service


                  Shared Partial Class across Strategies and Indicators

                  Hi Chelsea,

                  I've been reading this thread with great interest. I am seeking to implement some methods that are meant to be shared across the Strategy and Indicator space. For instance a simple debug print statement like the following...

                  public void P(string msg, int minLvl = 1){
                      string intro = DateTime.Now.ToLongTimeString() + " - Bar: " + Time[0].ToShortDateString() + " " + Time[0].ToLongTimeString() + " (" + CurrentBar.ToString() + "): ";
                      Print(intro	+ "OHLC: " + Open[0] + "/" + High[0] + "/" + Low[0] + "/" + Close[0] + " - " + msg);	
                  ...I'd like that to be accessible from an indicator and strategy alike with:
                  P("Test Message");

                  Is there any way to achieve this? (i.e. truly SHARED methods)



                    Hello Frank,

                    To access this from any script the method would need to be added to the NinjaTrader.NinjaScript namespace which may be difficult to acheive.

                    Instead the best way would be to use the fully qualified name. In the example in post #6 I've done exactly this with the
                    . Instead of a double use a method.
                    Last edited by NinjaTrader_ChelseaB; 07-11-2016, 08:16 AM.
                    Chelsea B.NinjaTrader Customer Service


                    Latest Posts


                    Topics Statistics Last Post
                    Started by firstlanetech, Today, 01:05 AM
                    0 responses
                    Last Post firstlanetech  
                    Started by HiddenPhilosopher, Yesterday, 11:58 PM
                    0 responses
                    Last Post HiddenPhilosopher  
                    Started by glennts, Yesterday, 08:24 PM
                    2 responses
                    Last Post bltdavid  
                    Started by ibabinov, 11-12-2019, 09:50 AM
                    3 responses
                    Last Post NinjaTrader_ShawnB  
                    Started by PrimoItch, 05-27-2020, 04:35 PM
                    2 responses
                    Last Post bkonia
                    by bkonia