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

Accessing non static members

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

    Accessing non static members

    Hello,
    I'm attempting to use NT methods in a nested class and I'm getting an error: Cannot access a non-static member of out type " " via nested type " ". Is there a way to make NT methods static or is there another workaround? Thanks.

    #2
    Hello CaptainAmericaXX,

    Thanks for your message. Using inheritance and other more advanced uses of C# may interfere with how our NinjaScript wrapper works for generating indicator code and other advanced uses of the C# language can carry other complications.

    As such it can extend beyond our scope of services to provide further direction with advanced C# concepts with NinjaScript. That being said, we provide some direction for creating static variables/methods and using partial classes on our Code Breaking Changes page of the HG.



    Also available is an example my colleague Chelsea has posted where these partial classes are created in a NinjaScript in the AddOns folder.



    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks Jim,
      I'm too cheap to pay a consultant $150 an hour so I'll just have to keep learning on my own.
      Those examples you gave me we for NT 8. I'm still plugging along with NT 7. Do you have any suggestions for 7?
      btw my understanding is limited, but I have a basic understanding of static members and partial classes. I promise I won't sue NT if you give me just a little more advice and I end up breaking something.
      Last edited by CaptainAmericaXX; 05-20-2019, 09:12 AM.

      Comment


        #4
        Hello CaptainAmericaXX,

        Thanks for the reply,

        The information above shows how the UserDefinedMethods approach can be used in NinjaTrader 8. This script comes shipped with NinjaTrader 7 and can be opened from the Control Center under Tools > Edit NinjaScript > Indicator...

        Documentation page - https://ninjatrader.com/support/help...ed_methods.htm

        Please let us know if there is anything else we can do to help.
        JimNinjaTrader Customer Service

        Comment


          #5
          Thanks Jim,
          I am familiar with the UserDefinedMethods file and have created a few dozen methods, but I'd like the option of using the NT methods so I don't have to recreate the wheel. I believe I'm on to something, but I can't figure out the last part. I can at least get the Intellisence to open up the NT methods (I'm using LowestBar()), but I'm getting an error cs1519 Invalid token ('(' & ',') in class, struct, or interface member declaration. Here's the code:
          Code:
           partial class Indicator
              {
                public class TestClass
                  {
                      private Indicator parent;
          
                      public TestClass(Indicator parent)
                      {
                          this.parent = parent;
                      }
                      Indicator.TestClass testClass = new Indicator.TestClass();
                      testClass.parent.LowestBar(Low, 30);
          
                  }
                }
          This dog is still hungry! Can you throw me a few more bones please?

          Comment


            #6
            Hello CaptainAmericaXX,

            We can leave this thread open for any input from other community members, but we could not comment further on this topic as it involves more advanced C# which would be outside of what we can support with NinjaScript.

            Unsupported does not mean this is not possible, it just means that the concepts go beyond what we can provide direction for.

            If there is another NinjaScript topic we can assist with, please do not hesitate to open new thread.
            JimNinjaTrader Customer Service

            Comment


              #7
              Ok so using a slightly modified version of what I wrote above I am able to at least get a few bars running on my chart, at least until the method runs and then I get the error: Object ref not set to an instance of an object. I created an instance at the beginning of the method. I'm not sure where else I need to create the instance.
              Code:
              public class LL_Up
              {
                          private A1WaveFinder parent;
              
                          public LL_Up()
                          {
              
                          }
                          public LL_Up(A1WaveFinder parent)
                          {
                              this.parent = parent;
                          }
              
              public void Find_Lower_Low(IDataSeries open, IDataSeries close, IDataSeries low, IDataSeries price_Valley)
                          {
                              A1WaveFinder.LL_Up llup = new A1WaveFinder.LL_Up();
              
                              if(UpBar(open,close) &&
                                  low[1] > low[0]){
              
                                        llup.parent.Print("Working"); //Adding the Print() is causing the crash!
              }
              
              }
              protected override void OnBarUpdate()
              {
              if(CurrentBar < 50)
                              return;
              LL_Up lowerLow_Up1 = new LL_Up();
              
                   if(lowerLow_Up1 != null){
                   lowerLow_Up1.Find_Lower_Low(Open, Close, Low, Price_Valley);
                   }
              }
              Adding the Print() is causing the crash as soon as the method is true. If anyone can help me with this I would be very appreciative!

              Sorry the code is a mess. The copy and paste didn't work very well.

              Comment


                #8
                Originally posted by CaptainAmericaXX View Post
                Ok so using a slightly modified version of what I wrote above I am able to at least get a few bars running on my chart, at least until the method runs and then I get the error: Object ref not set to an instance of an object. I created an instance at the beginning of the method. I'm not sure where else I need to create the instance.
                Code:
                public class LL_Up
                {
                private A1WaveFinder parent;
                
                public LL_Up()
                {
                
                }
                public LL_Up(A1WaveFinder parent)
                {
                this.parent = parent;
                }
                
                public void Find_Lower_Low(IDataSeries open, IDataSeries close, IDataSeries low, IDataSeries price_Valley)
                {
                A1WaveFinder.LL_Up llup = new A1WaveFinder.LL_Up();
                
                if(UpBar(open,close) &&
                low[1] > low[0]){
                
                llup.parent.Print("Working"); //Adding the Print() is causing the crash!
                }
                
                }
                protected override void OnBarUpdate()
                {
                if(CurrentBar < 50)
                return;
                LL_Up lowerLow_Up1 = new LL_Up();
                
                if(lowerLow_Up1 != null){
                lowerLow_Up1.Find_Lower_Low(Open, Close, Low, Price_Valley);
                }
                }
                Adding the Print() is causing the crash as soon as the method is true. If anyone can help me with this I would be very appreciative!

                Sorry the code is a mess. The copy and paste didn't work very well.
                It might be better if you explained what you are trying to do. There may be a better, more robust way than you have designed. What is the idea behind "parent"?

                Comment


                  #9
                  Give this a try ....

                  Code:
                  public class LL_Up
                  {
                  
                      private A1WaveFinder parent;
                  
                      public LL_Up(A1WaveFinder instance)
                      {
                          parent = instance;
                      }
                  
                      public void Find_Lower_Low()
                      {
                           if (parent.UpBar(parent.Open, parent.Close) && parent.Low[1] > parent.Low[0])
                               parent.Print("Working");
                      }
                  }
                  
                  protected override void OnBarUpdate()
                  {
                       if (CurrentBar < 50)
                           return;
                  
                       // you must pass 'this' on every new object creation
                       LL_Up llup = new LL_Up(this);
                  
                       // if a new lower low is found, "Working" is printed
                       llup.Find_Lower_Low();
                  }
                  Now that you have a working sample, that is a lot of trouble to go through for
                  the method Find_Lower_Low().

                  I mean, that method could be coded much easier directly within the scope of
                  your A1WaveFinder strategy, rather than being embedded in a nested class.

                  Nevertheless, I hope it helps your understanding of how to call things inside
                  a nested class that are 'owned' by the parent strategy class (such as Print).

                  Good luck!
                  Last edited by bltdavid; 05-21-2019, 09:45 AM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by dustydbayer, Today, 01:59 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post dustydbayer  
                  Started by inanazsocial, Today, 01:15 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post inanazsocial  
                  Started by trilliantrader, 04-18-2024, 08:16 AM
                  5 responses
                  22 views
                  0 likes
                  Last Post trilliantrader  
                  Started by Davidtowleii, Today, 12:15 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post Davidtowleii  
                  Started by guillembm, Yesterday, 11:25 AM
                  2 responses
                  10 views
                  0 likes
                  Last Post guillembm  
                  Working...
                  X