Situation
You want to know how to format text in the Output Window with things such as a tab, line break, or special Unicode characters.

Programming example

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

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

        protected override void StartCalc()
        {
            ExecInfo.MaxBarsBack = 10;

            Output.Clear();

            Output.WriteLine("Current bar \"volume\": {0}",
                Bars.Ticks[0]);

            Output.WriteLine("");
            
            Output.WriteLine("Filename: C:\\Temp\\myTextFile.txt.");

            Output.WriteLine("\r\nPrevious bar:\r\nOpen: {0}\r\nClose: {1}",
                Bars.Open[1],
                Bars.Close[1]);

            Output.WriteLine("\r\nCurrent bar price data:");
            Output.WriteLine("Open:\t{0}",  Bars.Open[0]);
            Output.WriteLine("High:\t{0}",  Bars.High[0]);
            Output.WriteLine("Low:\t{0}",   Bars.Low[0]);
            Output.WriteLine("Close:\t{0}", Bars.Close[0]);

            Output.WriteLine("\r\nSpecial Unicode characters:");
            Output.WriteLine("\u00BD \u2260 \u00BC");
            Output.WriteLine("\u2605, \u2602, \u266B");
        }
        
        protected override void CalcBar() { }
    }
}

Output of the programming example

The indicator generates the following text in the Output Window:

Current bar "volume": 129830

Filename: C:\Temp\myTextFile.txt.

Previous bar:
Open: 1,31514
Close: 1,31507

Current bar price data:
Open:     1,31507
High:     1,31516
Low:      1,31504
Close:  1,31513

Special Unicode characters:
½ ≠ ¼
★, ☂, ♫

The PowerLanguage .NET Editor Output Window

The Output Window in the PowerLanguage .NET Editor is a helpful tool for quickly printing information from an indicator or trading strategy. It can be accessed programmatically in any of the override methods (MultiCharts, 2013) and there are three methods available for doing so (PowerLanguage .NET Help, n.d.):

  • Output.Clear() erases all content of the Output Window;
  • Output.Write() and Output.WriteLine() both write text to the Output Window, but the latter also adds a line break.
Example of the output window in MultiCharts .NET

To enable the Output Window, go to ‘View’ in the menu bar.

To learn more about the Output Window, see outputting data to the Output Window, ‘debugging’ with the Output Window, and the Output Window and substitution parameters.

To influence how the text in the Output Window is formatted, we can use escape characters.

Escape characters in C#

A literal string (fixed text between double quotes) in C# can contain escape characters (also called escape sequences), which are characters with a special meaning (Liberty & MacDonald, 2009) and indicate that these characters should not be expressed or interpreted literally (Albahari & Albahari, 2012).

Escape characters begin with a backslash (\) followed by the character(s) with a special purpose (Albahari & Albahari, 2012), like \t for tab and \n for newline (Dorman, 2010).

Using escape characters in the Output Window

The table below lists the C# escape characters (Albahari & Albahari, 2012; Dorman, 2010; Stellman & Greene, 2010) that work in the PowerLanguage .NET Editor Output Window:

Escape character Meaning
\" Inserts a double quote.
\\ Inserts one backslash character.
\r\n Carriage return and new line: combined they insert a Windows line break.
\t Inserts a tab.
\u (or \x) Specifies an Unicode character with its four-digit hexadecimal code.

An example of each of these is given in the programming example.

MultiCharts .NET programming example

The example begins with a MultiCharts .NET class attribute:

[SameAsSymbol(true)]

By setting SameAsSymbol to true, the indicator will not make its own sub-chart.

Setting MaxBarsBack and clearing the Output Window

The StartCalc() override method, executed at the start of every calculation cycle (MultiCharts, 2013), contains all statements for printing to the Output Window. It begins as follows:

ExecInfo.MaxBarsBack = 10;

Output.Clear();

Here the indicator’s MaxBarsBack are set programmatically with the ExecInfo.MaxBarsBack property. This prevents a recalculation due to MaxBarsBack set to auto-detect.

For background information on MaxBarsBack, see what are ‘MaxBarsBack’ and how do they work?.

With the Output.Clear() method the Output Window is emptied to keep it orderly.

Using double quotes in the Output Window

The first escape character are double quotes:

Output.WriteLine("Current bar \"volume\": {0}",
    Bars.Ticks[0]);

Output.WriteLine("");

For more on substitution parameters ({0}, {1}, and so on), see composite formatting in the Output Window.

The current bar’s volume is inserted here in a string that contains the \" escape character. This escape character inserts a double quote (") instead of ending the string with the quote mark.

Then the Output.WriteLine() method is called (line 22). Because the \r\n escape character is automatically added by Output.WriteLine(), calling this method with a string that contains nothing generates a blank line in the Output Window. This keeps the output separate from the other Output.WriteLine() statements.

Lines 19 to 22 print the following to the Output Window:

Current bar "volume": 129830

Inserting the backslash character in the Output Window

Next a filename is outputted:

Output.WriteLine("Filename: C:\\Temp\\myTextFile.txt.");

This Output.WriteLine() statement leads to a single backslash being printed due to the \\ escape character:

Filename: C:\Temp\myTextFile.txt.

Adding line breaks in the PowerLanguage .NET Output Window

The next part add line breaks to the string:

Output.WriteLine("\r\nPrevious bar:\r\nOpen: {0}\r\nClose: {1}",
    Bars.Open[1],
    Bars.Close[1]);

The first line break escape character (\r\n) adds a blank line before the sentence begins to keep it isolated from the other output. The two other line breaks separate the previous bar historical price data:

Previous bar:
Open: 1,31514
Close: 1,31507

By the way, the Environment.NewLine property can also be used to insert a line break (Stellman & Greene, 2010). In MultiCharts .NET, the System namespace qualifier is needed to remove ambiguity with the Environment property from the PowerLanguage namespace (see PowerLanguage .NET Help, n.d.).

Formatting Output Window text with tab

The current bar price data it formatted as follows:

Output.WriteLine("\r\nCurrent bar price data:");
Output.WriteLine("Open:\t{0}",  Bars.Open[0]);
Output.WriteLine("High:\t{0}",  Bars.High[0]);
Output.WriteLine("Low:\t{0}",   Bars.Low[0]);
Output.WriteLine("Close:\t{0}", Bars.Close[0]);

The open (Bars.Open[0]), high (Bars.High[0]), low (Bars.Low[0]), and close (Bars.Close[0]) of the current price bar are nicely outlined with the tab escape character (\t):

Current bar price data:
Open:     1,31507
High:     1,31516
Low:      1,31504
Close:  1,31513

Getting creative with the Output Window: using Unicode characters

The last part uses the \u escape character to insert Unicode characters:

Output.WriteLine("\r\nSpecial Unicode characters:");
Output.WriteLine("\u00BD \u2260 \u00BC");
Output.WriteLine("\u2605, \u2602, \u266B");

Inserting an Unicode character is possible with the \u escape character and its four-digit hexadecimal code, which can be found in the Windows Character Map or online.

These statements generate the following text in the Output Window:

Special Unicode characters:
½ ≠ ¼
★, ☂, ♫

Key points

  • Escape characters (also called escape sequences) are preceded by a backslash (\). They are not processed literally but instead have a special meaning;
  • Fixed text between double quotes (a literal string) can include escape characters. Text with escape characters can be passed into the Output.Write() and Output.WriteLine() methods;
  • The \" escape character inserts a double quote (") in the Output Window, while \\ inserts one backslash character. \t inserts a tab, and \r\n is a Windows line break;
  • Unicode characters can be inserted with the \u or \x escape characters.
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

Stellman, A. & Greene, J. (2010). Head First C#: A Brain-Friendly Guide (2nd edition). Sebastopol, CA: O’Reilly Media.