The `when` parameter will be deprecated in future Pine versions. How to fix it?

Just recently you might start to see the following warnings in Tradingview :

line 12: The `when` parameter will be deprecated in future Pine versions. We recommend using `if` or `switch` conditional blocks instead

This is happening when you’re using “when” parameter in any strategy.* functions. For example, following code will produce 2 warnings for every “strategy.entry” call:

//@version=5
strategy("MACD Strategy", overlay=true)

fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)

MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD

strategy.entry("MacdLE", strategy.long,  when = ta.crossover(delta, 0))
strategy.entry("MacdSE", strategy.short, when = ta.crossunder(delta, 0))

How to Fix?

Solution for this error you can see in the warning itself. You have to replace your when parameter with if or switch operator. You can do this the following way with if statement:

//@version=5
strategy("MACD Strategy", overlay=true)

fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)

MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD

if (ta.crossover(delta, 0))
    strategy.entry("MacdLE", strategy.long)

if (ta.crossunder(delta, 0))
    strategy.entry("MacdSE", strategy.short)

Solution with a switch will be a bit more complicated because we have to create a variable with the type of input first:

//@version=5
strategy("MACD Strategy", overlay=true)

fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)

MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD

long = ta.crossover(delta, 0)
short = ta.crossunder(delta, 0) 

entry_type = long  ? 'long' : 
             short ? 'short' : 
             'none'

switch entry_type
    'long'  => strategy.entry("MacdLE", strategy.long)
    'short' => strategy.entry("MacdSE", strategy.short)

As you can see solution for this warning is very simple so try to start using it right away, because in the next version of Pine Script it might be an error.


Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png

2 thoughts on “The `when` parameter will be deprecated in future Pine versions. How to fix it?”

  1. I have:

    strategy.close(‘Short’, alert_message=’Close Short’)

    without a “when”, but I still get this warning.

    1. Yep, I saw it, I believe it’s a TV bug.
      Try to add the ‘immediately = false’ parameter, for a weird reason it solves the warning.

Leave a Comment

Your email address will not be published. Required fields are marked *