Plan Limit Timer

WHAT THIS LIBRARY DOES
TradingView enforces different script timeout limits based on subscription tier:
• Basic: 20 seconds
• Essential / Plus / Premium: 40 seconds
• Ultimate: 100 seconds
This library measures your script's total execution time and displays it relative to these limits, helping you optimize indicators for users on different plans.
THE DEBUG TABLE
When enabled, a table appears on your chart showing:
• Plan: The selected TradingView subscription tier
• Limit: Maximum allowed execution time for that plan
• Runtime: Measured script execution time
• Per Bar: Average time spent per bar
• Bars: Number of bars processed
• % Used: Percentage of timeout limit consumed (color-coded)
• Status: OK (green), WARNING (yellow), DANGER (orange), or EXCEEDED (red)
HOW IT WORKS
The library captures a timestamp at the start of your script using timenow, then calculates the elapsed time at the end. It compares this against the selected plan's timeout limit to determine percentage used and status.
Technical Note: Pine Script's timenow variable has approximately 1-second precision. Scripts that execute in under 1 second may display 0ms. This is a platform limitation, not a library issue. For detailed per-function profiling, use TradingView's built-in Pine Profiler (More → Profiler mode in the Editor).
EXPORTED FUNCTIONS
startTimer()
Call at the very beginning of your script. Returns a timestamp.
getStats(startTime, plan)
Calculates timing statistics. Returns a TimingStats object with all metrics.
showTimingTable(stats, plan, tablePosition, showOnlyOnLast)
Renders the debug table on the chart.
debugTiming(startTime, plan, tablePosition)
Convenience function combining getStats() and showTimingTable() in one call.
isApproachingLimit(stats, threshold)
Returns true if execution time has reached the specified percentage of the limit.
getRemainingMs(stats)
Returns milliseconds remaining before timeout.
formatSummary(stats)
Returns a compact single-line string for labels or tooltips.
addTimingLabel(stats, barIdx, price, labelStyle, textSize)
Creates a color-coded chart label displaying timing statistics. Useful for visual debugging without the full table. Returns the label object for further customization.
EXPORTED CONSTANTS
• LIMIT_BASIC = 20
• LIMIT_ESSENTIAL = 40
• LIMIT_PLUS = 40
• LIMIT_PREMIUM = 40
• LIMIT_ULTIMATE = 100
EXPORTED TYPE: TimingStats
Object containing:
• totalTimeMs (float): Total execution time in milliseconds
• timePerBarMs (float): Average time per bar
• barsTimed (int): Number of bars measured
• barsSkipped (int): Bars excluded from measurement
• planLimitMs (int): Plan timeout in milliseconds
• percentUsed (float): Percentage of limit consumed
• status (string): "OK", "WARNING", "DANGER", or "EXCEEDED"
HOW TO USE IN YOUR INDICATOR
//version=6
indicator("My Indicator", overlay = true)
import YourUsername/PlanLimitTimer/1 as timer
// User selects their TradingView plan
planInput = input.string("basic", "Your Plan", options = ["basic", "essential", "plus", "premium", "ultimate"])
// START TIMING - must be first
startTime = timer.startTimer()
// Your indicator calculations here
sma20 = ta.sma(close, 20)
rsi14 = ta.rsi(close, 14)
plot(sma20)
// END TIMING - must be last
timer.debugTiming(startTime, planInput)
ADVANCED USAGE EXAMPLE
//version=6
indicator("Advanced Example", overlay = true)
import YourUsername/PlanLimitTimer/1 as timer
planInput = input.string("basic", "Plan", options = ["basic", "essential", "plus", "premium", "ultimate"])
startTime = timer.startTimer()
// Your calculations...
sma = ta.sma(close, 200)
plot(sma)
// Get stats for programmatic use
stats = timer.getStats(startTime, planInput)
// Option 1: Use addTimingLabel for a quick visual indicator
if barstate.islast
timer.addTimingLabel(stats, bar_index, high)
// Option 2: Show custom warning label if approaching limit
if timer.isApproachingLimit(stats, 70.0) and barstate.islast
label.new(bar_index, low, "Warning: " + timer.formatSummary(stats),
color = color.orange, textcolor = color.white, style = label.style_label_up)
// Display the debug table
timer.showTimingTable(stats, planInput, position.bottom_right)
IMPORTANT LIMITATIONS
1. Precision: Timing precision is approximately 1 second due to timenow behavior. Fast scripts show 0ms.
2. Variability: Results vary based on TradingView server load. The same script may show different times across runs.
3. Total Time Only: This library measures total script execution time, not individual function timing. For per-function analysis, use the Pine Profiler in the Editor.
4. Historical Bars: On historical bars, timenow reflects when the script loaded, not individual bar processing times.
USE CASES
• Optimization Debugging: See how close your script is to timeout limits
• Multi-Plan Support: Help users select appropriate settings for their subscription tier
• Performance Regression: Detect when changes increase execution time
• Documentation: Show users the performance characteristics of your indicator
Perpustakaan pine
Dengan semangat TradingView yang sesungguhnya, penulis telah menerbitkan kode Pine ini sebagai pustaka sumber terbuka agar programmer Pine lain dari komunitas kami dapat menggunakannya kembali. Salut untuk penulis! Anda dapat menggunakan pustaka ini secara pribadi atau dalam publikasi sumber terbuka lainnya, tetapi penggunaan kembali kode ini dalam publikasi diatur oleh Tata Tertib.
Pernyataan Penyangkalan
Perpustakaan pine
Dengan semangat TradingView yang sesungguhnya, penulis telah menerbitkan kode Pine ini sebagai pustaka sumber terbuka agar programmer Pine lain dari komunitas kami dapat menggunakannya kembali. Salut untuk penulis! Anda dapat menggunakan pustaka ini secara pribadi atau dalam publikasi sumber terbuka lainnya, tetapi penggunaan kembali kode ini dalam publikasi diatur oleh Tata Tertib.