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 inside a custom class

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

    Print inside a custom class

    I have a custom class in NT8 that I use in multiple indicators.

    Console.write compiles but does not write to an accessible screen in NT8

    I understand from hints in other threads that I can somehow use Print in my custom class to print to Output 1.

    Here is an outline of my code where "TestIndic" is an indicator using my custom class and "TestCommon" is my common custom class.



    Code:
    namespace NinjaTrader.NinjaScript.Indicators.MyCommon
    {
       public class TestCommon
       {
          int Param1;
          double Param2;
          double myMagic = 1;
          public TestCommon (int param1, double param2)
          {
             Param1 = param1;
             Param2 = param2;
          }
       }
    
       public double DoGreatStuff ( ISeries<double> price )
       {
          Print ( String.Format( " The Magic : ",myMagic ) ); // <---- ??? Print is out of scope in TestCommon -- How do I use Print in my custom Class ???
          return price[0] * myMagic;
       }
    }
    
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
       public class TestIndic : Indicator
       {
          public int MyParam1 = 1;
          public double myParam2 = 2.0;
          public double myResult;
          public TestCommon myTest;
    
          protected override void OnStateChange()
          {
             if (State == State.DataLoaded)
             {
                myTest = new TestCommon ( MyParam1, MyParam2 );
             }
          }
    
    
          protected override void OnBarUpdate()
          {
             myResult = myTest.DoGreatStuff ( Close );
          }
       }
    }

    Thank you in advance for your help
    Last edited by JeffCO7; 01-17-2022, 10:32 AM.

    #2
    Hello JeffCO7,

    Thank you for your post.

    In most cases Print() can be used but if you're running into issues where you're outside the NinjaScript scope, instead of Print(), use Output.Process() to write a message and instead of ClearOutputWindow(), use Output.Reset() to clear the output window.

    Code:
    // Instead of Print()
    NinjaTrader.Code.Output.Process("my message", PrintTo.OutputTab1);
    
    // Instead of ClearOutputWindow()
    NinjaTrader.Code.Output.Reset()om/support/helpGuides/nt8/alert_and_debug_concepts.htm
    That being said, it looks like you should be in the NinjaScript scope and Print should work for you, but it looks like you don't have String.Format set up correctly:

    Print(String.Format( " The Magic: {0} ",myMagic ));

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      NinjaTrader_Kate,
      Thank you. I just hand jammed the sample code here to describe my situation and missed the {0} in the String.Format as I did not compile it.

      I tried
      Code:
          NinjaTrader.Code.Output.Process( "my message", PrintTo.OutputTab1 );
      It compiled and it DID WORK. Woohoo!! ( I originally missed the PrintTo.OutputTab1 and it would not compile with one argument - sorry for the false alarm.)


      Question: When I try to use Print, it has a NinjaScript compile error of "The name 'Print' does not exist in the current context"

      Any thoughts since you think it should be in context? Am I missing a using statement?

      Jeff
      Last edited by JeffCO7; 01-17-2022, 11:18 AM.

      Comment


        #4
        Hello JeffCO7,

        Thank you for your reply.

        I got some clarification on that, and Print() is available to indicators and strategies. NinjaTrader.Code.Output.Process() would have to be used in a custom class that doesn't inherit from indicator or strategy, but if you supplied the indicator to the custom class, the print could be used through the indicator

        myIndy.Print("some text");

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          @Ninjatrader_Kate,

          Thank you. I think the NinjaTrader.Code.Output.Process is the solution for my current context. I have been struggling with workarounds for some time.

          This was a great solution.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by quantismo, Yesterday, 05:13 PM
          1 response
          13 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by The_Sec, 04-16-2024, 02:29 PM
          3 responses
          16 views
          0 likes
          Last Post NinjaTrader_ChristopherS  
          Started by hurleydood, 09-12-2019, 10:45 AM
          15 responses
          1,098 views
          0 likes
          Last Post Leeroy_Jenkins  
          Started by danieldunn2024, Today, 03:06 AM
          1 response
          5 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by cre8able, Yesterday, 04:16 PM
          1 response
          16 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X