开发者

Compilation error. Line 42: Cannot use 'plot' in local scope

开发者 https://www.devze.com 2022-12-07 17:26 出处:网络
sorry if this is basic in pinescript, I\'m in first steps of learning and playing around with OpenAI generated code...

sorry if this is basic in pinescript, I'm in first steps of learning and playing around with OpenAI generated code...

I can't wrap my head around why it cant be compiled with a plot inside the if in script below:

//@version=5
// This script uses multiple indicators, including the relative strength index (RSI), the moving average convergence divergence (MACD),
// and the stochastic oscillator, and multiple time frames, to identify potential opportunities to enter or exit the market.
strategy(title="My Indicator", shorttitle="My Indicator", overlay=true)

// inputs 
myPeriod = input(defval=14, title='Period')
red = color.red
green = color.green


// Set the thresholds for the RSI and the stochastic oscillator (values between 0 and 100)
rsiThreshold = input(defval=70)
stochThreshold = input(defval=70)

// Set the time frames to use (in minutes)
shortTimeFrame = input(defval=5)
mediumTimeFrame = input(defval=15)
longTimeFrame = input(defval=30)

// Calculate the RSI and the stochastic oscillator for the short, medium, and long time frames
rsiShort = ta.rsi(close, myPeriod)
stochShort = ta.stoch(high, low, close, myPeriod)
rsiMedium = ta.rsi(close, myPeriod)
stochMedium = ta.stoch(high, low, close, myPeriod)
rsiLong = ta.rsi(close, myPeriod)
stochLong = ta.stoch(high, low, close, myPeriod)

// Calculate the MACD for the short, medium, and long time frames
fastLength = input(defval=12)
slowLength = input(defval=26)
macdLength = input(defval=9)
macdShort = ta.ema(close, fastLength) - ta.ema(close, slowLength)
macdMedium = ta.ema(close, fastLength) - ta.ema(close, slowLength)
macdLong = ta.ema(close, fastLength) - ta.ema(close, slowLength)
signalShort = ta.ema(macdShort, macdLength)
signalMedium = ta.ema(macdMedium, macdLength)
signalLong = ta.ema(macdLong, macdLength)

// If the RSI is above the threshold and the MACD is trending downward on the short and medium time frames, show a sell signal
if (rsiShort > rsiThreshold and rsiMedium > rsiThreshold and macdShort < signalShort and macdMedium < signalMedium)
    plot(0, title="Sell Signal", color=red)

// If the stochastic oscillator is above the threshold and the MACD is trending upward on the medium and long time frames, show a buy signal
if (stochMedium > stochThreshold and stochLong > stochThreshold and macdMedium > signalMedium and macdLong > signalLong)
    plot(0, ti开发者_Python百科tle="Buy Signal", color=green)


Tried throwing around if's but I'm a bit confused and missing the syntactical point i guess lol


There are workaround for using a plot function with a condition, but in your case it seems you want to use the plotshape function, that plots a shape whenever a condition is true. For example:

sellCond = (rsiShort > rsiThreshold and rsiMedium > rsiThreshold and macdShort < signalShort and macdMedium < signalMedium)
plotshape(sellCond, "Sell Signal", shape.labeldown, location.abovebar, color.green, textcolor = color.white)

buyCond = (stochMedium > stochThreshold and stochLong > stochThreshold and macdMedium > signalMedium and macdLong > signalLong)
plotshape(buyCond, "Buy Signal", shape.labelup, location.belowbar, color.red, textcolor = color.white)
0

精彩评论

暂无评论...
验证码 换一张
取 消