如何在Pine Script中绘制动态多路径预测股价?解决TradingView线条数量限制难题
Hey there, I totally get your frustration with the line.new limit—270 lines is way over TradingView's 50-object cap, and plot with offset not playing nice with loops is a real pain. Let's break down the best way to solve this using Pine Script's series-based plotting, which avoids the object count limit entirely.
Core Idea: Use Arrays + Series Plotting
Instead of creating hundreds of individual line objects, we'll store all our predicted high/low values for each path in arrays, then convert those arrays to series that plot() can handle. Each path only needs 2 plot calls (one for high, one for low), so 3 paths = just 6 plots—way under the limit. Plus, we can use fill() to shade the range between each path's high and low for better readability.
Step-by-Step Implementation
Here's a working example you can adapt to your actual prediction logic:
//@version=5 indicator("Dynamic Multi-Path Price Prediction", overlay=true) // Configure prediction parameters prediction_days = 90 num_paths = 3 // Initialize arrays to store predicted values (var = persist across bars) var float[] path1_high = array.new_float(prediction_days) var float[] path1_low = array.new_float(prediction_days) var float[] path2_high = array.new_float(prediction_days) var float[] path2_low = array.new_float(prediction_days) var float[] path3_high = array.new_float(prediction_days) var float[] path3_low = array.new_float(prediction_days) // Recalculate predictions whenever the spot price updates (last bar only) if barstate.islast current_price = close // Replace with your spot price source if needed // Loop through each prediction day to calculate values for i from 0 to prediction_days - 1 // --- Replace this with your actual prediction logic --- // Path 1: Bullish scenario (example: +2% daily gain, -1% downside) array.set(path1_high, i, current_price * (1 + 0.02) ^ (i + 1)) array.set(path1_low, i, current_price * (1 - 0.01) ^ (i + 1)) // Path 2: Bearish scenario (example: +1% upside, -2% daily loss) array.set(path2_high, i, current_price * (1 + 0.01) ^ (i + 1)) array.set(path2_low, i, current_price * (1 - 0.02) ^ (i + 1)) // Path 3: Neutral scenario (example: ±0.5% daily range) array.set(path3_high, i, current_price * (1 + 0.005) ^ (i + 1)) array.set(path3_low, i, current_price * (1 - 0.005) ^ (i + 1)) // Convert arrays to series (required for plot()) p1_h = array.to_series(path1_high) p1_l = array.to_series(path1_low) p2_h = array.to_series(path2_high) p2_l = array.to_series(path2_low) p3_h = array.to_series(path3_high) p3_l = array.to_series(path3_low) // Plot each path's high/low and fill the range plot(p1_h, color=color.new(color.green, 0), title="Path 1 High", offset=-prediction_days + 1) plot(p1_l, color=color.new(color.green, 0), title="Path 1 Low", offset=-prediction_days + 1) fill(plot1, plot2, color=color.new(color.green, 90), title="Path 1 Range") plot(p2_h, color=color.new(color.orange, 0), title="Path 2 High", offset=-prediction_days + 1) plot(p2_l, color=color.new(color.orange, 0), title="Path 2 Low", offset=-prediction_days + 1) fill(plot3, plot4, color=color.new(color.orange, 90), title="Path 2 Range") plot(p3_h, color=color.new(color.red, 0), title="Path 3 High", offset=-prediction_days + 1) plot(p3_l, color=color.new(color.red, 0), title="Path 3 Low", offset=-prediction_days + 1) fill(plot5, plot6, color=color.new(color.red, 90), title="Path 3 Range")
Key Details to Note
- Persistent Arrays: Using
varwhen initializing arrays ensures they don't reset on every bar—we only update them when the last bar forms (i.e., when the spot price changes). - Offset Calculation: The
offset=-prediction_days + 1ensures the first predicted value aligns with the next bar after the current one, stretching subsequent values 90 days into the future. - Dynamic Updates: Since we recalculate on
barstate.islast, predictions automatically refresh whenever the latest price data comes in—perfect for your dynamic requirement. - No Object Limit Issues: Each
plot()is a single series-based object, regardless of how many data points it has. Even with 90 days of predictions, you only use 6 plots total.
Alternative: Simplify with Custom Functions
If you want cleaner code (especially if you add more paths later), wrap the prediction logic in a custom function to avoid repetition:
// Custom function to calculate path values f_calculate_path(_current_price, _upside_perc, _downside_perc, _days) => var float[] high_vals = array.new_float(_days) var float[] low_vals = array.new_float(_days) if barstate.islast for i from 0 to _days - 1 array.set(high_vals, i, _current_price * (1 + _upside_perc) ^ (i + 1)) array.set(low_vals, i, _current_price * (1 - _downside_perc) ^ (i + 1)) [array.to_series(high_vals), array.to_series(low_vals)] // Use the function for each path [p1_h, p1_l] = f_calculate_path(close, 0.02, 0.01, prediction_days) [p2_h, p2_l] = f_calculate_path(close, 0.01, 0.02, prediction_days) [p3_h, p3_l] = f_calculate_path(close, 0.005, 0.005, prediction_days)
This makes your code more maintainable if you ever need to adjust the number of paths or prediction logic.
内容的提问来源于stack exchange,提问作者ALEUNG

