Skip to content

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, apply math.round, math.floor, or math.ceil, then multiply back by 100 to snap any value to the nearest, lower, or higher hundred.

DifficultyBeginner
Time to implement5-10 min
CategoryMath Tools
//@version=5
indicator("Round to hundreds", overlay=false)
nearestHundred(x) => math.round(x / 100.0) * 100.0
rounded = nearestHundred(close)
plot(rounded, "Close rounded to 100")
Tip. Accept floats in helpers but cast back to integers (`int()`) if you need whole numbers for lot sizing or label text.

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.

  • 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.
CallPurpose
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.
  1. Create rounding helpers
    Keep the conversion logic in functions so everything reads cleanly.

    roundToHundred(float v) => math.round(v / 100.0) * 100.0
    floorHundred(float v) => math.floor(v / 100.0) * 100.0
    ceilHundred(float v) => math.ceil(v / 100.0) * 100.0
  2. Apply the helper where needed
    Use it on prices, indicator values, or user inputs.

    roundedClose = roundToHundred(close)
    stopLevel = floorHundred(strategy.position_avg_price)
  3. 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))
//@version=5
indicator("Hundred-level dashboard", overlay=true, max_labels_count=500)
toNearestHundred(float v) => math.round(v / 100.0) * 100.0
floorHundred(float v) => math.floor(v / 100.0) * 100.0
ceilHundred(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))
Why this works.
  • 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.
  • 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.round uses banker’s rounding; add + 0.5 before math.floor if you prefer traditional rounding.
  • Combine with math.clamp if 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).

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.

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

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.