Situation
You want to know how to use the PowerLanguage .NET Editor Output Window programmatically, example by outputting strings or clearing the Output Window.

Programming example

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true)]
    public class Example_PLEditorOutput : IndicatorObject
    {
        public Example_PLEditorOutput(object _ctx) : base(_ctx) { }

        protected override void Create()
        {
            // Clear the Output tab every time the indicator is (re)created
            Output.Clear();
        }

        protected override void StartCalc()
        {
            // Once every calculation, output some symbol information
            Output.Write("Study name: " + Name);
            Output.Write(", applied to instrument: " + Bars.Info.Name + " (" + Bars.Info.Exchange + ")");
            Output.Write(System.Environment.NewLine);
        }

        protected override void CalcBar()
        {
            // Output bar information at the bar close
            if ((Bars.Status == EBarState.Close) && (Bars.CurrentBar < 20))
            {
                Output.WriteLine("{0} - Closed at {1}",
                    Bars.Time[0].ToString("d-M HH:mm:ss"),
                    Bars.Close[0]);
            }

            // Output chart information on a specific bar number
            if (Bars.CurrentBar == 50)
            {
                Output.Write("Chart resolution: ");
                Output.WriteLine(Bars.Info.Resolution.Size + " " + Bars.Info.Resolution.Type);
            }
        }
    }
}

Output of the programming example

The indicator generates the following text to the PowerLanguage .NET Editor Output Window:

Study name: Example_PLEditorOutput, applied to instrument: EUR/USD (FOREX)
16-1 00:30:00 - Closed at 1,36008
16-1 01:00:00 - Closed at 1,35991
16-1 01:30:00 - Closed at 1,35952
16-1 02:00:00 - Closed at 1,3597
16-1 02:30:00 - Closed at 1,36165
16-1 03:00:00 - Closed at 1,36177
16-1 03:30:00 - Closed at 1,36159
16-1 04:00:00 - Closed at 1,36193
16-1 04:30:00 - Closed at 1,36198
16-1 05:00:00 - Closed at 1,36189
16-1 05:30:00 - Closed at 1,36226
16-1 06:00:00 - Closed at 1,36219
16-1 06:30:00 - Closed at 1,36177
16-1 07:00:00 - Closed at 1,3619
16-1 07:30:00 - Closed at 1,36257
16-1 08:00:00 - Closed at 1,36208
16-1 08:30:00 - Closed at 1,3607
16-1 09:00:00 - Closed at 1,36087
16-1 09:30:00 - Closed at 1,36094
Chart resolution: 30 Minute

The PowerLanguage .NET Editor Output Window

The PowerLanguage .NET Editor Output Window (see image below) is helpful for keeping track of what an indicator or strategy does or for initial code debugging in the PowerLanguage .NET Editor.

Example of the Output Window in the PowerLanguage .NET Editor

The Output Window is located inside the main PowerLanguage .NET window, and is highlighted here with an orange rectangle. Your Output Window may be to the right or bottom.

The 'View' menu in the PowerLanguage .NET Editor

If you cannot locate the Output Window in the PowerLanguage .NET Editor, make sure that you have it set to enabled in the View options.

The Output Window can be accessed programmatically from an indicator or strategy during any calculation stage (MultiCharts, 2013). An example of that feature can be found in the current programming example or in the article about MultiCharts .NET’s override methods.

Programmatically using the PowerLanguage .NET Editor Output Window

The example starts by setting the SameAsSymbol class attribute to true (line 8) in order to plot the indicator on the main price chart.

Then we arrive at the Create() method:

protected override void Create()
{
    // Clear the Output tab every time the indicator is (re)created
    Output.Clear();
}

Calling the Output.Clear() method programmatically clears the Output Window. Since the Create() method is called once after the creation of a new study object (MultiCharts, 2013), each time this indicator is (re)created, the Output Window is cleared.

Writing to the Output Window

In the StartCalc() override method the following code is implemented:

protected override void StartCalc()
{
    // Once every calculation, output some symbol information
    Output.Write("Study name: " + Name);
    Output.Write(", applied to instrument: " + Bars.Info.Name + " (" + Bars.Info.Exchange + ")");
    Output.Write(System.Environment.NewLine);
}

By the way, the Environment.NewLine property is located in the System namespace to which we added a reference in line 1 (i.e. using System;). But since the PowerLanguage namespace also contains an Environment property, the System namespace needs to be made explicit to remove this ambiguity.

Here the Output.Write() method is called to write symbol information to the Output Window.

The difference between Output.Write() and Output.WriteLine() is that Output.Write() does not add a line ending symbol (MultiCharts, 2013). We therefore added a newline ourselves by calling the System.Environment.NewLine property in line 24 (see MSDN, n.d.).

Writing to the Output Window with a line ending symbol

In the first part of the ‘CalcBar()’ method, we evaluate if the bar has closed and if the bar number is less than 20 (to prevent cluttering the log):

// Output bar information at the bar close
if ((Bars.Status == EBarState.Close) && (Bars.CurrentBar < 20))
{
    Output.WriteLine("{0} - Closed at {1}",
        Bars.Time[0].ToString("d-M HH:mm:ss"),
        Bars.Close[0]);
}

When these expressions evaluate to true, the Output.WriteLine() method is called and information is written to the Output Window through the use of string substitution parameters and formatting DateTime to a string.

In the second part of the CalcBar() method some chart information is outputted on an arbitrarily chosen bar number:

// Output chart information on a specific bar number
if (Bars.CurrentBar == 50)
{
    Output.Write("Chart resolution: ");
    Output.WriteLine(Bars.Info.Resolution.Size + " " + Bars.Info.Resolution.Type);
}

Here the Output.Write() method is combined with the Output.WriteLine() method to generate one line of output. This is slightly more convenient than calling the NewLine property, as we did in line 24.

Key points:

  • Call the Output.Clear() method to clear the Output Window;
  • The Output.Write() method writes text to the Output Window, while the Output.WriteLine() method also adds a line ending symbol;
  • With the Output.Write() method you need to provide the line ending symbol (System.Environment.NewLine) yourself.
References

Microsoft Developer Network [MSDN] (n.d.). Environment.NewLine property (System). Retrieved on February 11, 2014, from http://msdn.microsoft.com/en-us/library/system.environment.newline%28v=vs.110%29.aspx

MultiCharts (April 2013). MultiCharts .NET Programming Guide (version 1.0). Retrieved from http://www.multicharts.com/downloads/MultiCharts.NET-ProgrammingGuide-v1.0.pdf