The following Pine Script v6 script demonstrates how to format prices and variables to generate formatted text labels on the chart and construct clean JSON strings for external trading webhooks.
Pine Script v6: Dynamic string conversion and JSON payload builderpinescript
//@version=6
indicator("String Formatting & Webhook Builder [v6]", overlay = true)
// Inputs
float stopMultiplier = input.float(1.5, "Stop ATR Multiplier", minval = 0.5)
string actionType = input.string("BUY", "Order Action", options = ["BUY", "SELL"])
// Calculations
float atrVal = ta.atr(14)
float stopPrice = actionType == "BUY" ? close - (atrVal * stopMultiplier) : close + (atrVal * stopMultiplier)
// Format numbers with tick precision
string priceStr = str.tostring(close, format.mintick)
string stopStr = str.tostring(stopPrice, format.mintick)
string timeStr = str.format_time(time, "yyyy-MM-dd HH:mm", "UTC")
// Construct multi-line on-chart dashboard text using str.format
string displayText = str.format(
"Status: {0}\nPrice: {1}\nStop: {2}\nTime: {3}",
actionType, priceStr, stopStr, timeStr)
// Render on-chart label on current bar
var label statusLabel = na
if barstate.islast
label.delete(statusLabel)
statusLabel := label.new(
bar_index, high, displayText,
color = color.navy, textcolor = color.white,
style = label.style_label_down)
// Generate JSON alert payload for webhook dispatch
string jsonPayload = str.format(
'{{"action":"{0}","symbol":"{1}","price":{2},"stop":{3},"timestamp":"{4}"}}',
actionType, syminfo.ticker, priceStr, stopStr, timeStr)
// Trigger condition example
bool signal = ta.crossover(close, ta.ema(close, 20))
if signal
alert(jsonPayload, alert.freq_once_per_bar_close)