In this article, I will show you how to fix the “‘expression’ argument of security function should have no side effects” error in PineScript.
Here is an example of the script that triggers that error:
//@version=4
study("My Script", overlay = true)
calc_high(len) =>
h = highest(high, len)
if (barstate.islast)
line.new(bar_index, h, bar_index[10], h)
h
high_htf = security(syminfo.tickerid, '240', calc_high(10))
plot(close)
Error message:
The issue is that when you call a function inside a “security” function you can’t plot trend line or labels in it. So you can’t use label.new, line.new and all related functions.
To solve that just delete all these functions from the functions and plot your stuff outside security call.
For example in my example this is a solution:
//@version=4
study("My Script", overlay = true)
calc_high(len) =>
h = highest(high, len)
h
high_htf = security(syminfo.tickerid, '240', calc_high(10))
if (barstate.islast)
line.new(bar_index, high_htf, bar_index[10], high_htf)
plot(close)
Follow me on TradingView and YouTube.