Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  576 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  334 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  101 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  553 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  551 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X