Vendrel
Vendrel

Reputation: 987

Failed to avoid 'mutable variable' error message on request.security(). What did I do wrong?

I tried to implement the example code from this useful answer but the 'mutable variable' error still remained. What did I do wrong?

Below, I wrapped a too long line (BooleanVar =) to avoid overflow, for making it easy-readable.

MyFunction() =>
    BooleanVar = //THE ERROR MESSAGE ADDRESSES THIS LINE
     ta.barssince(request.security(syminfo.tickerid, "5", MutableVar1)) 
     < ta.barssince(request.security(syminfo.tickerid, "5", MutableVar2)) // *

AnotherBooleanVar = MyFunction() and SomethingAlreadyCalculated
plotshape(AnotherBooleanVar ? ThingToPlot : na, text="Buy", location=location.belowbar, style=shape.labelup, size=size.tiny, color=longColorConfirmed, textcolor=color.white)

The strange thing is, there's no error message for the marked line until I include that AnotherBooleanVar into a plotchar() function. If I remove that criterion from plotchar() then suddenly no error for the line above.

Error message is, repeat: Cannot use a mutable variable as an argument of the request.security function.

Upvotes: 0

Views: 200

Answers (1)

beeholder
beeholder

Reputation: 1699

Once again, everything related to the variable that you mutate needs to be wrapped in a function. The mutated variable needs to be created inside this function and mutated inside this function. The goal is to make the variable contained inside the function so that it is possible to calculate it on a different symbol. In your example, you request MutableVar1 from a function, but the variable itself is both created and mutated elsewhere.

Upvotes: 1

Related Questions