简体   繁体   中英

trading view pine script strategy backtest error

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

Few issues here:

  1. The error message is clear. Add the version number to the top of your script.
  2. You can't call the ema function with a float . It needs to be an int (whole number).
  3. Your 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM