Edgoro
Compile error · Old code in v6 (v4 and v5 to v6)

Pine Script v6: strategy.entry has no argument when (fix)

Pine Script v6 removed the when parameter from strategy.entry(), strategy.exit() and the other order functions. Wrap the call in an if block instead.

Pine Editor console · line 4
The "strategy.entry" function does not have an argument with the name "when"

Cause

In v5 and earlier, order functions accepted when = condition. v5 deprecated it and v6 removed it from strategy.entry(), strategy.order(), strategy.exit(), strategy.close(), strategy.close_all(), strategy.cancel() and strategy.cancel_all().

Broken code

TradingView reports the error on line 4.

Pine Script v6
//@version=6
strategy("SMA cross", overlay = true)
longSignal = ta.crossover(close, ta.sma(close, 20))
strategy.entry("Long", strategy.long, when = longSignal)

Fixed code

The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.

Pine Script v6
//@version=6
strategy("SMA cross", overlay = true)
longSignal = ta.crossover(close, ta.sma(close, 20))
exitSignal = ta.crossunder(close, ta.sma(close, 20))
if longSignal
    strategy.entry("Long", strategy.long)
if exitSignal
    strategy.close("Long")

Why it works

An if block runs the order call only on bars where the condition is true, which is exactly what when did. Unlike plot(), strategy order functions are allowed inside blocks. The fixed version also adds an exit so the strategy closes its trades.

Exits with stop and target

v5 code often wrote strategy.exit("Exit", "Long", stop = s, limit = t, when = strategy.position_size > 0). In v6:

if strategy.position_size > 0
    strategy.exit("Exit", "Long", stop = low * 0.98, limit = high * 1.04)

See stop loss and take profit in Pine Script for exits based on the entry price.

Questions

Why was when removed from strategy.entry()?

It duplicated what an if statement does. v5 deprecated the parameter and v6 removed it, so order logic is written one way: an if block around the order call.

Does strategy.exit() need an if block in v6?

Only if the exit depends on a condition. A strategy.exit() with stop and limit levels can be called on every bar; it only acts on an open position with the matching entry ID.

The scripts on this page are minimal examples written to show the error and its fix, not trading systems. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Still getting an error?

Paste your script and the message into the free Pine Script error fixer: it explains what went wrong and returns a corrected v6 version. Or describe the indicator in plain English and let Edgoro write the code.