How to round to the higher, lower or nearest tick

Here are three simple functions to round a price to its higher, lower or nearest tick.

function RoundToHigherTick( price )
{
    return ceil( price / TickSize ) * TickSize;
}

function RoundToLowerTick( price )
{
    return floor( price / TickSize ) * TickSize;
}

function RoundToNearestTick( price )
{
    rounded = RoundToHigherTick( price );
    return IIf( rounded - price > TickSize / 2, roundToLowerTick( price ), rounded );
}

They are useful when you need to set the BuyPrice, SellPrice, ShortPrice or CoverPrice array to values ‚Äč‚Äčthat you have calculated, but which are not equal to valid price levels.

You can download the code here

Leave a Reply

Your email address will not be published. Required fields are marked *