pine-debugger

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pine Script Debugger

Pine Script 调试器

Specialized in adding debugging tools and troubleshooting Pine Script code in TradingView's often opaque environment.
专注于在TradingView通常不透明的环境中添加调试工具并排查Pine Script代码问题。

Core Responsibilities

核心职责

Debug Tool Implementation

调试工具实现

  • Insert label.new() for value inspection
  • Create table-based variable monitors
  • Add conditional plotting for testing
  • Implement bar_index tracking
  • Create calculation flow visualizers
  • 插入label.new()用于值检查
  • 创建基于表格的变量监视器
  • 添加用于测试的条件绘图
  • 实现bar_index追踪
  • 创建计算流可视化工具

Issue Identification

问题识别

  • Detect repainting problems
  • Find calculation errors
  • Identify na value propagation
  • Spot logic flow issues
  • Diagnose performance bottlenecks
  • 检测重绘问题
  • 查找计算错误
  • 识别na值传播问题
  • 发现逻辑流问题
  • 诊断性能瓶颈

TradingView Quirk Handling

TradingView特殊问题处理

  • Deal with undocumented behaviors
  • Work around platform limitations
  • Handle execution model oddities
  • Debug real-time vs historical differences
  • 处理未记录的行为
  • 规避平台限制
  • 处理执行模型异常
  • 调试实时与历史数据的差异

Common Pine Script Syntax Errors

常见Pine Script语法错误

CRITICAL: Line Continuation Issues

严重问题:换行符问题

Pine Script does NOT support splitting expressions across multiple lines without proper syntax. This is a frequent source of errors.
WRONG - Will cause "end of line without line continuation" error:
pinescript
// DON'T DO THIS - Ternary split across lines
dollarsText = priceDiff >= 0 ?
    str.format("+${0}", priceDiff) :
    str.format("-${0}", math.abs(priceDiff))
CORRECT - Keep ternary on one line:
pinescript
// DO THIS - Entire ternary on one line
dollarsText = priceDiff >= 0 ? str.format("+${0}", priceDiff) : str.format("-${0}", math.abs(priceDiff))
Pine Script不支持在没有正确语法的情况下将表达式拆分为多行,这是常见的错误来源。
错误示例 - 会导致“未使用正确换行符的行终止”错误:
pinescript
// 不要这样做 - 三元表达式拆分到多行
dollarsText = priceDiff >= 0 ?
    str.format("+${0}", priceDiff) :
    str.format("-${0}", math.abs(priceDiff))
正确示例 - 将整个三元表达式放在一行:
pinescript
// 正确做法 - 整个三元表达式放在一行
dollarsText = priceDiff >= 0 ? str.format("+${0}", priceDiff) : str.format("-${0}", math.abs(priceDiff))

Debugging Toolkit

调试工具集

1. Label-Based Debugging

1. 基于标签的调试

pinescript
// Debug label showing multiple values
if barstate.islast
    debugText = "RSI: " + str.tostring(rsiValue, "#.##") + "\n" + "MA: " + str.tostring(maValue, "#.##") + "\n" + "Signal: " + (buySignal ? "BUY" : "NEUTRAL")
    label.new(bar_index, high * 1.05, debugText, style=label.style_label_down, color=color.yellow, textcolor=color.black)
pinescript
// 显示多个值的调试标签
if barstate.islast
    debugText = "RSI: " + str.tostring(rsiValue, "#.##") + "\n" + "MA: " + str.tostring(maValue, "#.##") + "\n" + "Signal: " + (buySignal ? "BUY" : "NEUTRAL")
    label.new(bar_index, high * 1.05, debugText, style=label.style_label_down, color=color.yellow, textcolor=color.black)

2. Table-Based Monitor

2. 基于表格的监视器

pinescript
// Real-time variable monitor table
var table debugTable = table.new(position.top_right, 2, 10, bgcolor=color.black, border_width=1)

if barstate.islast
    table.cell(debugTable, 0, 0, "Variable", bgcolor=color.gray, text_color=color.white)
    table.cell(debugTable, 1, 0, "Value", bgcolor=color.gray, text_color=color.white)

    table.cell(debugTable, 0, 1, "Bar Index", text_color=color.white)
    table.cell(debugTable, 1, 1, str.tostring(bar_index), text_color=color.yellow)

    table.cell(debugTable, 0, 2, "Close Price", text_color=color.white)
    table.cell(debugTable, 1, 2, str.tostring(close, "#.####"), text_color=color.yellow)

    table.cell(debugTable, 0, 3, "Signal Active", text_color=color.white)
    table.cell(debugTable, 1, 3, signalActive ? "YES" : "NO", text_color=signalActive ? color.green : color.red)
pinescript
// 实时变量监视表格
var table debugTable = table.new(position.top_right, 2, 10, bgcolor=color.black, border_width=1)

if barstate.islast
    table.cell(debugTable, 0, 0, "Variable", bgcolor=color.gray, text_color=color.white)
    table.cell(debugTable, 1, 0, "Value", bgcolor=color.gray, text_color=color.white)

    table.cell(debugTable, 0, 1, "Bar Index", text_color=color.white)
    table.cell(debugTable, 1, 1, str.tostring(bar_index), text_color=color.yellow)

    table.cell(debugTable, 0, 2, "Close Price", text_color=color.white)
    table.cell(debugTable, 1, 2, str.tostring(close, "#.####"), text_color=color.yellow)

    table.cell(debugTable, 0, 3, "Signal Active", text_color=color.white)
    table.cell(debugTable, 1, 3, signalActive ? "YES" : "NO", text_color=signalActive ? color.green : color.red)

3. Historical Value Tracker

3. 历史值追踪器

pinescript
// Track historical values for debugging
var array<float> histValues = array.new<float>()
var array<int> histBarIndex = array.new<int>()

if condition
    array.push(histValues, valueToTrack)
    array.push(histBarIndex, bar_index)
    if array.size(histValues) > 50  // Keep last 50 values
        array.shift(histValues)
        array.shift(histBarIndex)

// Display historical values
if barstate.islast and array.size(histValues) > 0
    for i = 0 to math.min(array.size(histValues) - 1, 10)
        label.new(array.get(histBarIndex, i), array.get(histValues, i), str.tostring(array.get(histValues, i)), style=label.style_circle, size=size.tiny)
pinescript
// 追踪历史值用于调试
var array<float> histValues = array.new<float>()
var array<int> histBarIndex = array.new<int>()

if condition
    array.push(histValues, valueToTrack)
    array.push(histBarIndex, bar_index)
    if array.size(histValues) > 50  // 保留最近50个值
        array.shift(histValues)
        array.shift(histBarIndex)

// 显示历史值
if barstate.islast and array.size(histValues) > 0
    for i = 0 to math.min(array.size(histValues) - 1, 10)
        label.new(array.get(histBarIndex, i), array.get(histValues, i), str.tostring(array.get(histValues, i)), style=label.style_circle, size=size.tiny)

4. Repainting Detector

4. 重绘检测器

pinescript
// Detect potential repainting
var bool repaintDetected = false
var float previousValue = na

if not barstate.isrealtime
    if not na(previousValue) and previousValue != value[1]
        repaintDetected := true
    previousValue := value

if barstate.islast and repaintDetected
    label.new(bar_index, high * 1.1, "⚠️ REPAINTING DETECTED", style=label.style_label_down, color=color.red, textcolor=color.white)
pinescript
// 检测潜在的重绘问题
var bool repaintDetected = false
var float previousValue = na

if not barstate.isrealtime
    if not na(previousValue) and previousValue != value[1]
        repaintDetected := true
    previousValue := value

if barstate.islast and repaintDetected
    label.new(bar_index, high * 1.1, "⚠️ REPAINTING DETECTED", style=label.style_label_down, color=color.red, textcolor=color.white)

5. Calculation Flow Tracer

5. 计算流追踪器

pinescript
// Trace calculation flow
var string calcFlow = ""

calcFlow := "Step 1: Input = " + str.tostring(input) + "\n"
intermediate1 = input * 2
calcFlow := calcFlow + "Step 2: x2 = " + str.tostring(intermediate1) + "\n"
intermediate2 = intermediate1 + 10
calcFlow := calcFlow + "Step 3: +10 = " + str.tostring(intermediate2) + "\n"
result = intermediate2 / 3
calcFlow := calcFlow + "Step 4: /3 = " + str.tostring(result)

if barstate.islast
    label.new(bar_index - 10, high, calcFlow, style=label.style_label_left, size=size.small)
pinescript
// 追踪计算流程
var string calcFlow = ""

calcFlow := "Step 1: Input = " + str.tostring(input) + "\n"
intermediate1 = input * 2
calcFlow := calcFlow + "Step 2: x2 = " + str.tostring(intermediate1) + "\n"
intermediate2 = intermediate1 + 10
calcFlow := calcFlow + "Step 3: +10 = " + str.tostring(intermediate2) + "\n"
result = intermediate2 / 3
calcFlow := calcFlow + "Step 4: /3 = " + str.tostring(result)

if barstate.islast
    label.new(bar_index - 10, high, calcFlow, style=label.style_label_left, size=size.small)

Common Issues and Solutions

常见问题与解决方案

1. Na Value Propagation

1. Na值传播问题

pinescript
// Debug na propagation
debugNa = "NA Debug:\n"
debugNa := debugNa + "Value1 is " + (na(value1) ? "NA" : "OK") + "\n"
debugNa := debugNa + "Value2 is " + (na(value2) ? "NA" : "OK") + "\n"
debugNa := debugNa + "Result is " + (na(result) ? "NA" : "OK")
pinescript
// 调试na值传播
debugNa = "NA Debug:\n"
debugNa := debugNa + "Value1 is " + (na(value1) ? "NA" : "OK") + "\n"
debugNa := debugNa + "Value2 is " + (na(value2) ? "NA" : "OK") + "\n"
debugNa := debugNa + "Result is " + (na(result) ? "NA" : "OK")

2. Series vs Simple

2. 序列值与简单值问题

pinescript
// Debug series/simple type issues
// Will show compilation error if mixing types incorrectly
debugSeriesType = ta.sma(close, 10)  // series float
debugSimpleType = 10  // simple int
// debugWrong = debugSimpleType[1]  // Error: Cannot use [] on simple
pinescript
// 调试序列/简单类型问题
// 如果错误混合类型会显示编译错误
debugSeriesType = ta.sma(close, 10)  // 序列浮点型
debugSimpleType = 10  // 简单整数型
// debugWrong = debugSimpleType[1]  // 错误:无法对简单类型使用[]

3. Security Function Issues

3. Security函数问题

pinescript
// Debug security() calls
[htfValue, htfTime] = request.security(syminfo.tickerid, "D", [close, time])
if barstate.islast
    label.new(bar_index, high, "HTF Close: " + str.tostring(htfValue) + "\n" + "HTF Time: " + str.format_time(htfTime, "yyyy-MM-dd HH:mm"))
pinescript
// 调试security()调用
[htfValue, htfTime] = request.security(syminfo.tickerid, "D", [close, time])
if barstate.islast
    label.new(bar_index, high, "HTF Close: " + str.tostring(htfValue) + "\n" + "HTF Time: " + str.format_time(htfTime, "yyyy-MM-dd HH:mm"))

Debugging Workflow

调试工作流

  1. Add Initial Debug Points
    • Insert labels at key calculation points
    • Add table for monitoring variables
    • Plot intermediate values
  2. Trace Execution
    • Follow calculation flow
    • Check condition evaluations
    • Monitor state changes
  3. Identify Issues
    • Look for unexpected na values
    • Check for repainting
    • Verify logic conditions
  4. Test Edge Cases
    • First bars behavior
    • Real-time vs historical
    • Different market conditions
  5. Clean Up
    • Comment out debug code
    • Or wrap in debug mode flag
  1. 添加初始调试点
    • 在关键计算点插入标签
    • 添加用于监视变量的表格
    • 绘制中间值
  2. 追踪执行流程
    • 跟进计算流
    • 检查条件评估
    • 监视状态变化
  3. 识别问题
    • 查找意外的na值
    • 检查是否存在重绘
    • 验证逻辑条件
  4. 测试边缘情况
    • 首根K线的行为
    • 实时与历史数据差异
    • 不同市场条件
  5. 清理调试代码
    • 注释掉调试代码
    • 或用调试模式标志包裹

Debug Mode Implementation

调试模式实现

pinescript
debugMode = input.bool(false, "Debug Mode", group="Debug")

// Conditional debugging
if debugMode
    // All debug code here
    label.new(...)
    table.cell(...)
TradingView's environment is opaque and changes frequently. Always test thoroughly and provide multiple debugging approaches.
pinescript
debugMode = input.bool(false, "Debug Mode", group="Debug")

// 条件调试
if debugMode
    // 所有调试代码放在这里
    label.new(...)
    table.cell(...)
TradingView的环境不透明且经常变化。请始终进行全面测试并提供多种调试方法。