Debugging

Técnicas de debugging no Pine Script

Neste capítulo

Técnicas de Debugging

Debugging no Pine Script

1. Use plotchar() para Data Window

plotchar(valor, "Nome", "", location.top, size=size.tiny)

O valor aparece na Data Window sem plotar nada no gráfico.

2. Use label.new() para valores dinâmicos

if barstate.islast
    label.new(bar_index, high, str.tostring(meuValor))

3. Use bgcolor() para estados

bgcolor(condicao1 ? color.green : 
        condicao2 ? color.red : na)

4. Tabelas para informações complexas

var table t = table.new(position.top_right, 2, 3)
if barstate.islast
    table.cell(t, 0, 0, "Valor:")
    table.cell(t, 1, 0, str.tostring(valor))

5. Checar valores na

O valor na é a causa de muitos bugs:

valorSeguro = nz(valorPodeSerNa, valorPadrao)
Template de Debugging

Exemplo completo de técnicas de debug

1//@version=6
2indicator("Debug Template", overlay=true)
3
4// Cálculos exemplo
5sma20 = ta.sma(close, 20)
6rsi = ta.rsi(close, 14)
7cruzamento = ta.crossover(close, sma20)
8
9// DEBUG 1: Data Window
10plotchar(sma20, "SMA20", "", location.top, size=size.tiny)
11plotchar(rsi, "RSI", "", location.top, size=size.tiny)
12plotchar(cruzamento ? 1 : 0, "Cruzamento", "", location.top, size=size.tiny)
13
14// DEBUG 2: Visual markers
15plotshape(cruzamento, "Cross", shape.circle, location.belowbar, color.green)
16
17// DEBUG 3: Background para estados
18bgcolor(rsi > 70 ? color.new(color.red, 90) :
19        rsi < 30 ? color.new(color.green, 90) : na)
20
21// DEBUG 4: Tabela de informações
22var table debugTable = table.new(position.top_right, 2, 5, bgcolor=color.black)
23
24if barstate.islast
25    table.cell(debugTable, 0, 0, "Variável", text_color=color.white)
26    table.cell(debugTable, 1, 0, "Valor", text_color=color.white)
27    table.cell(debugTable, 0, 1, "SMA20", text_color=color.gray)
28    table.cell(debugTable, 1, 1, str.tostring(sma20, "#.##"), text_color=color.blue)
29    table.cell(debugTable, 0, 2, "RSI", text_color=color.gray)
30    table.cell(debugTable, 1, 2, str.tostring(rsi, "#.##"), text_color=color.purple)
31    table.cell(debugTable, 0, 3, "Close", text_color=color.gray)
32    table.cell(debugTable, 1, 3, str.tostring(close, "#.##"), text_color=color.white)
33    table.cell(debugTable, 0, 4, "Bar Index", text_color=color.gray)
34    table.cell(debugTable, 1, 4, str.tostring(bar_index), text_color=color.orange)
35
36plot(sma20, "SMA 20", color.blue, 2)