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 Print() from custom class...

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

    Accessing Print() from custom class...

    Creating a custom class in my Wizard created indicator. Within the class I have:

    try {
    blah;
    }catch (Exception e){
    Print("blah blah blah");
    }

    Of course I'm getting an error trying to access the Print():
    "Cannot access a nonstatic member of outer type 'NinjaTrader.Indicator.IndicatorBase' via nested type 'NinjaTrader.Indicator.MyAppName.MyCustomClass'

    How do I make this work?

    #2
    Won't fly, since Print is method of an instance of class IndicatorBase but not your custom class.

    You would have to pass in an indicator instance to your class and then call myIndicator.Print(...).

    Comment


      #3
      Print()...

      Is that the best way to do it? Or is there another way?

      Comment


        #4
        Thats' the only way.

        Comment


          #5
          Another way to Print from a custom class

          I just tested this works in NT7:

          //1) Declare a delegate (say just before the Variables region):
          protected delegate void PrintString(string str);

          //2) Declare an event in your custom class:
          public event PrintString Prtstr;

          //3) Where you want to use Print("something important") in your class instead raise the event
          Prtstr("something important");

          //4) Create an event handler (could place this after Initialize):
          protected void PrintHandler(string str)
          {
          Print(str);
          }

          //5) Assuming you instantiate the class in Initialise as Obj1 then on next line wire up:
          Obj1.Prtstr+= new PrintString(PrintHandler);

          Comment


            #6
            I'm at the same point as the guy who did the 1st post on this thread.

            delegate might be neat but I can't be bothered writing a handler for everything I want to access outside the class - which will quickly add up (every method/function in the ninjascript manual)

            So what's the syntax to pass the pointer into the class? for a strategy in NT7.
            I'm looking to setup a global pointer passed in via the constructor so I can go ptr->Print(), ptr->TIme[], ptr->Low[], etc etc in the class. Or something similar to this. The next step is an array of class which is where I really want to get to - with a state machine running inside each class.

            This must be a common problem when you start writing your own classes.
            Last edited by PeterBear; 04-23-2011, 04:01 AM.

            Comment


              #7
              I've been through this and it was a pain to figure out.

              Here's the code to create an array "myClassArray" of class "MyClass" inside strategy "MyStrategy". You'll need to add your own parameters to the class constructor and method.

              Good luck,
              Kevin

              Code:
              public class MyStrategy : Strategy
              {
                  private MyClass[] myClassArray;
                  private int arraySize = 20;
              
                  public class MyClass
                  {
                      private MyStrategy parent;
                      private int arrayIndex;
              
                      public MyClass(MyStrategy parent, int index)
                      {
                          this.parent = parent;
                          arrayIndex = index;
                      }
                      public void MyMethod()
                      {
                          parent.Print("MyClass: Printing from arrayIndex: " + arrayIndex.ToString());
                          // can also refer to things like parent.CurrentBar, parent.Close[0], etc.
                      }
                  }
                  protected override void Initialize()
                  {
                  }
                  protected override void OnStartUp()
                  {
                      myClassArray = new MyStrategy.MyClass[arraySize];
                      for (int i=0; i < arraySize; i++)
                      {
                          myClassArray[i] = new MyStrategy.MyClass(this,i);
                      }
                  }
                  protected override void OnBarUpdate()
                  {
                      for (int i=0; i < arraySize; i++)
                      {
                          myClassArray[i].MyMethod();
                      }
                  }
              }

              Comment


                #8
                Cheers. Really appreicate it. That fills in the gaps. Figured someone else must have been down the same road before.

                Why do you use OnStartUp instead of Initialize? Read the Ninjatrader v7 manual. Obviously, I don't appreciate the difference between the two.
                Last edited by PeterBear; 04-23-2011, 07:17 AM.

                Comment


                  #9
                  Initialize() can get run multiple times before your indicator or strategy gets started, it's really for NT7 housekeeping. You need to add any instruments and plots in initialize (it's the only place you can add them), but don't do anything else there.

                  OnStartUp() gets run once when your strategy is started. It's the place to initialize everything else.

                  Comment


                    #10
                    Originally posted by kdoren View Post
                    I've been through this and it was a pain to figure out.
                    Wouldn't it be just as easy to force your custom class to inherit the methods available in the parent class? You do it by appending a colon and the name of the class to inherit from, in your custom class declaration.

                    The code above could be rewritten like this:
                    Code:
                    public class MyStrategy : Strategy {
                        private MyClass[] myClassArray;
                        private int arraySize = 20;
                    
                        public class MyClass : Strategy {  // " : Strategy" forces inheritance
                            private int arrayIndex;
                    
                            protected override void Initialize() { } // empty declaration needed for inheritance
                            protected override void OnBarUpdate() { } // ditto
                    
                            public MyClass(int index) {
                                arrayIndex = index;
                            }
                            public void MyMethod() {
                                Print("MyClass: Printing from arrayIndex: " + arrayIndex.ToString());
                                // can also refer to things like parent.CurrentBar, parent.Close[0], etc.
                            }
                        }
                        protected override void Initialize() {
                        }
                        protected override void OnStartUp() {
                            myClassArray = new MyStrategy.MyClass[arraySize];
                            for (int i=0; i < arraySize; i++) {
                                myClassArray[i] = new MyStrategy.MyClass(i);
                            }
                        }
                        protected override void OnBarUpdate() {
                            for (int i=0; i < arraySize; i++) {
                                myClassArray[i].MyMethod();
                            }
                        }
                    }
                    Would there be any problems with that approach?
                    -A
                    Last edited by anachronist; 04-27-2012, 04:51 PM.

                    Comment


                      #11
                      Continuing from Kevin's 04-23-2011 (code snippet below), is there a way to move from arrays to Lists or the System.Collections.CollectionBase while still permitting the all important access to NT methods etc (e.g. CurrentBar, Close[0] etc)?

                      Code:
                      public class MyStrategy : Strategy
                      {
                          private MyClass[] myClassArray;
                          private int arraySize = 20;
                      
                          public class MyClass
                          {
                              private MyStrategy parent;
                              private int arrayIndex;
                      
                              public MyClass(MyStrategy parent, int index)
                              {
                                  this.parent = parent;
                                  arrayIndex = index;
                              }
                              public void MyMethod()
                              {
                                  parent.Print("MyClass: Printing from arrayIndex: " + arrayIndex.ToString());
                                  // can also refer to things like parent.CurrentBar, parent.Close[0], etc.
                              }
                          }
                          protected override void Initialize()
                          {
                          }
                          protected override void OnStartUp()
                          {
                              myClassArray = new MyStrategy.MyClass[arraySize];
                              for (int i=0; i < arraySize; i++)
                              {
                                  myClassArray[i] = new MyStrategy.MyClass(this,i);
                              }
                          }
                          protected override void OnBarUpdate()
                          {
                              for (int i=0; i < arraySize; i++)
                              {
                                  myClassArray[i].MyMethod();
                              }
                          }
                      }

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by adeelshahzad, Today, 03:54 AM
                      4 responses
                      22 views
                      0 likes
                      Last Post adeelshahzad  
                      Started by merzo, 06-25-2023, 02:19 AM
                      10 responses
                      823 views
                      1 like
                      Last Post NinjaTrader_ChristopherJ  
                      Started by frankthearm, Today, 09:08 AM
                      5 responses
                      15 views
                      0 likes
                      Last Post NinjaTrader_Clayton  
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      3 responses
                      43 views
                      0 likes
                      Last Post jeronymite  
                      Started by yertle, Today, 08:38 AM
                      5 responses
                      16 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Working...
                      X