Search code examples
compiler-errorspine-scriptpine-script-v5tradingview-api

Pine script MVWAP strategy error: "cannot compile script"


Could someone kindly tell me where the error is in the following Pine script code?

MVWAP Strategy: Buy when the closing price is above MVWAP and sell when the closing price is below.

Thank you.

//@version=5
indicator("MVWAP Buy/Sell Strategy", shorttitle="MVWAP Strategy", overlay=true)

length = input(14, title="VWAP Length")

// Calculate VWAP
vwapValue = wma(close, length)

// Calculate MVWAP
mvwapValue = sma(vwapValue, length)

// Plot MVWAP on the chart
plot(mvwapValue, color=color.blue, title="MVWAP")

// Buy condition: Close is above MVWAP
buyCondition = close > mvwapValue

// Sell condition: Close is below MVWAP
sellCondition = close < mvwapValue

// Plot buy and sell signals on the chart
plotshape(series=buyCondition, title="Buy Signal", color=color.green, style=shape.labelup, text="Buy", location=location.belowbar)
plotshape(series=sellCondition, title="Sell Signal", color=color.red, style=shape.labeldown, text="Sell", location=location.abovebar)

// Strategy
strategy.entry("Buy", strategy.long, when = buyCondition)
strategy.close("Buy", when = sellCondition)


Solution

  • //@version=5
    strategy("MVWAP Buy/Sell Strategy", shorttitle="MVWAP Strategy", overlay=true)
    length = input(14, title="VWAP Length")
    vwapValue = ta.wma(close, length)
    mvwapValue = ta.sma(vwapValue, length)
    plot(mvwapValue, color=color.blue, title="MVWAP")
    
    buyCondition = close > mvwapValue
    sellCondition = close < mvwapValue
    plotshape(series=buyCondition, title="Buy Signal", color=color.green, style=shape.labelup, text="Buy", location=location.belowbar)
    plotshape(series=sellCondition, title="Sell Signal", color=color.red, style=shape.labeldown, text="Sell", location=location.abovebar)
    
    strategy.entry("Buy", strategy.long, when = buyCondition)
    strategy.close("Buy", when = sellCondition)
    

    The script you have provided contain several issues:

    1. The type of script is indicator(), if you want to use the strategy functions and execute trades, you should use the strategy() type of script instead.

    2. The declared version in the first line is version=5, however the built-in function calls do not include their namespaces (ta.wma() instead of just wma()), which is a requirement in v5.