Tyche Trading

Price Rounding (Tick)

Bar Info

Debugging

Derivatives

Matrix Handling

Price Rounding

Regression Lines

Statistics

User Tracking

Example Strategies

Price Rounding (Tick)

Description:

Round an inexact price value to the nearest tick, or round up/down.

 

Syntax:

NearestTickValue(double price);
RoundUpTickValue(double price);
RoundDownTickValue(double price);

 

Arguments:

price: The inexact price value that needs to be adjusted.

 

Use Case:

Often times doing complex calculations can leave an inexact price value, or if you want to know if a bar is near or equal to a calculated value

these methods become very useful.

				
					/// <summary>
/// Finds the price to the nearest tick of an inexact price.
/// </summary>
/// <param name="price"> The inexact original price. </param>
/// <returns> The exact price closest to the original price. </returns> 
private double NearestTickValue(double price)
{
	double testSize = TickSize;
	double decimalVal = price % 1;
	double diff = decimalVal;

	// set new price to prevent modifing the original
	double newPrice = price - decimalVal;

	// test each price index to find which one is closest to actual value
	int index = 0;
	int count = Convert.ToInt32(1 / testSize);
	for (int i = 1; i <= count; i++)
	{
		// ">=" rounds up if decimal is exactly between two tick values
		// turn to ">" if desire is to round down
		if (diff >= Math.Abs(testSize * i - decimalVal))
		{
			diff = Math.Abs(testSize * i - decimalVal);
			index = i;
		}
	}

	newPrice += index * testSize;

	return newPrice;
}

/// <summary>
/// Rounds up price to the next tick value.
/// </summary>
/// <param name="price"> The inexact original price. </param>
/// <returns> The exact price rounded up to the next tick. </returns>
private double RoundUpTickValue(double price)
{
	double testSize = TickSize;
	double decimalVal = price % 1;

	// set new price to prevent modifing the original
	double newPrice = price - decimalVal;

	// test each tick range to determine best round
	int index = 0;
	int count = Convert.ToInt32(1 / testSize);
	for (int i = 0; i <= count; i++)
    {
		// if decimal is greater than a tick value and less than or equal to the next tick value
		if (decimalVal > i * testSize && decimalVal <= (i + 1) * testSize)
			index = i + 1;
    }

	newPrice += index * testSize;

	return newPrice;
}

/// <summary>
/// Rounds down price to the next tick value.
/// </summary>
/// <param name="price"> The inexact original price. </param>
/// <returns> The exact price rounded down to the next tick. </returns>
private double RoundDownTickValue(double price)
{
	double testSize = TickSize;
	double decimalVal = price % 1;

	// set new price to prevent modifing the original
	double newPrice = price - decimalVal;

	// test each tick range to determine best round
	int index = 0;
	int count = Convert.ToInt32(1 / testSize);
	for (int i = 0; i <= count; i++)
	{
		// if decimal is greater than a tick value and less than or equal to the next tick value
		if (decimalVal >= i * testSize && decimalVal < (i + 1) * testSize)
			index = i;
	}

	newPrice += index * testSize;

	return newPrice;
}