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. 

Backtest Rookies is a registered with Brave publisher!

Brave users can drop us a tip.

Alternatively, support us by switching to Brave using this referral link and we will receive some BAT! 


https://brave.com/bac691

Referral Link

Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up! 

https://tradingview.go2cloud.org/SHpB


Donate with PayPal using any payment method you are comfortable with! 


3HxNVyh5729ieTfPnTybrtK7J7QxJD9gjD

0x9a2f88198224d59e5749bacfc23d79507da3d431

M8iUBnb8KamHR18gpgi2sSG7jopddFJi8S