Composite Formatting

Introduction

The composite formatting feature takes a composite format string and list of objects as input. A composite format string consists of fixed text intermixed with indexed placeholders, called format items, that correspond to the objects in the list. The formatting operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.

The composite formatting is supported by:

· String.Format, which returns a formatted result string. See the ELString Format method.

· StreamWriter.WriteLine, which outputs a formatted line. See the StreamWriter WriteLine method.

Composite Format String

A composite format string and object list are used as arguments of methods that support the composite formatting feature. A composite format string consists of zero or more runs of fixed text intermixed with one or more format items. The fixed text is any string that you choose, and each format item corresponds to an object in the list. The composite formatting feature returns a new result string where each format item is replaced by the string representation of the corresponding object in the list.

var:

string newstring("Fred");


Print(string.format("Name = {0}, hours = {1:hh}", newstring, elsystem.DateTime.Now));

The fixed text is "Name = " and ", hours = ". The format items are "{0}", whose index is 0, which corresponds to the object newstring, and "{1:hh}", whose index is 1, which corresponds to the object elsystem.DateTime.Now.

Format Item Syntax

Each format item takes the following form and consists of the following components:

{ index[,alignment][:formatString]}

The matching braces ("{" and "}") are required.

Index Component

The mandatory index component, also called a parameter specifier, is a number starting from 0 that identifies a corresponding item in the list of objects. That is, the format item whose parameter specifier is 0 formats the first object in the list, the format item whose parameter specifier is 1 formats the second object in the list, and so on.

Multiple format items can refer to the same element in the list of objects by specifying the same parameter specifier.

Each format item can refer to any object in the list. For example, if there are three objects, you can format the second, first, and third object by specifying a composite format string like this: "{1} {0} {2}". An object that is not referenced by a format item is ignored.

Alignment Component

The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.

Print(string.format("{0:MM/dd/yy HH:mm:ss} {1,-8} TODAY: {2:MM/dd/yy} V={3} YESTERDAY: {4:MM/dd/yy} V={5} ",DateTime.Now, Symbol, PSP.Time[0], PSP.Volume[0], PSP.Time[1], PSP.Volume[1]));

The -8 left justifies the symbol (index 1), padding it to 8 characters in length with blanks.

Note that alignment will give better results in the print log or text window when using a fixed width font, such as Courier,

Format String Component

The optional formatString component is a format string that is appropriate for the type of object being formatted. Specify a standard or custom numeric format string if the corresponding object is a numeric value, a standard or custom date and time format string if the corresponding object is a DateTime object, or an enumeration format string if the corresponding object is an enumeration value. If formatString is not specified, the general ("G") format specifier for a numeric, date and time, or enumeration type is used. The colon is required if formatString is specified.

Formatting Types

Different types of data values can use specialized format specifiers to determine how to format the data when converting it into a string. For more information, refer to the following:

Example

The following shows the use of composite formatting.  

Import Example

Composite formatting sample print output

  1. Click on the Import Example link to import the example into TradeStation.   
  2. Go to the TradeStation platform and create a Chart window.  Use the Insert - Indicator menu sequence and Add !ex_CompositeFormatting to the chart window. Select the View > EasyLanguage Print Log to see the text output.
  3. To review or modify the example code, go to the TS Development Environment and open indicator !ex_CompositeFormatting in the EasyLanguage Editor.