How to size TradingView labels with a Pine Script input?
How to size TradingView labels with a Pine Script input?
Section titled “How to size TradingView labels with a Pine Script input?”TL;DR
Offer a dropdown with the supported label sizes, translate the selection into alabel.size_*constant, and reuse that value whenever you create or update labels.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Visual Effects |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Resizable labels", overlay=true, max_labels_count=500)
labelSizeInput = input.string("default", "Label size", options=["very small", "small", "default", "large", "very large"])
labelSizeFromInput(string value) => switch value "very small" => label.size_tiny "small" => label.size_small "large" => label.size_large "very large" => label.size_huge => label.size_normal
sizeSetting = labelSizeFromInput(labelSizeInput)
if barstate.islast label.new(bar_index, high, text="Size: " + labelSizeInput, style=label.style_label_down, textcolor=color.white, bgcolor=color.new(color.blue, 65), size=sizeSetting)Why It Matters
Section titled “Why It Matters”Charts rarely ship with one-size-fits-all annotations. Allowing traders to resize labels helps them fit more (or less) text, align with branding, or keep overlays readable on different monitors. Inputs provide the control; a small helper function keeps the sizing logic consistent everywhere labels are used.
What You’ll Learn
Section titled “What You’ll Learn”- Capture label-size preferences with
input.string(). - Translate human-readable dropdown values into Pine’s
label.size_*enum. - Apply the selected size when creating new labels or updating existing ones.
- Reuse the helper across multiple label types (signals, stats, tooltips).
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
input.string(defval, title, options=[]) | Renders a dropdown for label size choices. |
label.size_* | Enum constants Pine expects when sizing labels. |
label.new(x, y, text, size=...) | Creates an annotation with the chosen size. |
label.set_size(id, size) | Updates an existing label when preferences change. |
switch | Cleanly maps input strings to size constants. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Collect the size preference
Use a dropdown with descriptive options so users recognise each size.sizeChoice = input.string("default", "Label size", options=["very small", "small", "default", "large", "very large"], tooltip="Controls label.size_* constant") -
Map strings to
label.size_*constants
Convert the selection once and reuse the result throughout the script.labelSize(string value) =>switch value"very small" => label.size_tiny"small" => label.size_small"large" => label.size_large"very large" => label.size_huge=> label.size_normal -
Apply the size to labels
Pass the mapped constant tolabel.new()orlabel.set_size()whenever you render annotations.sizeSetting = labelSize(sizeChoice)tradeLabel = label.new(bar_index, high, text="Signal", size=sizeSetting)
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Signal labels with sizing", overlay=true, max_labels_count=500)
sizeChoice = input.string("default", "Label size", options=["very small", "small", "default", "large", "very large"], tooltip="Pick how big signal labels should render")textColour = input.color(color.white, "Text colour")bgColour = input.color(color.new(color.blue, 70), "Background colour")
labelSize(string value) => switch value "very small" => label.size_tiny "small" => label.size_small "large" => label.size_large "very large" => label.size_huge => label.size_normal
sizeSetting = labelSize(sizeChoice)
fast = ta.ema(close, 12)slow = ta.ema(close, 26)
bullish = ta.crossover(fast, slow)bearish = ta.crossunder(fast, slow)
if bullish label.new(bar_index, low, "Bull", style=label.style_label_up, size=sizeSetting, textcolor=textColour, bgcolor=bgColour)
if bearish id = label.new(bar_index, high, "Bear", style=label.style_label_down, textcolor=textColour, bgcolor=bgColour) label.set_size(id, sizeSetting)- A single helper translates user-friendly text into Pine’s enum values.
- Both new labels and post-creation adjustments respect the same preference.
- Separating colour inputs from size keeps the settings panel tidy but flexible.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”label.size_autois available if you’d rather Pine adapt the size to text, but users often prefer explicit control.- When you allow emojis or icons in labels, bump the default size to keep them legible.
- Reapply
label.set_size()after clearing/recreating labels so they stay in sync with updated input values. - If you manage dozens of labels, cache the size constant in a global variable to avoid recomputing inside loops.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”The dropdown shows values I didn’t provide
Section titled “The dropdown shows values I didn’t provide”Each option must be unique. Double-check the options array and make sure the default exists inside it.
Changing the input doesn’t resize existing labels
Section titled “Changing the input doesn’t resize existing labels”Labels retain their original size until you call label.set_size() with the new constant. Re-run updates on the next bar or when detecting an input change (if barstate.isnew and sizeChoice != sizeChoice[1]).
Can I expose different sizes for different label types?
Section titled “Can I expose different sizes for different label types?”Absolutely—duplicate the input (e.g., “Signal label size”, “Stats label size”) and keep separate helper calls for each context.
Key Takeaways
Section titled “Key Takeaways”- Collect label-size preferences via
input.string()so users can resize annotations quickly. - Translate those strings to
label.size_*constants with a small helper before applying them. - Use the mapped size for both new labels and any adjustments you make later via
label.set_size(). - Consistent sizing inputs improve readability while keeping scripts flexible for different chart setups.
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.