How to round to full hundreds (100s) in TradingView's Pine Script?
How to round to full hundreds (100s) in TradingView’s Pine Script?
Section titled “How to round to full hundreds (100s) in TradingView’s Pine Script?”TL;DR
Divide by 100, applymath.round,math.floor, ormath.ceil, then multiply back by 100 to snap any value to the nearest, lower, or higher hundred.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 5-10 min |
| Category | Math Tools |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Round to hundreds", overlay=false)
nearestHundred(x) => math.round(x / 100.0) * 100.0
rounded = nearestHundred(close)plot(rounded, "Close rounded to 100")Why It Matters
Section titled “Why It Matters”Many instruments (indices, crypto, custom tickers) behave around round numbers. Rounding to full hundreds lets you set psychological alert levels, print clean labels, or normalise data for dashboards without manual math each time.
What You’ll Learn
Section titled “What You’ll Learn”- Implement nearest, floor, and ceiling rounding to 100s.
- Encapsulate the logic in helper functions for reuse.
- Apply rounded values to plots, labels, and strategy thresholds.
- Combine rounding with formatting helpers to produce clean output.
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
math.round(value) | Rounds to nearest integer (ties to even). |
math.floor(value) | Rounds down to the largest integer ≤ value. |
math.ceil(value) | Rounds up to the smallest integer ≥ value. |
str.tostring(value, format) | Formats rounded values for labels/alerts. |
hline(price) | Draws horizontal levels at rounded hundreds. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Create rounding helpers
Keep the conversion logic in functions so everything reads cleanly.roundToHundred(float v) => math.round(v / 100.0) * 100.0floorHundred(float v) => math.floor(v / 100.0) * 100.0ceilHundred(float v) => math.ceil(v / 100.0) * 100.0 -
Apply the helper where needed
Use it on prices, indicator values, or user inputs.roundedClose = roundToHundred(close)stopLevel = floorHundred(strategy.position_avg_price) -
Visualise and act on the rounded values
Plot or annotate them so users see the adjusted level.plot(roundedClose, "Rounded close", color=color.new(color.orange, 0))label.new(bar_index, roundedClose, "Nearest 100: " + str.tostring(roundedClose, format.price))
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Hundred-level dashboard", overlay=true, max_labels_count=500)
toNearestHundred(float v) => math.round(v / 100.0) * 100.0floorHundred(float v) => math.floor(v / 100.0) * 100.0ceilHundred(float v) => math.ceil(v / 100.0) * 100.0
nearest = toNearestHundred(close)roundLow = floorHundred(low)roundHigh = ceilHundred(high)
plot(nearest, "Nearest 100", color=color.new(color.blue, 0))plot(roundLow, "Floor 100", color=color.new(color.green, 50))plot(roundHigh, "Ceil 100", color=color.new(color.red, 50))
if barstate.isconfirmed txt = "Nearest: " + str.tostring(nearest, format.price) + "\nFloor: " + str.tostring(roundLow, format.price) + "\nCeil: " + str.tostring(roundHigh, format.price) label.new(bar_index, nearest, txt, style=label.style_label_left, textcolor=color.white, bgcolor=color.new(color.blue, 60))- Dedicated helpers prevent repeated divide/multiply boilerplate throughout the script.
- Visualising floor/ceil levels exposes nearby support and resistance at clean numbers.
- Labels summarise the rounded levels so alerts or strategies can reuse them easily.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- For assets quoted in cents, rounding to hundreds equates to dollars. Adjust the factor (e.g., 1000) to fit other key denominations.
- Remember that
math.rounduses banker’s rounding; add+ 0.5beforemath.floorif you prefer traditional rounding. - Combine with
math.clampif you also need to restrict values to a bounded range after rounding. - Use integer casting when the rounded number should feed into size calculations:
int(roundToHundred(value) / 100).
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”Rounding results look off by a few cents
Section titled “Rounding results look off by a few cents”Ensure you divide/multiply by a float (100.0). Using integers can cause unexpected truncation due to integer division.
How do I round to the nearest 500 or 1000 instead?
Section titled “How do I round to the nearest 500 or 1000 instead?”Replace the 100 factor with whichever step you need: step = 500; math.round(value / step) * step.
Can I round indicator values before plotting to reduce noise?
Section titled “Can I round indicator values before plotting to reduce noise?”Yes—apply the helper to the indicator output and plot both raw and rounded versions to see the smoothing effect.
Key Takeaways
Section titled “Key Takeaways”- Divide by 100, round with your preferred method, and multiply back to snap values to full hundreds.
- Wrap the logic in helpers so scripts stay readable and reusable.
- Rounded levels help you align alerts, labels, and strategy thresholds with psychological price areas.
- Adjust the factor to extend the technique to 50s, 500s, or any other denomination your market respects.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse Strategy Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.