abolvardim
abolvardim

Reputation: 3

Cannot use a mutable variable as an argument of the request.security function

I've written a code in order to find a specific situation as shown below; (if a crossover/crossunder of 2 situations were true in 15mintues candles, then find candles with the same situation in 5 minutes.

here's the code:

//@version=5
indicator(title=" TRY ___???", overlay=true)

//StochRSI

smoothK = input.int(3, "K", minval=1)
smoothD = input.int(3, "D", minval=1)
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)
src = input(close, title="RSI Source")
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
//plot(k, "K", color=#2962FF)
//plot(d, "D", color=#FF6D00)
//h0 = hline(80, "Upper Band", color=#787B86)
//h1 = hline(20, "Lower Band", color=#787B86)
//fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")

//FISHER

len = input.int(9, minval=1, title="Length")
high_ = ta.highest(hl2, len)
low_ = ta.lowest(hl2, len)
round_(val) => val > .99 ? .999 : val < -.99 ? -.999 : val
value = 0.0
value := round_(.66 * ((hl2 - low_) / (high_ - low_) - .5) + .67 * nz(value[1]))
fish1 = 0.0
fish1 := .5 * math.log((1 + value) / (1 - value)) + .5 * nz(fish1[1])
fish2 = fish1[1]
//hline(1.5, "1.5", color=#E91E63)
//hline(0.75,"0.75", color=#787B86)
//hline(0, "0", color=#E91E63)
//hline(-0.75, "-0.75", color=#787B86)
//hline(-1.5, "-1.5", color=#E91E63)
//plot(fish1, color=#2962FF, title="Fisher")
//plot(fish2, color=#FF6D00, title="Trigger")


//BUY
[fish1_15,fish2_15] = request.security('','15',[fish1,fish2])
[k_15,d_15] = request.security('','15',[k,d])

[fish1_5,fish2_5] = request.security('','5',[fish1,fish2])
[k_5,d_5] = request.security('','5',[k,d])

buy_signal = if ta.crossover(fish1_15,fish2_15) and ta.crossover(k_15,d_15)
    ta.crossover(fish1_5,fish2_5) and ta.crossover(k_5,d_5)
else
    false
plotshape(buy_signal, style= shape.labelup, location= location.belowbar ,color= color.green,  textcolor= color.white, text = "buy" )

//SELL

sell_signal = if ta.crossunder(fish1_15,fish2_15) and ta.crossunder(k_15,d_15)
    ta.crossunder(fish1_5,fish2_5) and ta.crossunder(k_5,d_5)
else
    false
plotshape(sell_signal, style= shape.labeldown, location= location.abovebar ,color= color.red,  textcolor= color.white, text = "sell" )

and here's the error: //line 41: Cannot use a mutable variable as an argument of the request.security function.

I would like to know if anyone can help me with this.

Upvotes: 0

Views: 1037

Answers (1)

Starr Lucky
Starr Lucky

Reputation: 1961

Those lines

value = 0.0
value := round_(.66 * ((hl2 - low_) / (high_ - low_) - .5) + .67 * nz(value[1]))
fish1 = 0.0
fish1 := .5 * math.log((1 + value) / (1 - value)) + .5 * nz(fish1[1])
fish2 = fish1[1]

are logically incorrect. Probably you're meant to code it like this:


v1 = round_(.66 * ((hl2 - low_) / (high_ - low_) - .5) + .67) 
value = v1 * nz(v1[1])
f1 = .5 * math.log((1 + value) / (1 - value)) + .5
fish1 = f1 * nz(f1[1])
fish2 = fish1[1]

As for the error, variables which have been first declared, then reassigned using :=, are called mutable variables. Mutable variable may be changed inside any scope of the script. The security function creates another chart scope, 'sucking' inside all of the declared variables from the script. It cannot 'suck' mutable variables, because they values can be changed somewhere else.

Upvotes: 1

Related Questions