0

Compilation error. Line 23: Mismatched input 'plot' expecting 'end of line without line continuation'.

// Plot the trend indicator EMA if it's enabled if (useTrendIndicator) plot(trendIndicator, title="Trend Indicator EMA", color=color.blue)

If I give it an indentation it gives me:

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

Here is the rest of the code:

//@version=4
strategy("EMA Strategy")

//Input parameters
emaShortPeriod = input(100, minval=1)
emaLongPeriod = input(150, minval=1)
trendIndicatorPeriod = input(5, minval=1)
useTrendIndicator = input(true)
stopLoss = input(100, minval=0)
takeProfit = input(5, minval=0)

//Define the EMA series
emaShort = ema(close, emaShortPeriod)
emaLong = ema(close, emaLongPeriod)
trendIndicator = ema(close, trendIndicatorPeriod)

//Plot the EMA series on the chart
plot(emaShort, title="EMA (short period)", color=color.green)
plot(emaLong, title="EMA (long period)", color=color.orange)

//Plot the trend indicator EMA if it's enabled
if (useTrendIndicator)
plot(trendIndicator, title="Trend Indicator EMA", color=color.blue)

//Entry signals
longEntry = crossover(emaShort, emaLong)
shortEntry = crossunder(emaShort, emaLong)

//entry signals on the chart
plotchar(longEntry, "Long Entry", "▲", location.top, color = #00FF00, transp = 0)
plotchar(shortEntry, "Short Entry", "▼", location.top, color = #FF0000, transp = 0)

//Only enter a trade if the trend indicator EMA is above or below the price
if (useTrendIndicator)
    longEntry = longEntry and close > trendIndicator
    shortEntry = shortEntry and close < trendIndicator

//Function to generate market orders when the entry signals are triggered
strategy.entry("Long", strategy.long, when=longEntry)
strategy.entry("Short", strategy.short, when=shortEntry)

//Exit signals
longExitForShort = crossover(emaLong, trendIndicator)
shortExitForLong = crossunder(emaLong, trendIndicator)

//Plot the exit signals on the chart
plotchar(longExitForShort, "Long Exit for Short", "▼", location.bottom, color = #FF0000, transp = 0)
plotchar(shortExitForLong, "Short Exit for Long", "▲", location.bottom, color = #00FF00, transp = 0)

//Function to generate market orders when the exit signals are triggered
if (longExitForShort)
    strategy.close("Long")
if (shortExitForLong)
    strategy.close("Short")

//Set the stop loss and take profit levels for each trade
strategy.exit("Long Exit", "Long", stop=strategy.position_avg_price * (1 - stopLoss/100), limit=strategy.position_avg_price * (1 + takeProfit/100))
strategy.exit("Short Exit", "Short", stop=strategy.position_avg_price * (1 + stopLoss/100), limit=strategy.position_avg_price * (1 - takeProfit/100))

3 emas

  • 2 of them make the long and short signals

  • 3rd one being used as a trend indicator. If turned on longs will only trade above this ema and the other way around for shorts. Short signals above the ema will still be used as exits for longs and the other way around

Gittiwilo
  • 11
  • 2

1 Answers1

0

The solution:
plot(useTrendIndicator ? trendIndicator : na, title="Trend Indicator EMA", color=color.blue)

The error says it all. In pine script the plot() functions can only be used in the global scope that's why you need to use a ternary operator in the series source of the plot function instead of an if conditional above it that would put your plot into a local scope.

Suggestion: use pine script v5 instead. It's the current version and has many valuable updates.

elod008
  • 1,227
  • 1
  • 5
  • 15