Situation
Your indicator or strategy code seems to contain errors since it gives some odd results. But after going over your code again, you still cannot find the error. You decide to manually check the calculations and want to output your variables to the output tab of the PowerLanguage Editor.

Programming example

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

namespace PowerLanguage.Indicator
{
    public class OutputForDebuggingExample : IndicatorObject
    {
        private IPlotObject myPlot;
        private double barMidpoint = 0;

        public OutputForDebuggingExample(object _ctx) : base(_ctx) { }

        protected override void Create()
        {
            myPlot = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Yellow));
        }

        protected override void StartCalc()
        {
            // Clear the output tab of the PowerLanguage Editor
            Output.Clear();
        }

        protected override void CalcBar()
        {
            // Calculate on bar close
            if (Bars.Status == EBarState.Close)
            {
                // Calculate the midpoint of the bar
                barMidpoint = Bars.High[0] + Bars.Low[0] / 2;

                // Output data for some basic debugging
                Output.WriteLine("{0}  High: {1}, Low: {2}, barMidpoint: {3}",
                    Bars.TimeValue.ToString("dd-MM HH:mm:ss"),
                    Bars.High[0],
                    Bars.Low[0],
                    barMidpoint);

                myPlot.Set(barMidpoint);
            }
        }
    }
}

Outputting data to the MultiCharts .NET PowerLanguage .NET Editor

Two things in this example have to do with outputting data. First, in the StartCalc() method (lines 20-24) the Output.Clear() method is called. This method clears the PowerLanguage Editor output tab, and given its location (in the StartCalc() method) it clears the output tab every time the indicator recalculates. More information about these methods can be found in the MultiCharts .NET override methods article.

Second, starting at line 35 the following code is used to output the variables:

// Output data for some basic debugging
Output.WriteLine("{0}  High: {1}, Low: {2}, barMidpoint: {3}",
    Bars.TimeValue.ToString("dd-MM HH:mm:ss"),
    Bars.High[0],
    Bars.Low[0],
    barMidpoint);

The Bars.TimeValue property returns the current closing time of the bar (the equivalent of Bars.Time[0]). By formatting the DateTime string, we’re returning the date, month, hour, minutes, and seconds. By doing so, we can match the output with a certain price bar to help locate the erroneous calculations. See formatting DateTime strings for more about DateTime formatting.

The output will be something like the following:

03-10 06:45:00  High: 1,36081, Low: 1,36066, barMidpoint: 2,04114
03-10 06:50:00  High: 1,36068, Low: 1,36051, barMidpoint: 2,040935
03-10 06:55:00  High: 1,36062, Low: 1,36056, barMidpoint: 2,0409
03-10 07:00:00  High: 1,36073, Low: 1,36061, barMidpoint: 2,041035
03-10 07:05:00  High: 1,36072, Low: 1,36059, barMidpoint: 2,041015
03-10 07:10:00  High: 1,36063, Low: 1,36056, barMidpoint: 2,04091
03-10 07:15:00  High: 1,36062, Low: 1,36034, barMidpoint: 2,04079
03-10 07:20:00  High: 1,36037, Low: 1,36024, barMidpoint: 2,04049
03-10 07:25:00  High: 1,36067, Low: 1,36031, barMidpoint: 2,040825
03-10 07:30:00  High: 1,36062, Low: 1,36044, barMidpoint: 2,04084
03-10 07:35:00  High: 1,36071, Low: 1,36034, barMidpoint: 2,04088
03-10 07:40:00  High: 1,36068, Low: 1,36045, barMidpoint: 2,040905

Identifying errors with the PowerLanguage .NET Editor Output Window

The output shows that our bar midpoint calculation is wrong. This variable was calculated as follows:

// Calculate the midpoint of the bar
barMidpoint = Bars.High[0] + Bars.Low[0] / 2;

Given the order of operations in C#, Bars.Low[0] is first divided by 2 and then added to Bars.High[0]. Since this is not what we intended, we’ll need to change it to:

// Calculate the midpoint of the bar
barMidpoint = (Bars.High[0] + Bars.Low[0]) / 2;

Now the code runs fine and we can remove the Output.WriteLine() and Output.Clear() code segments.