Tyche Trading

Write Methods

Bar Info

Debugging

Derivatives

Matrix Handling

Price Rounding

Regression Lines

Statistics

User Tracking

Example Strategies

Write Methods

Description:

These methods will print full lists, 1d/2d arrays, jagged 1d arrays, nested 1d arrays, and nested lists to the NinjaTrader Output Window.

 

Syntax:

Write(int[] arr);
Write(double[] arr);
Write(List<int> list);
Write(List<double> list);
Write(int[,] arr);
Write(double[,] arr);
Write(int[][] jagArr);
Write(double[][] jagArr);
Write(List<int[]> nestArr);
Write(List<double[]> nestArr);
Write(List<List<int>> nestList);
Write(List<List<double>> nestList);

 

Syntax 2:

These methods can be used to print lists that can easily be copy and pasted into Excel or Google Sheets if both boolean arguments are true.

Write(List<int> list, bool vertical, bool comma);
Write(List<double> list, bool vertical, bool comma);

 

Arguments:

arr: A d1 or 2d array of ints or doubles.

list: A List of ints or doubles.

jagArr: A jagged array of 1d arrays or ints or doubles.

nestArr: A list of 1d arrays.

nestList: A list of lists.

vertical: A boolean that if true will print the provided list vertically rather than horizontally.

comma: A boolean that if true will print the provided list without commas.

 

Notes:

Neither C# or NinjaScript offer methods that will print the content of lists or arrays directly to the output window/console.

When we are running through the debugging proccess, being able to easily see the values within our lists and arrays in important.

So we made these methods to be able to do just that.

We also included a method for lists to be printed in a format that can easily be copy and pasted into Excel or Google Sheets.

				
					/// <summary>
/// Print entire provided array in NinjaScript Output Widow.
/// </summary>
/// <remarks> If array is null the method will print null. </remarks>
/// <param name="arr"> 1d array of ints </param>
private void Write(int[] arr)
{
	if (arr != null)
	{
		int n = arr.Length;
		if (n > 0)
		{
			string str = "[ ";
			for (int i = 0; i < n - 1; i++)
				str += String.Format("{0}, ", arr[i]);
			str += String.Format("{0} ]", arr[n - 1]);
			Print(str);
		}
		else
			Print("[ ]");
	}
	else
		Print("[ null ]");
}

/// <summary>
/// Print entire provided array in NinjaScript Output Widow.
/// </summary>
/// <remarks> If array is null the method will print null. </remarks>
/// <param name="arr"> 1d array of doubles </param>
private void Write(double[] arr)
{
	if (arr != null)
	{
		int n = arr.Length;
		if (n > 0)
		{
			string str = "[ ";
			for (int i = 0; i < n - 1; i++)
				str += String.Format("{0}, ", arr[i]);
			str += String.Format("{0} ]", arr[n - 1]);
			Print(str);
		}
		else
			Print("[ ]");
	}
	else
		Print("[ null ]");
}

/// <summary>
/// Print entire provided array in NinjaScript Output Widow.
/// </summary>
/// <remarks> If array is null the method will print null. </remarks>
/// <param name="arr"> 2d array of ints </param>
private void Write(int[,] arr)
{
	if (arr != null)
	{
		int row = arr.GetLength(0);
		int col = arr.GetLength(1);
		string str;
		if (row == 0 && col == 0)
			Print("[ ]");
		else
		{
			for (int i = 0; i < row - 1; i++)
			{
				if (i != 0)
					str = "  ";
				else
					str = "[ ";
				if (col == 0)
					str += "";
				else
				{
					for (int j = 0; j < col - 1; j++)
						str += String.Format("{0}, ", arr[i, j]);
					str += String.Format("{0}", arr[i, col - 1]);
				}
				Print(str);
			}
			if (row == 1)
				str = "[ ";
			else
				str = "  ";
			if (col == 0)
				str += " ]";
			else
			{
				for (int j = 0; j < col - 1; j++)
					str += String.Format("{0}, ", arr[row - 1, j]);
				str += String.Format("{0} ]", arr[row - 1, col - 1]);
			}
			Print(str);
		}
	}
	else
		Print("[ null ]");
}

/// <summary>
/// Print entire provided array in NinjaScript Output Widow.
/// </summary>
/// <remarks> If array is null the method will print null. </remarks>
/// <param name="arr"> 2d array of doubles </param>
private void Write(double[,] arr)
{
	if (arr != null)
	{
		int row = arr.GetLength(0);
		int col = arr.GetLength(1);
		string str;
		if (row == 0 && col == 0)
			Print("[ ]");
		else
		{
			for (int i = 0; i < row - 1; i++)
			{
				if (i != 0)
					str = "  ";
				else
					str = "[ ";
				if (col == 0)
					str += "";
				else
				{
					for (int j = 0; j < col - 1; j++)
						str += String.Format("{0}, ", arr[i, j]);
					str += String.Format("{0}", arr[i, col - 1]);
				}
				Print(str);
			}
			if (row == 1)
				str = "[ ";
			else
				str = "  ";
			if (col == 0)
				str += " ]";
			else
			{
				for (int j = 0; j < col - 1; j++)
					str += String.Format("{0}, ", arr[row - 1, j]);
				str += String.Format("{0} ]", arr[row - 1, col - 1]);
			}
			Print(str);
		}
	}
	else
		Print("[ null ]");
}

/// <summary>
/// Print entire provided list in NinjaScript Output Widow.
/// </summary>
/// <remarks> If list is null the method will print null. </remarks>
/// <param name="list"> List of ints. </param>
private void Write(List<int> list)
{
	if (list != null)
	{
		if (list.Count > 0)
		{
			string str = "( ";
			int n = list.Count;
			for (int i = 0; i < n - 1; i++)
			{
				str += String.Format("{0}, ", list[i]);
			}
			str += String.Format("{0} )", list[n - 1]);
			Print(str);
		}
		else
			Print("( )");
	}
	else
		Print("( null )");
}

/// <summary>
/// Print entire provided list in NinjaScript Output Widow.
/// </summary>
/// <remarks> If list is null the method will print null. </remarks>
/// <param name="list"> List of doubles. </param>
private void Write(List<double> list)
{
	if (list != null)
	{
		if (list.Count > 0)
		{
			string str = "( ";
			int n = list.Count;
			for (int i = 0; i < n - 1; i++)
			{
				str += String.Format("{0}, ", list[i]);
			}
			str += String.Format("{0} )", list[n - 1]);
			Print(str);
		}
		else
			Print("( )");
	}
	else
		Print("( null )");
}

/// <summary>
/// Print entire provided list in NinjaScript Output Widow vertically.
/// </summary>
/// <remarks> If list is null the method will print null. </remarks>
/// <param name="list"> List of doubles. </param>
/// <param name="vertical"> Sets the list to print vertically. </param>
/// <param name="comma"> Option to keep or remove the comma. </param>
private void Write(List<double> list, bool vertical, bool comma)
{
	if (list != null)
	{
		if (vertical == true)
		{
			if (list.Count > 0)
			{
				if (list.Count > 1)
				{
					if (comma == true)
					{
						string str = "( ";
						str += String.Format("{0},", list[0]);
						Print(str);
						int n = list.Count;
						for (int i = 1; i < n - 1; i++)
						{
							str = String.Format("{0},", list[i]);
							Print(str);
						}
						str = String.Format("{0} )", list[n - 1]);
						Print(str);
					}
					else
                    {
						string str = "( ";
						str += String.Format("{0}", list[0]);
						Print(str);
						int n = list.Count;
						for (int i = 1; i < n - 1; i++)
						{
							str = String.Format("{0}", list[i]);
							Print(str);
						}
						str = String.Format("{0} )", list[n - 1]);
						Print(str);
					}
				}
				else
					Print(String.Format("( {0} )", list[0]));
			}
			else
				Print("( )");
		}
		else
        {
			if (list.Count > 0)
			{
				if (comma == true)
				{
					string str = "( ";
					int n = list.Count;
					for (int i = 0; i < n - 1; i++)
					{
						str += String.Format("{0}, ", list[i]);
					}
					str += String.Format("{0} )", list[n - 1]);
					Print(str);
				}
				else
                {
					string str = "( ";
					int n = list.Count;
					for (int i = 0; i < n - 1; i++)
					{
						str += String.Format("{0} ", list[i]);
					}
					str += String.Format("{0} )", list[n - 1]);
					Print(str);
				}
			}
			else
				Print("( )");
		}
	}
	else
		Print("( null )");
}

/// <summary>
/// Print entire provided list in NinjaScript Output Widow vertically.
/// </summary>
/// <remarks> If list is null the method will print null. </remarks>
/// <param name="list"> List of ints. </param>
/// <param name="vertical"> Sets the list to print vertically. </param>
/// <param name="comma"> Option to keep or remove the comma. </param>
private void Write(List<int> list, bool vertical, bool comma)
{
	if (list != null)
	{
		if (vertical == true)
		{
			if (list.Count > 0)
			{
				if (list.Count > 1)
				{
					if (comma == true)
					{
						string str = "( ";
						str += String.Format("{0},", list[0]);
						Print(str);
						int n = list.Count;
						for (int i = 1; i < n - 1; i++)
						{
							str = String.Format("{0},", list[i]);
							Print(str);
						}
						str = String.Format("{0} )", list[n - 1]);
						Print(str);
					}
					else
					{
						string str = "( ";
						str += String.Format("{0}", list[0]);
						Print(str);
						int n = list.Count;
						for (int i = 1; i < n - 1; i++)
						{
							str = String.Format("{0}", list[i]);
							Print(str);
						}
						str = String.Format("{0} )", list[n - 1]);
						Print(str);
					}
				}
				else
					Print(String.Format("( {0} )", list[0]));
			}
			else
				Print("( )");
		}
		else
		{
			if (list.Count > 0)
			{
				if (comma == true)
				{
					string str = "( ";
					int n = list.Count;
					for (int i = 0; i < n - 1; i++)
					{
						str += String.Format("{0}, ", list[i]);
					}
					str += String.Format("{0} )", list[n - 1]);
					Print(str);
				}
				else
				{
					string str = "( ";
					int n = list.Count;
					for (int i = 0; i < n - 1; i++)
					{
						str += String.Format("{0} ", list[i]);
					}
					str += String.Format("{0} )", list[n - 1]);
					Print(str);
				}
			}
			else
				Print("( )");
		}
	}
	else
		Print("( null )");
}

/// <summary>
/// Print entire provided jagged array in NinjaScript Output Widow.
/// </summary>
/// <remarks> If jagged array is null the method will print null. </remarks>
/// <param name="jagArr"> jagged array of ints. </param>
private void Write(int[][] jagArr)
{
	if (jagArr != null)
	{
		if (jagArr.Length > 0)
		{
			string str;
			int n;
			if (jagArr[0] != null)
			{
				str = "[ [ ";
				n = jagArr[0].Length;
				if (n > 0)
				{
					for (int j = 0; j < n - 1; j++)
						str += String.Format("{0}, ", jagArr[0][j]);
					str += String.Format("{0} ] ", jagArr[0][n - 1]);
				}
				else
					str += "] ";
				if (jagArr.Length == 1)
					str += "]";
				Print(str);
			}
			else
				Print("[ [ null ]");
			if (jagArr.Length > 1)
			{
				int count = jagArr.Length;
				for (int i = 1; i < count - 1; i++)
				{
					if (jagArr[i] != null)
					{
						n = jagArr[i].Length;
						str = "  [ ";
						if (n > 0)
						{
							for (int j = 0; j < n - 1; j++)
								str += String.Format("{0}, ", jagArr[i][j]);
							str += String.Format("{0} ]", jagArr[i][n - 1]);
						}
						else
							str += "]";
						Print(str);
					}
					else
						Print("  [ null ]");
				}
				if (jagArr[count - 1] != null)
				{
					n = jagArr[count - 1].Length;
					str = "  [ ";
					if (n > 0)
					{
						for (int j = 0; j < n - 1; j++)
							str += String.Format("{0}, ", jagArr[count - 1][j]);
						str += String.Format("{0} ] ]", jagArr[count - 1][n - 1]);
					}
					else
						str += "] ]";
					Print(str);
				}
				else
					Print("  [ null ] ]");
			}
		}
		else
			Print("[ [ ] ]");
	}
	else
		Print("[ [ null ] ]");
}

/// <summary>
/// Print entire provided jagged array in NinjaScript Output Widow.
/// </summary>
/// <remarks> If jagged array is null the method will print null. </remarks>
/// <param name="jagArr"> jagged array of doubles. </param>
private void Write(double[][] jagArr)
{
	if (jagArr != null)
	{
		if (jagArr.Length > 0)
		{
			string str;
			int n;
			if (jagArr[0] != null)
			{
				str = "[ [ ";
				n = jagArr[0].Length;
				if (n > 0)
				{
					for (int j = 0; j < n - 1; j++)
						str += String.Format("{0}, ", jagArr[0][j]);
					str += String.Format("{0} ] ", jagArr[0][n - 1]);
				}
				else
					str += "] ";
				if (jagArr.Length == 1)
					str += "]";
				Print(str);
			}
			else
				Print("[ [ null ]");
			if (jagArr.Length > 1)
			{
				int count = jagArr.Length;
				for (int i = 1; i < count - 1; i++)
				{
					if (jagArr[i] != null)
					{
						n = jagArr[i].Length;
						str = "  [ ";
						if (n > 0)
						{
							for (int j = 0; j < n - 1; j++)
								str += String.Format("{0}, ", jagArr[i][j]);
							str += String.Format("{0} ]", jagArr[i][n - 1]);
						}
						else
							str += "]";
						Print(str);
					}
					else
						Print("  [ null ]");
				}
				if (jagArr[count - 1] != null)
				{
					n = jagArr[count - 1].Length;
					str = "  [ ";
					if (n > 0)
					{
						for (int j = 0; j < n - 1; j++)
							str += String.Format("{0}, ", jagArr[count - 1][j]);
						str += String.Format("{0} ] ]", jagArr[count - 1][n - 1]);
					}
					else
						str += "] ]";
					Print(str);
				}
				else
					Print("  [ null ] ]");
			}
		}
		else
			Print("[ [ ] ]");
	}
	else
		Print("[ [ null ] ]");
}

/// <summary>
/// Print entire provided nested array in NinjaScript Output Widow.
/// </summary>
/// <remarks> If nested array is null the method will print null. </remarks>
/// <param name="nestArr"> nested array of ints. </param>
private void Write(List<int[]> nestArr)
{
	if (nestArr != null)
	{
		if (nestArr.Count > 0)
		{
			string str;
			int n;
			if (nestArr[0] != null)
			{
				str = "( [ ";
				n = nestArr[0].Length;
				if (n > 0)
				{
					for (int j = 0; j < n - 1; j++)
						str += String.Format("{0}, ", nestArr[0][j]);
					str += String.Format("{0} ] ", nestArr[0][n - 1]);
				}
				else
					str += "] ";
				if (nestArr.Count == 1)
					str += ")";
				Print(str);
			}
			else
				Print("( [ null ]");
			if (nestArr.Count > 1)
			{
				int count = nestArr.Count;
				for (int i = 1; i < count - 1; i++)
				{
					if (nestArr[i] != null)
					{
						n = nestArr[i].Length;
						str = "  [ ";
						if (n > 0)
						{
							for (int j = 0; j < n - 1; j++)
								str += String.Format("{0}, ", nestArr[i][j]);
							str += String.Format("{0} ]", nestArr[i][n - 1]);
						}
						else
							str += "]";
						Print(str);
					}
					else
						Print("  [ null ]");
				}
				if (nestArr[count - 1] != null)
				{
					n = nestArr[count - 1].Length;
					str = "  [ ";
					if (n > 0)
					{
						for (int j = 0; j < n - 1; j++)
							str += String.Format("{0}, ", nestArr[count - 1][j]);
						str += String.Format("{0} ] )", nestArr[count - 1][n - 1]);
					}
					else
						str += "] )";
					Print(str);
				}
				else
					Print("  [ null ] )");
			}
		}
		else
			Print("( [ ] )");
	}
	else
		Print("( [ null ] )");
}

/// <summary>
/// Print entire provided nested array in NinjaScript Output Widow.
/// </summary>
/// <remarks> If nested array is null the method will print null. </remarks>
/// <param name="nestArr"> nested array of doubles. </param>
private void Write(List<double[]> nestArr)
{
	if (nestArr != null)
	{
		if (nestArr.Count > 0)
		{
			string str;
			int n;
			if (nestArr[0] != null)
			{
				str = "( [ ";
				n = nestArr[0].Length;
				if (n > 0)
				{
					for (int j = 0; j < n - 1; j++)
						str += String.Format("{0}, ", nestArr[0][j]);
					str += String.Format("{0} ] ", nestArr[0][n - 1]);
				}
				else
					str += "] ";
				if (nestArr.Count == 1)
					str += ")";
				Print(str);
			}
			else
				Print("( [ null ]");
			if (nestArr.Count > 1)
			{
				int count = nestArr.Count;
				for (int i = 1; i < count - 1; i++)
				{
					if (nestArr[i] != null)
					{
						n = nestArr[i].Length;
						str = "  [ ";
						if (n > 0)
						{
							for (int j = 0; j < n - 1; j++)
								str += String.Format("{0}, ", nestArr[i][j]);
							str += String.Format("{0} ]", nestArr[i][n - 1]);
						}
						else
							str += "]";
						Print(str);
					}
					else
						Print("  [ null ]");
				}
				if (nestArr[count - 1] != null)
				{
					n = nestArr[count - 1].Length;
					str = "  [ ";
					if (n > 0)
					{
						for (int j = 0; j < n - 1; j++)
							str += String.Format("{0}, ", nestArr[count - 1][j]);
						str += String.Format("{0} ] )", nestArr[count - 1][n - 1]);
					}
					else
						str += "] )";
					Print(str);
				}
				else
					Print("  [ null ] )");
			}
		}
		else
			Print("( [ ] )");
	}
	else
		Print("( [ null ] )");
}

/// <summary>
/// Print entire provided nested list in NinjaScript Output Widow.
/// </summary>
/// <remarks> If nested list is null the method will print null. </remarks>
/// <param name="nestList"> nested list of ints. </param>
private void Write(List<List<int>> nestList)
{
	if (nestList != null)
	{
		if (nestList.Count > 0)
		{
			string str;
			int n;
			if (nestList[0] != null)
			{
				str = "( ( ";
				n = nestList[0].Count;
				if (n > 0)
				{
					for (int j = 0; j < n - 1; j++)
						str += String.Format("{0}, ", nestList[0][j]);
					str += String.Format("{0} ) ", nestList[0][n - 1]);
				}
				else
					str += ") ";
				if (nestList.Count == 1)
					str += ")";
				Print(str);
			}
			else
				Print("( ( null )");
			if (nestList.Count > 1)
			{
				int count = nestList.Count;
				for (int i = 1; i < count - 1; i++)
				{
					if (nestList[i] != null)
					{
						n = nestList[i].Count;
						str = "  ( ";
						if (n > 0)
						{
							for (int j = 0; j < n - 1; j++)
								str += String.Format("{0}, ", nestList[i][j]);
							str += String.Format("{0} )", nestList[i][n - 1]);
						}
						else
							str += ")";
						Print(str);
					}
					else
						Print("  ( null )");
				}
				if (nestList[count - 1] != null)
				{
					n = nestList[count - 1].Count;
					str = "  ( ";
					if (n > 0)
					{
						for (int j = 0; j < n - 1; j++)
							str += String.Format("{0}, ", nestList[count - 1][j]);
						str += String.Format("{0} ) )", nestList[count - 1][n - 1]);
					}
					else
						str += ") )";
					Print(str);
				}
				else
					Print("  ( null ) )");
			}
		}
		else
			Print("( ( ) )");
	}
	else
		Print("( ( null ) )");
}

/// <summary>
/// Print entire provided nested list in NinjaScript Output Widow.
/// </summary>
/// <remarks> If nested list is null the method will print null. </remarks>
/// <param name="nestList"> nested list of doubles. </param>
private void Write(List<List<double>> nestList)
{
	if (nestList != null)
	{
		if (nestList.Count > 0)
		{
			string str;
			int n;
			if (nestList[0] != null)
			{
				str = "( ( ";
				n = nestList[0].Count;
				if (n > 0)
				{
					for (int j = 0; j < n - 1; j++)
						str += String.Format("{0}, ", nestList[0][j]);
					str += String.Format("{0} ) ", nestList[0][n - 1]);
				}
				else
					str += ") ";
				if (nestList.Count == 1)
					str += ")";
				Print(str);
			}
			else
				Print("( ( null )");
			if (nestList.Count > 1)
			{
				int count = nestList.Count;
				for (int i = 1; i < count - 1; i++)
				{
					if (nestList[i] != null)
					{
						n = nestList[i].Count;
						str = "  ( ";
						if (n > 0)
						{
							for (int j = 0; j < n - 1; j++)
								str += String.Format("{0}, ", nestList[i][j]);
							str += String.Format("{0} )", nestList[i][n - 1]);
						}
						else
							str += ")";
						Print(str);
					}
					else
						Print("  ( null )");
				}
				if (nestList[count - 1] != null)
				{
					n = nestList[count - 1].Count;
					str = "  ( ";
					if (n > 0)
					{
						for (int j = 0; j < n - 1; j++)
							str += String.Format("{0}, ", nestList[count - 1][j]);
						str += String.Format("{0} ) )", nestList[count - 1][n - 1]);
					}
					else
						str += ") )";
					Print(str);
				}
				else
					Print("  ( null ) )");
			}
		}
		else
			Print("( ( ) )");
	}
	else
		Print("( ( null ) )");
}