Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Printing to output window using class method

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

    Printing to output window using class method

    I'm trying to print to the output window by referencing a class method. I'm new to OOP programming so please forgive my ignorance. I created a class with a method that is meant to display to output but attempts at using Print() are met with error:

    "Cannot access a non-static member of outer type 'NinaTrader.Indicator.IndicatorBase' via nested type 'NinjaTrader.Indicator.csharplearning.Rectangle"

    Code is:

    Code:
        public class csharplearning : Indicator
        {
            #region Variables
            
            Rectangle r = new Rectangle();
            
            #endregion
    
            protected override void Initialize()
            {
                r.Acceptdetails();
                r.Display();
            }
    
    
            protected override void OnBarUpdate()
            {
    
                
            }
            
            class Rectangle
            {
                double length;
                double width;
                
                public void Acceptdetails()
                {
                    length = 4.5;
                    width = 3.5;
                }
                public double GetArea()
                {
                    return length * width;
                }
                public void Display()
                {
                    // produces error message - Print function appears to be invalid here
                    Print("test"); 
                }
            }
            
    
    
            #region Properties
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Plot0
            {
                get { return Values[0]; }
            }
    
    //        [Description("")]
    //        [GridCategory("Parameters")]
    //        public int MyInput0
    //        {
    //            get { return myInput0; }
    //            set { myInput0 = Math.Max(1, value); }
    //        }
            #endregion
        }

    #2
    Hello,

    Thank you for the question.

    The error you are getting is due to the Print() statement being outside of the indicators class scope.

    First I would like to check what you are trying to do in the example to better assist with an answer, there are a number of ways to Print outside of a indicator but quite possibly there is no need in the case what you are trying can be accomplished in a more simple way.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      This code is just some experiments leading up to a much larger project I'm working on implementing a neural net. The reason I want to be able print from a class method is that I want to be able to trouble shoot the implementation with print statements sprinkled within the class methods. This rectangle class is meaningless for my project. This was only a test to see if I could create my own print "method" within a class. Apparently I cannot do this?

      I know how to use the Print() function I'm just trying to get it to work in the scope of a custom class/method. So it's not possible?

      Comment


        #4
        Hello,

        If the class was only a test, I would suggest looking at the UserDefinedMethods scripts that are included in the platform.

        A Partial class would allow you to print because it is a Part of the indicator or strategy base where a standard Class would not because it is separate.

        You could use the same structure, methods etc inside of a Partial class instead to allow prints to be used.


        Code:
        partial class Indicator
        {
        	public void MyFavoriteMethod(string s)
        	{
        		Print(s);	
        	}
        }
        In your Indicator, this would just be called as : MyFavoriteMethod("Something to print");

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Brilliant! I see the value of this. I can put this custom print method in the user defined file and access it from my main indicator script. I'll give this a whirl and let you know if I have trouble.
          thx!

          Comment


            #6
            Ok I got the custom method thing to work however is there a way to define custom classes outside of the main indicator script?

            Comment


              #7
              Hello,

              Standard C# scripting would apply so you can define a class where you want to. You could create a Indicator for example, clear all the text from the file and make a namespace and class if you wanted. Regarding standard classes, only methods like Print or inherited items would not be available in those classes. instead you can Return values to complete the logic.

              If you have logic that can be pushed into a class that is not indicator specific such as a math equation, you could certainly do that. For items that are indicator or strategy specific such as Print or checking the Position, you would need to use a Partial class.


              You can do something like the following as an example of this:

              Code:
              partial class Indicator
              {
              	public void MyFavoriteMethod(int someValue)
              	{
              		MyTestClass myClass = new MyTestClass();
              		double result = myClass.DoSomeMath(someValue);
              		Print(result);	
              	}
              }
              
              public class MyTestClass
              {
              	public double DoSomeMath(int val)
              	{
              		return val * 2;
              	}
              }
              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Print Method in Custom Class

                What is the (unsupported, of course) syntax for using Print within a custom class? Similarly, for Log.

                Thanks.

                Comment


                  #9
                  Hello jeronymite,

                  For NT7 I am unaware of any undocumented print methods, you would likely need to pass in the instance of the indicator or script that calls the class. Going forward in NT8 a static method was added which can be used mostly anywhere.

                  Building off the prior posts sample, you could so something like the following:

                  Code:
                  partial class Indicator
                  {
                  	public void MyFavoriteMethod(int someValue)
                  	{
                  		MyTestClass myClass = new MyTestClass(this);
                  		double result = myClass.DoSomeMath(someValue);
                  		Print(result);	
                  	}
                  }
                  
                  public class MyTestClass
                  {
                  	private Indicator myIndi;
                  	public MyTestClass(Indicator i)
                  	{
                  		myIndi = i;
                  		myIndi.Print("Test");
                  	}
                  	public double DoSomeMath(int val)
                  	{
                  		myIndi.Print(val);
                  		return val * 2;
                  	}
                  }
                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by soon123, Today, 02:14 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post soon123
                  by soon123
                   
                  Started by guilhem, Today, 01:53 AM
                  1 response
                  6 views
                  0 likes
                  Last Post sgkcfc
                  by sgkcfc
                   
                  Started by elkingonzalez, 11-24-2020, 09:59 AM
                  4 responses
                  40 views
                  0 likes
                  Last Post MrSmart
                  by MrSmart
                   
                  Started by Photon66, Today, 12:03 AM
                  0 responses
                  9 views
                  0 likes
                  Last Post Photon66  
                  Started by morrnel, Yesterday, 09:38 PM
                  1 response
                  10 views
                  0 likes
                  Last Post NinjaTrader_Marco  
                  Working...
                  X