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

"Print" declaration outside scope within a class

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

    "Print" declaration outside scope within a class

    Hello NT!


    I am using a class implementation within a custom strategy/indicator.

    When attempting to debug, I normally use "Print" statements. However, using "Print" within class methods results in compile error, Print declaration outside of scope.......

    Is there a way around this ?

    Curious also how the Ninjatrader Output Window that uses Print, relates to c# Console.WriteLine ? I should be able to use the System console writeLine within class methods......

    #2
    pvtrader,

    To use Print() in your own class instances you could pass a indicator/strategy reference by construction (this). Then you have access to the public interface of that indicator/strategy.

    Regards
    Ralph

    Comment


      #3
      Thanks Ralph for your prompt reply.

      It helps to debug the innards of a class by using Print statements embedded at various points within the code, instead of examining it as a blackbox as it were, via its' public interface.

      Using "Print" results in compiler error -> "The name 'Print' does not exist in the current context.

      Using c# 'Console.WriteLine' compiles with no errors.

      So the question comes to mind, how to re direct Console.WriteLine to NinjaTraders' Output Window?

      Or, what do I need to do in order to include a declaration of 'Print' within the class definition?

      Comment


        #4
        I don't know how to bind the console to the output window. I use Print() only. Here is an example:

        class MyClass
        {
        private Indicator parent;
        public MyClass(Indicator indi)
        {
        parent = indi;
        }
        public void MyPrint()
        {
        parent.Print("here we go");
        }
        }

        ... somewhere in your regular indicator class:

        MyClass myClass = new MyClass(this);
        myClass.MyPrint();

        Regards
        Ralph

        Comment


          #5
          Thanks again for quick reply.

          That is slick. Using the class to inherit method from parent. Very nice.

          Another question if I may. Attached is sample code of a Strategy that uses your approach. Works fine.

          Only one glitch. I would like to have only one instance of class that persists during the lifetime of the Strategy. The sample code I attached instantiates class within the Initialize method and terminates on return from Initialize.

          I don't want to instantiate class within OnBarUpdate, only want one instance of.

          So where to instantiate ?
          Attached Files

          Comment


            #6
            Is the strategy code NT6.5 or NT7?

            Comment


              #7
              Using NT 7

              Comment


                #8
                You define your class and an instance counter as static:

                private static MyClass myClass;
                private static in instCounter;

                In OnStartup() you let the first strategy instance do the job:

                if (instCounter++ == 0)
                {
                myClass = new MyClass(this);
                }

                In OnTermination() you let the last strategy instance turn out the light:

                if (--instCounter == 0)
                {
                myClass = null;
                }

                Regards
                Ralph

                Comment


                  #9

                  Thanks again, very appreciative of you taking the time to respond.

                  Works like a charm!

                  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