Print()

View as Markdown

Definition

Converts object data to a string format and appends the specified value as text to the NinjaScript Output window. Printing data to the NinjaScript Output window is a useful debugging technique to verify values while developing your custom NinjaScript object.

The Print() method only targets the Output tab recently specified by set PrintTo property.

Method Return Value

This method does not return a value.

Syntax

Print(object value)

Warning: High frequency of Print() method calls can represent a performance hit on your PC. Please see the NinjaScript section of the Performance Tips article for more information.

Parameters

ParameterDescription
valueThe object to print to the output window

Tips:

  1. You can format prices aligned for easier debugging by using the ToString() method. E.g., Low[0].ToString(“0.00”) forces the format from 12.5 to 12.50. Low[0].ToString(“0.000”) forces 12.500.
  2. You can format one or more objects in a specified string with the text equivalent of a corresponding object’s value for better maintainability using the .NET string.Format() method. Please see the examples below.

Examples

Passing objects directly to Print() method

1protected override void OnBarUpdate()
2{
3 // Generates a message
4 Print("This is a message");
5 //Output: This is a message
6 Print("The high of the current bar is : " + High[0]);
7 //Output: The high of the current bar is : 2112.75
8 // Prints the current bar SMA value to the output window
9 Print(SMA(Close, 20)[0]);
10 //Output: 2110.5;
11}

Passing string.Format() directly to Print() method

1protected override void OnBarUpdate()
2{
3 //Format and Print each bar value to the output window
4 Print(string.Format("{0};{1};{2};{3};{4};{5}", Time[0], Open[0], High[0], Low[0], Close[0], Volume[0]));
5 //Output: 2/24/2015 11:01:00 AM;2110.5;2110.5;2109.75;2110;1702
6}

Storing and reusing variables in Print() method

1protected override void OnBarUpdate()
2{
3 //store the Close[0] value in a variable which can be printed later
4 double myValue = Close[0];
5 //create and store a custom error message
6 string myError = string.Format("Error on Bar {0}, value {1} was not expected", CurrentBar, myValue);
7 //our first test case, if true print our error
8 if(myValue > High[0])
9 Print(myError);
10 //Output: Error on Bar 233, value 1588.25 was not expected
11 //reassign myValue
12 myValue = Low[0];
13 //our second test case (now uses Low[0]), if true print our error
14 if(myValue > Close[0])
15 Print(myError);
16 //Output: Error on Bar 57, value 1585.5 was not expected
17}