Reputation: 11
im new to this, please advise what am i doing wrong?
TRADINGview pinescript strategy backtest EMAs crossovers of the 13-day and 48.5-day averages
i get this error
Compilation error. Line 11: no viable alternative at input 'if'. Try to add line '//@version=2' to the top of your script
// Set the lookback periods for the EMAs
lookbackShort = 13
lookbackLong = 48.5
// Calculate the short and long EMAs
emaShort = ema(close, lookbackShort)
emaLong = ema(close, lookbackLong)
// Check if the short EMA crosses above the long EMA
if crossover(emaShort, emaLong)
strategy.entry("Long", strategy.long)
// Check if the short EMA crosses below the long EMA
if crossunder(emaShort, emaLong)
strategy.exit("Close Long", "Long", strategy.close)
// Plot the EMAs on the chart
plot(emaShort, color=red)
plot(emaLong, color=blue)
im new to this, please advise what am i doing wrong?
TRADINGview pinescript strategy backtest EMAs crossovers of the 13-day and 48.5-day averages
i get this error
Compilation error. Line 11: no viable alternative at input 'if'. Try to add line '//@version=2' to the top of your script
Upvotes: 1
Views: 347
Reputation: 2171
Few issues here:
ema
function with a float
. It needs to be an int
(whole number).strategy.exit
function has to have a an exit price (or the difference in ticks from purchase price). If you want to close the position in market order, you'll need to use strategy.close()
function.//@version=2
strategy("ema")
// Set the lookback periods for the EMAs
lookbackShort = 13
lookbackLong = 48
// Calculate the short and long EMAs
emaShort = ema(close, lookbackShort)
emaLong = ema(close, lookbackLong)
// Check if the short EMA crosses above the long EMA
if crossover(emaShort, emaLong)
strategy.entry("Long", strategy.long)
// Check if the short EMA crosses below the long EMA
if crossunder(emaShort, emaLong)
strategy.close("Long")
// Plot the EMAs on the chart
plot(emaShort, color=red)
plot(emaLong, color=blue)
Upvotes: 0