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

Printing to output window using class method

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

  • NinjaTrader_Jesse
    replied
    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([B]this[/B]);
    		double result = myClass.DoSomeMath(someValue);
    		Print(result);	
    	}
    }
    
    public class MyTestClass
    {
    	[B]private Indicator myIndi;[/B]
    	public MyTestClass([B]Indicator i[/B])
    	{
    		[B]myIndi = i;[/B]
    		myIndi.Print("Test");
    	}
    	public double DoSomeMath(int val)
    	{
    		myIndi.Print(val);
    		return val * 2;
    	}
    }
    I look forward to being of further assistance.

    Leave a comment:


  • jeronymite
    replied
    Print Method in Custom Class

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

    Thanks.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    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.

    Leave a comment:


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

    Leave a comment:


  • ShruggedAtlas
    replied
    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!

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    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.

    Leave a comment:


  • ShruggedAtlas
    replied
    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?

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    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.

    Leave a comment:


  • ShruggedAtlas
    started a topic Printing to output window using class method

    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
        }

Latest Posts

Collapse

Topics Statistics Last Post
Started by ScottWalsh, Today, 06:52 PM
4 responses
32 views
0 likes
Last Post ScottWalsh  
Started by olisav57, Today, 07:39 PM
0 responses
4 views
0 likes
Last Post olisav57  
Started by trilliantrader, Today, 03:01 PM
2 responses
19 views
0 likes
Last Post helpwanted  
Started by cre8able, Today, 07:24 PM
0 responses
6 views
0 likes
Last Post cre8able  
Started by Haiasi, Today, 06:53 PM
1 response
5 views
0 likes
Last Post NinjaTrader_Manfred  
Working...
X