Pine Script编译错误:无法在局部作用域使用plot函数求助
Root Cause
In Pine Script, plot() is a declaration function, not an executable runtime function. It defines plot properties once during script initialization, and cannot be called inside local scopes like if blocks (which handle per-bar logic). This is why you encounter the "local scope" compilation error.
Solution
Instead of calling plot() conditionally, use one of these approaches:
1. Use plot() with a Conditional Series
Define a series that returns your target value only when the condition is true, and na otherwise. This way, the plot is declared once, but only renders when your condition is met.
2. Use plotshape() (Recommended for Signals)
For buy/sell signals, plotshape() is more appropriate—it draws shapes (arrows, labels, circles) on the chart when conditions are satisfied, which aligns better with entry/exit marker use cases than plotting a fixed value.
Corrected Code
Here’s the fixed version using plotshape() for signals, plus a fix for the multi-timeframe calculation issue (your original code didn’t actually use the input timeframes):
//@version=5 strategy(title="My Indicator", shorttitle="My Indicator", overlay=true) // Inputs myPeriod = input(defval=14, title='Period') red = color.red green = color.green // Thresholds rsiThreshold = input(defval=70) stochThreshold = input(defval=70) // Time frames (in minutes) shortTimeFrame = input(defval=5) mediumTimeFrame = input(defval=15) longTimeFrame = input(defval=30) // Calculate indicators across different time frames using request.security() [rsiShort, stochShort, macdShort, signalShort] = request.security(syminfo.tickerid, tostring(shortTimeFrame), [ta.rsi(close, myPeriod), ta.stoch(high, low, close, myPeriod), ta.ema(close, 12) - ta.ema(close, 26), ta.ema(ta.ema(close,12)-ta.ema(close,26),9)]) [rsiMedium, stochMedium, macdMedium, signalMedium] = request.security(syminfo.tickerid, tostring(mediumTimeFrame), [ta.rsi(close, myPeriod), ta.stoch(high, low, close, myPeriod), ta.ema(close, 12) - ta.ema(close, 26), ta.ema(ta.ema(close,12)-ta.ema(close,26),9)]) [rsiLong, stochLong, macdLong, signalLong] = request.security(syminfo.tickerid, tostring(longTimeFrame), [ta.rsi(close, myPeriod), ta.stoch(high, low, close, myPeriod), ta.ema(close, 12) - ta.ema(close, 26), ta.ema(ta.ema(close,12)-ta.ema(close,26),9)]) // Define signal conditions sellCondition = rsiShort > rsiThreshold and rsiMedium > rsiThreshold and macdShort < signalShort and macdMedium < signalMedium buyCondition = stochMedium > stochThreshold and stochLong > stochThreshold and macdMedium > signalMedium and macdLong > signalLong // Plot signals using plotshape plotshape(sellCondition, title="Sell Signal", location=location.belowbar, color=red, style=shape.labeldown, text="SELL") plotshape(buyCondition, title="Buy Signal", location=location.abovebar, color=green, style=shape.labelup, text="BUY")
Key Notes:
- Multi-Timeframe Fix: The original code calculated all indicators on the current chart timeframe. Using
request.security()correctly fetches data from the specified 5/15/30-minute time frames. - Plotshape Efficiency:
plotshape()accepts a boolean condition directly, drawing the shape only when the condition is true—no need forifblocks around plotting functions.
内容的提问来源于stack exchange,提问作者Dr. Degen

