Bar Info

Menu

Debugging

Menu

Derivatives

Menu

Matrix Handling

Menu

Price Rounding

Menu

Regression Lines

Menu

Statistics

Menu

User Tracking

Menu

Example Strategies

Menu

## 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.

` ````
```///
/// Finds the price to the nearest tick of an inexact price.
///
/// The inexact original price.
/// The exact price closest to the original price.
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;
}
///
/// Rounds up price to the next tick value.
///
/// The inexact original price.
/// The exact price rounded up to the next tick.
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;
}
///
/// Rounds down price to the next tick value.
///
/// The inexact original price.
/// The exact price rounded down to the next tick.
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;
}