The example logs input ordering once, crossover events on confirmed bars, and a summary at the last confirmed historical bar. Logs describe conditions, not orders or fills. Historical timestamps identify bar opening time; realtime log timestamps identify the event time.
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 barstate.isfirst and 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 and barstate.isconfirmed
log.info("Bullish Cross on bar index {0} | Close: {1} | Fast MA: {2}",
bar_index, str.tostring(close, "#.##"), str.tostring(fastMa, "#.##"))
if bearCross and barstate.isconfirmed
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)