Josef K
Josef K

Reputation: 1

How to simplify the repeated security()? Is it possible?

I found that the pine script only provides 40 securities in a script. And, I am trying to simplify the repeated "security()" and also the "len(n)= input". Below is just a random code.

//@version=4
study("How to make it simple?", overlay=false)

len1 = input(10)
sma_H1 = security(syminfo.tickerid, "5", sma(high, len1))
sma_L1 = security(syminfo.tickerid, "5", sma(low, len1))

UP1 = sma_H1 and sma_L1 > close
DN1 = sma_H1 and sma_L1 < close

plotshape(UP1, style= shape.xcross, location= location.top, color=color.green)
plotshape(DN1, style= shape.xcross, location= location.top, color=color.red)

I want to add more higher-timeframes with different "len(n)= input". Below as you can see, there are many codes repeated. Is it possible to keep only 2 security() and add more higher-timeframes by using “for loop” or “if statement”?

I've tried many methods but the script says "Cannot use a mutable variable as an argument of the security function" in the script or "Invalid value of the 'resolution' argument(i) in the 'security' function" in the chart.

Any reference would be also very helpful or can anyone make the codes below as simple as possible? Please, someone, help me with this..

//@version=4
study("How to make it simple?", overlay=false)

len1 = input(10)
sma_H1 = security(syminfo.tickerid, "5", sma(high, len1))
sma_L1 = security(syminfo.tickerid, "5", sma(low, len1))
//------------------------------------------------------------------------------
len2 = input(20)
sma_H2 = security(syminfo.tickerid, "15", sma(high, len2))
sma_L2 = security(syminfo.tickerid, "15", sma(low, len2))
//------------------------------------------------------------------------------
len3 = input(30)
sma_H3 = security(syminfo.tickerid, "30", sma(high, len3))
sma_L3 = security(syminfo.tickerid, "30", sma(low, len3))
//------------------------------------------------------------------------------
len4 = input(40)
sma_H4 = security(syminfo.tickerid, "45", sma(high, len4))
sma_L4 = security(syminfo.tickerid, "45", sma(low, len4))

//////////////////////////////////////////////////////////////////////////////////////////////
UP1 = sma_H1 and sma_L1 > close
DN1 = sma_H1 and sma_L1 < close
//------------------------------------------------------------------------------
UP2 = sma_H2 and sma_L2 > close
DN2 = sma_H2 and sma_L2 < close
//------------------------------------------------------------------------------
UP3 = sma_H3 and sma_L3 > close
DN3 = sma_H3 and sma_L3 < close
//------------------------------------------------------------------------------
UP4 = sma_H4 and sma_L4 > close
DN4 = sma_H4 and sma_L4 < close

//////////////////////////////////////////////////////////////////////////////////////////////
UP = UP1 and UP2 and UP3 and UP4
DN = DN1 and DN2 and DN3 and DN4
plotshape(UP, style= shape.xcross, location= location.top, color=color.green)
plotshape(DN, style= shape.xcross, location= location.top, color=color.red)

Upvotes: 0

Views: 648

Answers (1)

e2e4
e2e4

Reputation: 3828

You can create a function that returns a tuple, it will reduce some code.

//@version=4
study("How to make it simple?", overlay=false)

f_security(_tf, _len) => [_high, _low] = security(syminfo.tickerid, _tf, [sma(high, _len), sma(low, _len)])

//------------------------------------------------------------------------------
len1 = input(10)
[sma_H1, sma_L1] = f_security("5", len1)
//------------------------------------------------------------------------------
len2 = input(20)
[sma_H2, sma_L2] = f_security("15", len2)
//------------------------------------------------------------------------------
len3 = input(30)
[sma_H3, sma_L3] = f_security("30", len3)
//------------------------------------------------------------------------------
len4 = input(40)
[sma_H4, sma_L4] = f_security("45", len4)

Upvotes: 2

Related Questions