Situation
You want to combine text with other values (such as variables) and output these to the Output Window conveniently.

Programming example

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

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true)]
    public class Example_OutputWindowCompositeFormatting : IndicatorObject
    {
        public Example_OutputWindowCompositeFormatting(object _ctx) : base(_ctx) { }
        
        protected override void StartCalc()
        {
            Output.Clear();
        }

        protected override void CalcBar()
        {
            if (Environment.IsRealTimeCalc)
            {
                Output.Write("\r\nBar open: {0}, high: {1}, and low: {2}. ",
                    Bars.Open[0],
                    Bars.High[0],
                    Bars.Low[0]);

                Output.WriteLine("Current tick: {0}", Bars.Close[0]);

                Output.WriteLine("Bar date {0} and time {1}. Tick time: {2}",
                    Bars.Time[0].ToString("d-M"),
                    Bars.Time[0].ToString("HH:mm:ss"),
                    Bars.StatusLine.Time.ToString("HH:mm:ss"));
            }
        }
    }
}

Output of the programming example

The example generates the following text in the Output Window:

Bar open: 1,29372, high: 1,29372, and low: 1,29352. Current tick: 1,29364
Bar date 5-9 and time 08:15:00. Tick time: 08:07:47

Bar open: 1,29372, high: 1,29372, and low: 1,29352. Current tick: 1,29365
Bar date 5-9 and time 08:15:00. Tick time: 08:07:47

Bar open: 1,29372, high: 1,29372, and low: 1,29352. Current tick: 1,29364
Bar date 5-9 and time 08:15:00. Tick time: 08:07:53

Bar open: 1,29372, high: 1,29372, and low: 1,29352. Current tick: 1,29365
Bar date 5-9 and time 08:15:00. Tick time: 08:07:59

Bar open: 1,29372, high: 1,29372, and low: 1,29352. Current tick: 1,29366
Bar date 5-9 and time 08:15:00. Tick time: 08:08:14

Bar open: 1,29372, high: 1,29372, and low: 1,29352. Current tick: 1,29367
Bar date 5-9 and time 08:15:00. Tick time: 08:08:17

Bar open: 1,29372, high: 1,29372, and low: 1,29352. Current tick: 1,29368
Bar date 5-9 and time 08:15:00. Tick time: 08:08:17

The PowerLanguage .NET Editor Output Window

The Output Window is the PowerLanguage .NET Editor’s tool for quickly printing information when developing an indicator or trading strategy. With logging the steps of a script potential errors can be located and the logic verified.

Example of the output window in MultiCharts .NET

For more on the Output Window, see outputting data to the Output Window and ‘debugging’ with the Output Window.

The Output Window methods can be accessed in any override method (MultiCharts, 2013) with the following methods (PowerLanguage .NET Help, n.d.):

  • Output.Clear() clears the complete Output Window on every call;
  • Output.Write() writes text to the Output Window as does Output.WriteLine(), but this latter adds a newline at the end.

Both Output.Write() and Output.WriteLine() can use C#’s composite formatting.

Composite formatting of strings in C#

A composite format string is a string that defines a template of fixed text that includes substitution parameters (Dorman, 2010; Liberty & MacDonald, 2009). Substitution parameters are numeric values between braces ({0}, {1}, {2}, up to {n}) that correspond to the objects in the list following the composite format string (Liberty & MacDonald, 2009; Sempf, Sphar, & Davis, 2010).

For example:

Output.WriteLine("Volume of the bar: {0}", Bars.Ticks[0]);

The "Volume of the bar: {0}" string has one substitution parameter: {0}. This zero acts as a placeholder for the first object in the list following the string (a {1} would refer to the second, and so on). Because of this, the volume data returned by Bars.Ticks[0] is placed (i.e., substituted) at the location of the {0} substitution parameter in the string.

By the way, the static String.Format() method, which returns a formatted string, also allows composite formatting (Albahari & Albahari, 2012).

PS: What Liberty and MacDonald (2009) term substitution parameters is called format items by Dorman (2010) and placeholders by Sempf et al. (2010). This article uses the term substitution parameters, but note that all refer to the same thing.

Composite formatting has the following noteworthy features (Dorman, 2010):

  • More substitution parameters than there are objects in the list is not allowed;
  • Multiple substitution parameters can use the same zero-based index;
  • Substitution parameters can refer to any object in the list, in any order.

In the programming example substitution parameters are used to output price bar data to the PowerLanguage .NET Editor Output Window.

MultiCharts .NET programming example

The example begins with setting SameAsSymbol:

[SameAsSymbol(true)]

Setting this class attribute to true plots the indicator on the main price chart instead of creating a separate sub-chart.

Clearing the PowerLanguage .NET Editor Output Window

Next is the StartCalc() method:

protected override void StartCalc()
{
    Output.Clear();
}

The StartCalc() method is executed once at the start of every calculation (MultiCharts, 2013). Calling Output.Clear() here ensures that the Output Window does not gets cluttered.

Outputting data to the Outputting Window with composite formatting

The last part is the CalcBar() override method:

protected override void CalcBar()
{
    if (Environment.IsRealTimeCalc)
    {
        Output.Write("\r\nBar open: {0}, high: {1}, and low: {2}. ",
            Bars.Open[0],
            Bars.High[0],
            Bars.Low[0]);

        Output.WriteLine("Current tick: {0}", Bars.Close[0]);

        Output.WriteLine("Bar date {0} and time {1}. Tick time: {2}",
            Bars.Time[0].ToString("d-M"),
            Bars.Time[0].ToString("HH:mm:ss"),
            Bars.StatusLine.Time.ToString("HH:mm:ss"));
    }
}

The if statement verifies whether the calculation is performed on real-time data, in which case the Environment.IsRealTimeCalc property returns true (MultiCharts, 2013).

The body of the if statement consists out of three parts. First, the Output.Write() method is called (lines 22-25). Its string argument starts with the \r\n escape character (which inserts a newline) and contains three substitution parameters with price data of the current bar: {0} for the opening price (Bars.Open[0]), {1} for the high (Bars.High[0]), and {2} is the placeholder for the low (Bars.Low[0]).

The first Output.WriteLine() method (line 27) prints the price of the current tick (returned by Bars.Close[0] since the >indicator is calculated in real-time). Because Output.Write() has no line-ending character, the Output.WriteLine() string is printed at the same line as Output.Write().

The last statement calls Output.WriteLine() with three substitution parameters: the time of the current bar (Bars.Time[0]) twice with different DateTime format specifiers, and the time of the last price update (Bars.StatusLine.Time) formatted to a string with hours, minutes, and seconds (ToString("HH:mm:ss")).

Key points

  • Composite formatting in C# allows for combining fixed text with placeholders (the substitution parameters);
  • Substitution parameters allow combining a fixed string with values that can change at script runtime;
  • Outputting text to the Output Window is done with the Output.Write() and Output.WriteLine() methods. Both allow composite formatting.
References

Albahari, J. & Albahari, B. (2012). C# 5.0 in a Nutshell: The Definitive Reference (5th edition). Sebastopol, CA: O’Reilly Media.

Dorman, S. (2010). Sams Teach Yourself Visual C# 2010 in 24 Hours. Indianapolis, IN (USA): Sams/Pearson Education.

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.

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

PowerLanguage .NET Help (n.d.). Retrieved on May 16, 2014, from http://www.multicharts.com/downloads/PowerLanguage.NET.chm

Sempf, B., Sphar, C., & Davis, S.R. (2010). C# 2010 All-In-One for Dummies. Hoboken, NJ: John Wiley & Sons.