Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Partial Class does not work!

Collapse
X
 
  • 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.

    #2
    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.

    Example in post # 6.
    Last edited by NinjaTrader_ChelseaB; 01-24-2023, 10:51 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      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:
      High
      EMA

      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.

      Thanks!

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

      Comment


        #4
        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

        Comment


          #5
          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

          Comment


            #6
            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.

            MySharedMethodsAddonExample.zip

            Click image for larger version  Name:	1-7-2016 8-51-31 AM.png Views:	1327 Size:	31.6 KB ID:	877203
            Last edited by NinjaTrader_ChelseaB; 01-24-2023, 10:52 AM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              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.

              Comment


                #8
                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.
                http://ninjatrader.com/support/helpG...ng_changes.htm
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  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...

                  Code:
                  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)

                  Cheers
                  Frank

                  Comment


                    #10
                    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
                    Code:
                    NinjaTrader.NinjaScript.AddOns.MySharedMethods.SharedDouble
                    . Instead of a double use a method.
                    Last edited by NinjaTrader_ChelseaB; 07-11-2016, 08:16 AM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Barry Milan, Yesterday, 10:35 PM
                    5 responses
                    16 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by DanielSanMartin, Yesterday, 02:37 PM
                    2 responses
                    13 views
                    0 likes
                    Last Post DanielSanMartin  
                    Started by DJ888, 04-16-2024, 06:09 PM
                    4 responses
                    13 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by terofs, Today, 04:18 PM
                    0 responses
                    12 views
                    0 likes
                    Last Post terofs
                    by terofs
                     
                    Started by nandhumca, Today, 03:41 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post nandhumca  
                    Working...
                    X