cdestatte0
cdestatte0

Reputation: 13

Can not use a mutable variable as an argument of the security function?

How would I code this to get around the error "Cannot use a mutable variable as an argument of the security function." Any help would be great. Thanks in advance

res4 = input(title="time frame", type=input.resolution, defval="240")

len = input(14)
th = input(20)

TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0



SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange


SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus



SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, len)


//security function
DIPlus4H = security("USDCAD", res4, DIPlus)
DIMinus4H = security("USDCAD", res4, DIMinus)

Upvotes: 1

Views: 2462

Answers (1)

vitruvius
vitruvius

Reputation: 21209

So, the error message is clear. You cannot use mutable objects with the security function.

The workaround is, move all your calculations related to that specific variable that you want to use in your security() call into a function and then cal that function in security().

//@version=4
study("My Script")
res4 = input(title="time frame", type=input.resolution, defval="240")

len = input(14)
th = input(20)

get_DIPlus() =>
    TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
    DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
    DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0
    SmoothedTrueRange = 0.0
    SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
    SmoothedDirectionalMovementPlus = 0.0
    SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
    DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100

//security function
DIPlus4H = security("USDCAD", res4, get_DIPlus())
plot(DIPlus4H)

Upvotes: 3

Related Questions