clst
clst

Reputation: 121

Use variables in security expression variable (cannot use a mutable variable in security())

Here is my Pinescript code. Problem is, I have the following error : Cannot use a mutable variable as an argument of the security function. Do you have any idea how I could use my boolean variable toz in the security() function ?

TD = 0
TS = 0

if close > close[4]
    TD := TD[1]+1
if close < close[4]
    TS := TS[1]+1

TDUp = TD - valuewhen(TD < TD[1], TD , 1 )
TDDn = TS - valuewhen(TS < TS[1], TS , 1 )
bool toz = (TDDn==9) //or (TDDn==9)
customFunc() => toz

s1  = security('BTCUSD',  'D', customFunc())

Upvotes: 0

Views: 568

Answers (1)

Denis T
Denis T

Reputation: 416

Try to move the whole calculation into the function:

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

customFunc() =>
    TD = 0
    TS = 0

    if close > close[4]
        TD := TD[1]+1
    if close < close[4]
        TS := TS[1]+1

    TDUp = TD - valuewhen(TD < TD[1], TD , 1 )
    TDDn = TS - valuewhen(TS < TS[1], TS , 1 )
    (TDDn==9) //or (TDDn==9)

s1  = security('BTCUSD',  'D', customFunc())

plot(s1 ? 1 : 0)

Upvotes: 3

Related Questions