Tradingview: Rounding to the nearest tick in Pine Script

In this short article we will take a look at rounding to the nearest tick. This can be useful for anyone who is using Tradingview’s alert system to send real order. Rounding to the nearest tick will ensure that orders are sent at valid levels and not rejected by your broker.

The Snippet

Let’s dive straight in with the snippet. The functions below can be copy and pasted into any script. There are two functions. One which will round up to the nearest tick and one which will round down. The functions have the same two inputs (called arguments):

  • x, a series, e.g. a calculated stop loss level.
  • mintick, a float or integer that we will round to the nearest value.

If the code above looks complex, fear not, we have a tutorial on functions in pine script:

Full Example

The example below has a couple of simple inputs so you can test each function easily. In reality, you would not manually enter the mintickvalue yourself but instead grab it using the built-in variable syminfo.mintick.

On the charts

This one is not much to look at but should look something like this:

A chart showing the rounded function working on the charts

Using a single, simple line makes it easy to test whether the rounding is working or not. Go on, try a few values and see for yourself!

Find This Post Useful?

If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.