Mayank Jain
Mayank Jain

Reputation: 3205

How to correctly reduce security calls from Pine Script?

I am trying to make a dashboard which calculates same condition for multiple symbols enter image description here

I am currently calling security function like this, so I can use it for multiple symbols. However, as you can see it takes 4 security calls.

getCandle(symbol) =>
    _open = security(symbol, "240", open)    
    _close = security(symbol, "240", close)
    _high = security(symbol, "240", high)
    _low = security(symbol, "240", low)
    [_open, _close, _high, _low]

This function is then repeatedly called for different symbols by my script as follows

[_open, _close, _high, _low] = getCandle("OANDA:USDJPY")
// do something with the candle

I have 20+ symbols, so I quickly ran into max security calls/script error.

To optimize this, I tried as shown in the docs

getCandle(symbol) =>
    [_open, _close, _high, _low] = security(symbol, "240", [open, close, high, low])

The moment I make this change, I get huge number of errors on the console which read something like

line 85: Cannot call 'anonym_function_10' with arguments (fun_arg__<anonym_function_7_arg0_type>, fun_arg__<anonym_function_7_arg1_type>, fun_arg__<anonym_function_7_arg2_type>, fun_arg__<anonym_function_7_arg3_type>, input integer, input integer);
line 85: Cannot call 'anonym_function_11' with arguments (fun_arg__<anonym_function_7_arg0_type>, fun_arg__<anonym_function_7_arg1_type>, fun_arg__<anonym_function_7_arg2_type>, fun_arg__<anonym_function_7_arg3_type>, input integer, input integer);
line 85: Undeclared identifier '#var_24';

Can someone please help to understand what is incorrect in the optimized code?

Thanks a bunch!

Upvotes: 0

Views: 1437

Answers (1)

Andr&#233;
Andr&#233;

Reputation: 470

hm, works for me: (you don't even need the getCandle function anymore at this point)

// © marketscripters

//@version=4
study("My Script")

[_open, _close, _high, _low] = security("GBPUSD", "240", [open, close, high, low])
[_o2, _c2, _h2, _l2] = security("EURUSD", "240", [open, close, high, low])

plot(_low)
plot(_high)
plot(_l2, color=color.green)
plot(_h2, color=color.green)

enter image description here

Upvotes: 1

Related Questions