The error you’re seeing comes from the way Pine expects your strategy() call to be wrapped when you split it over multiple lines (and the inline comment inside the parentheses doesn’t help). You have two options:
- Put everything on one line
//@version=6
strategy("Grid Strategy with MA Filter", overlay=true, margin_long=100, margin_short=100, default_qty_type=strategy.fixed, default_qty_value=1, pyramiding=10)
- Split into multiple lines – but be sure each line ends with a comma and don’t put comments inside the parentheses
//@version=6
strategy(
"Grid Strategy with MA Filter",
overlay=true,
margin_long=100,
margin_short=100,
default_qty_type=strategy.fixed,
default_qty_value=1,
pyramiding=10
)
Below is the full corrected script using option 2. I’ve pulled the comment out of the parentheses and ensured each parameter line ends with a comma (except the last):
//@version=6
strategy(
"Grid Strategy with MA Filter",
overlay=true,
margin_long=100,
margin_short=100,
default_qty_type=strategy.fixed,
default_qty_value=1,
pyramiding=10
)
// ─── Inputs ───────────────────────────────────────────────────────────────────────
maType = input.string("SMA", "MA Type", options=["SMA","EMA","WMA","VWMA"])
maLen = input.int(50, "MA Length", minval=1)
gridPct = input.float(1.0, "Grid Size (%)", minval=0.1, step=0.1)
levels = input.int(3, "Number of Grid Levels", minval=1)
profitPct = input.float(1.0, "Take-Profit per Level (%)", minval=0.1, step=0.1)
showMA = input.bool(true, "Show MA on Chart?")
// ─── Calculate MA ────────────────────────────────────────────────────────────────
float ma = switch maType
"SMA" => ta.sma(close, maLen)
"EMA" => ta.ema(close, maLen)
"WMA" => ta.wma(close, maLen)
=> ta.vwma(close, maLen)
plot(showMA ? ma : na, color=color.orange, title="MA")
// ─── Grid Orders ─────────────────────────────────────────────────────────────────
inUptrend = close > ma
inDowntrend = close < ma
for i = 1 to levels
// Buy grid levels (only in uptrend)
levelBuy = close * (1 - i * gridPct/100)
tpBuy = levelBuy * (1 + profitPct/100)
if inUptrend
strategy.entry("Long_"+str.tostring(i), strategy.long, limit=levelBuy)
strategy.exit("TP_Long_"+str.tostring(i), from_entry="Long_"+str.tostring(i), limit=tpBuy)
// Sell grid levels (only in downtrend)
levelSell = close * (1 + i * gridPct/100)
tpSell = levelSell * (1 - profitPct/100)
if inDowntrend
strategy.entry("Short_"+str.tostring(i), strategy.short, limit=levelSell)
strategy.exit("TP_Short_"+str.tostring(i), from_entry="Short_"+str.tostring(i), limit=tpSell)