The following Pine Script v6 indicator demonstrates how to log entry conditions, print variable values with formatted text, and guard against excessive log output using barstate filters.
Pine Script v6: Structured debugging with log.info and log.warningpinescript
//@version=6
indicator("Debug Logger Example [v6]", overlay = true)
// Input parameters
int fastLength = input.int(9, "Fast MA Length", minval = 1)
int slowLength = input.int(21, "Slow MA Length", minval = 1)
// Sanity check input parameters
if fastLength >= slowLength
log.warning("Fast length ({0}) is greater than or equal to slow length ({1})", fastLength, slowLength)
// Calculations
float fastMa = ta.ema(close, fastLength)
float slowMa = ta.ema(close, slowLength)
bool bullCross = ta.crossover(fastMa, slowMa)
bool bearCross = ta.crossunder(fastMa, slowMa)
// Visual plots
plot(fastMa, "Fast EMA", color = color.green, linewidth = 2)
plot(slowMa, "Slow EMA", color = color.red, linewidth = 2)
// Conditional logging on signal events
if bullCross
log.info("Bullish Cross on bar index {0} | Close: {1} | Fast MA: {2}",
bar_index, str.tostring(close, "#.##"), str.tostring(fastMa, "#.##"))
if bearCross
log.info("Bearish Cross on bar index {0} | Close: {1} | Slow MA: {2}",
bar_index, str.tostring(close, "#.##"), str.tostring(slowMa, "#.##"))
// Log summary once on the latest confirmed bar
if barstate.islastconfirmedhistory
log.info("Historical calculation complete. Total bars processed: {0}", bar_index + 1)