Congratulations on reaching the advanced debugging features! This tutorial will guide you through using all the new Phase 3 capabilities.
# Start with our comprehensive test indicator
node scripts/pinescript/debug-server.js --file examples/pinescript-projects/debug-examples/phase3-comprehensive-test.pine --port 3000- Main Interface: http://localhost:3000/
- Debug Interface: http://localhost:3000/debug
Use the comprehensive test indicator to see all features in action!
Memory profiling helps you:
- Track memory usage of variables, arrays, and series
- Detect memory leaks before they cause issues
- Optimize memory usage for better performance
- Set warning and critical thresholds
-
Enable Memory Profiling in the test indicator
- Turn on "Enable Memory Profiling" input
- Set "Memory Warning Threshold" to 30
- Set "Memory Critical Threshold" to 50
-
Watch Memory Usage
- Observe the "Avg Memory" plot (purple line)
- Look for warnings when thresholds are exceeded
-
Try Different Scenarios
- Increase "Max Bars to Process" to see memory growth
- Enable/disable different indicator calculations
- Watch how memory usage changes
//@include "examples/pinescript-projects/debug-helpers/debug-memory.pine"
// Configure memory profiling
memoryProfilingEnabled = input.bool(true, "Enable Memory Profiling")
warningThreshold = input.int(50, "Memory Warning Threshold")
// Track variable memory
myVariable = close * 2
debug.trackVariable("myVariable", myVariable, "variable")
// Profile a code block
result = debug.profileMemory("Complex Calculation", () =>
ta.rsi(close, 14) * ta.sma(close, 20)
)
// Visualize memory usage
debug.plotMemoryUsage()
debug.showMemoryTable()
Code coverage analysis helps you:
- Track which parts of your code are executed
- Identify untested code sections
- Generate tests for uncovered code
- Measure testing completeness
-
Enable Code Coverage in the test indicator
- Turn on "Enable Code Coverage"
- Set "Debug Level" to 2 or higher
-
Watch Coverage Data
- Check the debug table on the last bar
- Look at "Functions Called" and "Conditions Checked"
- Observe the coverage percentage
-
Experiment with Code Paths
- Change indicator parameters to trigger different conditions
- Watch how coverage metrics change
- Try to achieve 100% coverage!
//@include "examples/pinescript-projects/debug-helpers/debug-coverage.pine"
// Register functions for coverage tracking
myFunction() =>
debug.registerFunction("myFunction")
debug.markFunctionExecuted("myFunction")
// Your function logic here
result = close * 2
result
// Track conditions
condition = close > open
debug.registerCondition()
if condition
debug.markConditionExecuted()
debug.markBranchExecuted() // True branch
else
debug.markBranchExecuted() // False branch
// View coverage information
debug.showCoverageTable()
debug.plotCoverageTrend()
Automated test generation helps you:
- Create tests from actual execution data
- Capture edge cases automatically
- Generate unit, integration, and condition tests
- Build comprehensive test suites
-
Enable Test Generation in the test indicator
- Turn on "Enable Test Generation"
- Set "Debug Level" to 3
-
Collect Test Data
- Let the indicator run for several bars
- Watch test cases accumulate in the background
-
View Generated Tests
- Check the "Test Data Export" label on last bar
- See how many test cases were collected
- Examine the test data format
//@include "examples/pinescript-projects/debug-helpers/debug-testgen.pine"
// Collect test data during execution
value = close
calculated = myCalculation(value)
debug.collectTestData(value, calculated, "Close price calculation")
// Record function calls
debug.recordFunctionCall("myCalculation", [value], calculated)
// Record edge cases
if close == 0
debug.recordEdgeCase("Zero price", [close], calculated)
// Generate and view tests
if barstate.islast
testSuite = debug.generateTestSuite()
debug.log(testSuite)
Performance benchmarking helps you:
- Compare your code against standard indicators
- Measure execution time and memory usage
- Calculate accuracy scores
- Get performance ratings and recommendations
-
Enable Benchmarking in the test indicator
- Turn on "Enable Benchmarking"
- Set "Debug Level" to 2 or higher
-
Analyze Performance
- Watch the "Avg Exec Time" plot (orange line)
- Check the performance summary on last bar
- Look at the performance score and rating
-
Compare Implementations
- Try different calculation methods
- Watch how performance metrics change
- Aim for "Excellent" or "Good" ratings
//@include "examples/pinescript-projects/debug-helpers/debug-benchmark.pine"
// Benchmark your custom RSI
customRSI = debug.benchmark("My RSI", () =>
// Your custom RSI implementation
ta.rsi(close, 14)
)
// Compare against standard RSI
[rsiRatio, rsiAccuracy, testTime, standardTime] = debug.benchmarkRSI(() =>
customRSI
)
// View benchmark results
debug.showBenchmarkTable()
debug.plotPerformanceTrends()
Session export/import helps you:
- Save and restore debugging sessions
- Share sessions with team members
- Analyze sessions offline
- Track debugging history
-
Start a Debugging Session
- Load the comprehensive test indicator
- Set some breakpoints
- Run a few bars of execution
-
Export the Session
# Export to JSON file curl http://localhost:3000/api/export/file?format=json > my-session.json # Or use the web interface export button
-
Examine the Export
# Look at the exported data cat my-session.json | python3 -m json.tool | head -50
-
Import the Session
# Import from JSON file curl -X POST -H "Content-Type: application/json" \ -d @my-session.json \ http://localhost:3000/api/import
- Export: Click the "Export Session" button in the debug interface
- Import: Click the "Import Session" button and select a file
- Formats: Choose between JSON (programmatic) and CSV (spreadsheet)
TradingView integration helps you:
- Use debug functions within TradingView constraints
- Export data from TradingView to debug server
- Maintain a seamless development workflow
- Deploy debug-enabled indicators safely
-
Examine the TV Example
# Look at the TradingView example cat examples/pinescript-projects/tradingview-integration/tv-debug-example.pine | head -100
-
Key Concepts to Note
- Conditional debugging with input controls
- Performance-optimized debug functions
- Data export via chart labels
- Level-based debug controls
-
Integration Workflow
Develop → Debug Server (full features) ↓ Test → With comprehensive debugging ↓ Extract → TV-compatible debug functions ↓ Deploy → To TradingView (debug disabled) ↓ Enable → Debugging when needed ↓ Export → Data back to debug server
//@version=5
indicator("TV Debug", overlay=true)
// Debug controls
debugEnabled = input.bool(false, "Enable Debugging")
debugLevel = input.int(1, "Debug Level", 0, 3)
// TV-compatible debug functions
debugPlot(value, title) =>
if debugEnabled and debugLevel >= 1
plot(value, title, color=color.new(color.blue, 70))
// Usage
rsi = ta.rsi(close, 14)
debugPlot(rsi, "RSI Debug")
// Data export on last bar
if barstate.islast and debugEnabled
exportData = "close=" + str.tostring(close) + ",rsi=" + str.tostring(rsi)
label.new(bar_index, low, "📋 " + exportData, color=color.new(color.blue, 90))
Goal: Create an indicator that demonstrates a memory leak and use memory profiling to detect it.
Steps:
- Create an array that grows without cleanup
- Enable memory profiling
- Run for many bars
- Watch for memory leak warnings
- Fix the leak and verify
Goal: Achieve 100% code coverage on a simple indicator.
Steps:
- Create a simple indicator with multiple conditions
- Enable code coverage tracking
- Test all possible code paths
- Use coverage data to identify gaps
- Add tests for uncovered code
Goal: Optimize an indicator to achieve "Excellent" performance rating.
Steps:
- Start with a working but slow indicator
- Enable benchmarking
- Identify performance bottlenecks
- Optimize the slowest parts
- Verify improved performance score
Goal: Use all Phase 3 features in a complete development workflow.
Steps:
- Develop indicator in debug server
- Use memory profiling and coverage analysis
- Generate tests automatically
- Benchmark against standards
- Deploy to TradingView with debug support
- Export/import sessions as needed
Issue: Indicator runs slowly with memory profiling. Solution:
- Increase warning/critical thresholds
- Use
debug.profileMemory()selectively - Disable memory profiling in production
Issue: Coverage percentages seem incorrect. Solution:
- Ensure all code paths are registered
- Check
mark...Executed()calls - Verify coverage configuration
Issue: Not enough test cases generated. Solution:
- Increase
maxTestCasesparameter - Manually record edge cases
- Run through more diverse scenarios
Issue: Benchmark results vary between runs. Solution:
- Increase
benchmarkIterations - Ensure sufficient
warmupBars - Run benchmarks multiple times
Issue: Debug functions don't work in TradingView. Solution:
- Use TV-compatible functions from examples
- Test with minimal configuration first
- Disable unsupported features
ADVANCED-DEBUGGING.md: Complete Phase 3 documentationSETUP-DEBUGGING.md: Basic debugging setup guidetradingview-integration.md: TV integration details
phase3-comprehensive-test.pine: All features in one indicatortv-debug-example.pine: TradingView implementationdebug-*.pine: Individual feature helpers
- Debug Server API:
/api/export,/api/import, etc. - CLI Commands:
pine-debug profilewith advanced options - PineScript Functions:
debug.*functions for each feature
- Start with the comprehensive test indicator
- Enable one feature at a time
- Follow the hands-on exercises
- Try the interactive challenges
- Integrate features into your own indicators
- Develop a complete workflow
- Optimize performance based on benchmarks
- Create comprehensive test suites
- Extend the debug helpers with custom features
- Integrate with CI/CD pipelines
- Develop custom benchmarking suites
- Create specialized TradingView integrations
You've completed the Phase 3 Advanced Debugging tutorial! You now have access to:
✅ Memory Profiling - Optimize memory usage and detect leaks
✅ Code Coverage - Ensure complete testing of your code
✅ Test Generation - Automatically create comprehensive test suites
✅ Performance Benchmarking - Compare against standards and optimize
✅ Session Export/Import - Collaborate and analyze debugging sessions
✅ TradingView Integration - Seamless development and deployment workflow
These tools will help you develop more robust, efficient, and maintainable PineScript indicators. Happy debugging! 🐛🔧
Need Help?
- Review the documentation files
- Examine the example indicators
- Try the interactive challenges
- Refer to the troubleshooting guide
Ready to Build? Start integrating these features into your own PineScript projects today!