Home > Enterprise >  MultiTimeFrame problem on ploting with 'request.security' function
MultiTimeFrame problem on ploting with 'request.security' function

Time:11-05

I got a problem with my scritp. I'm trying to plot the value of the 'total_score' variable, but I want it from an specific timeframe. The thing is that when I'm ploting with the 'request.security' function, a message pops up: "Cannot use a mutable variable as an argument of the 'request.security' function". That is because 'total_score' is a variable that is always being updated.

In order to do the ploting, I've tried creating a function that calculates this variable, and put it into a new variable 'test_tf_4H'. Then, I tried to plot this 'test_tf_4H' variable instead of ploting the request.security function, but neither that works. The same message still coming up: "Cannot use a mutable variable as an argument of the 'request.security' function".

Can anyone tell me what I'm I doing wrong here? Please.

// == inputs ==

Tenkan_P = input(9, title="Línea Tenkan-Sen"),
Kijun_P = input(26, title="Línea Kijun-Sen")
LeadingB_P = input(52, title="Línea Leading Span B"),
displacement = input(26, title="Retraso Chikou Span")

// == Vectores Ichimoku ==

Maximos_Tenkan = ta.highest(high, Tenkan_P)
Minimos_Tenkan = ta.lowest(low, Tenkan_P)
Maximos_Kijun = ta.highest(high, Kijun_P)
Minimos_Kijun = ta.lowest(low, Kijun_P)
Maximos_SpanB = ta.highest(high, LeadingB_P)
Minimos_SpanB = ta.lowest(low, LeadingB_P)
Tenkan = (Maximos_Tenkan   Minimos_Tenkan)/2
Kijun = (Maximos_Kijun   Minimos_Kijun) / 2
LeadingSpan_A = (Tenkan   Kijun) / 2
LeadingSpan_B = (Maximos_SpanB   Minimos_SpanB) / 2

resolve(src, default) =>
    if na(src)
        default
    else
        src

//Definición de cruces Gold / Death 
tk_cross_bull = ta.crossover(Tenkan, Kijun)
tk_cross_bear = ta.crossunder(Tenkan, Kijun)

// Momento de cruce TK / KJ
cross_y = (Tenkan[1] * (Kijun - Kijun[1]) - Kijun[1] * (Tenkan - Tenkan[1])) / ((Kijun - Kijun[1]) - (Tenkan - Tenkan[1]))
tk_cross_below_kumo = cross_y <= LeadingSpan_B[27] and cross_y <= LeadingSpan_A[27] and cross_y <= LeadingSpan_B[26] and cross_y <= LeadingSpan_A[26]
tk_cross_above_kumo = cross_y >= LeadingSpan_B[27] and cross_y >= LeadingSpan_A[27] and cross_y >= LeadingSpan_B[26] and cross_y >= LeadingSpan_A[26]
tk_cross_inside_kumo = (not tk_cross_below_kumo) and (not tk_cross_above_kumo)
//Función TK Cross
tk_cross_score = 0.0
tk_cross_score := resolve(tk_cross_score[1], 0)
// Ponderación del cruce TK / KJ
tk_cross_weight = input.float(1.0, title="Tenkan/Kijun Ponderación", step=0.1)
tk_cross_weak_bullish_points = input.float(0.5, title="Ponderación Tenkan/Kijun Bullish débil", step=0.1)
tk_cross_neutral_bullish_points = input.float(1.0, title="Ponderación Tenkan/Kijun Bullish neutral", step=0.1)
tk_cross_strong_bullish_points = input.float(2.0, title="Ponderación Tenkan/Kijun Bullish fuerte", step=0.1)

tk_cross_weak_bearish_points = input.float(-0.5, title="Ponderación Tenkan/Kijun Bearish débil", step=0.1)
tk_cross_neutral_bearish_points = input.float(-1.0, title="Ponderación Tenkan/Kijun Bearish neutral", step=0.1)
tk_cross_strong_bearish_points = input.float(-2.0, title="Ponderación Tenkan/Kijun Bearish fuerte", step=0.1)

// == TK Cross / weak bullish (below kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_below_kumo) ? tk_cross_weak_bullish_points : tk_cross_score

// == TK Cross / neutral bullish (inside kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_inside_kumo) ? tk_cross_neutral_bullish_points : tk_cross_score

// == TK Cross / strong bullish (above kumo) ==
tk_cross_score := (tk_cross_bull and tk_cross_above_kumo) ? tk_cross_strong_bullish_points : tk_cross_score

// == TK Cross / strong bearish (below kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_below_kumo) ? tk_cross_strong_bearish_points : tk_cross_score

// == TK Cross / neutral bearish (inside kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_inside_kumo) ? tk_cross_neutral_bearish_points : tk_cross_score

// == TK Cross / weak bearish (above kumo) ==
tk_cross_score := (tk_cross_bear and tk_cross_above_kumo) ? tk_cross_weak_bearish_points : tk_cross_score

// == Price and Kijun Sen (standard line) Cross ==

//pk_cross_bull = (close[1] < Kijun[1] and close >= Kijun)
//pk_cross_bear = (close[1] > Kijun[1] and close <= Kijun)
pk_cross_bull = ta.crossover(close, Kijun)
pk_cross_bear = ta.crossunder(close, Kijun)

cross_pk_y = (close[1] * (Kijun - Kijun[1]) - Kijun[1] * (close - close[1])) / ((Kijun - Kijun[1]) - (close - close[1]))

pk_cross_below_kumo = cross_pk_y <= LeadingSpan_B[27] and cross_pk_y <= LeadingSpan_A[27] and cross_pk_y <= LeadingSpan_B[26] and cross_pk_y <= LeadingSpan_A[26]
pk_cross_above_kumo = cross_pk_y >= LeadingSpan_B[27] and cross_pk_y >= LeadingSpan_A[27] and cross_pk_y >= LeadingSpan_B[26] and cross_pk_y >= LeadingSpan_A[26]
pk_cross_inside_kumo = (not pk_cross_below_kumo) and (not pk_cross_above_kumo)

pk_cross_score = 0.0
pk_cross_score := resolve(pk_cross_score[1], 0)

pk_cross_weight = input.float(1.0, title="PS Cross Importance Weight", step=0.1)

pk_cross_weak_bullish_points = input.float(0.5, title="Ponderación Precio/Kijun Bullish débil", step=0.1)
pk_cross_neutral_bullish_points = input.float(1.0, title="Ponderación Precio/Kijun Bullish neutral", step=0.1)
pk_cross_strong_bullish_points = input.float(2.0, title="Ponderación Precio/Kijun Bullish fuerte", step=0.1)

pk_cross_weak_bearish_points = input.float(-0.5, title="Ponderación Precio/Kijun Bearish débil", step=0.1)
pk_cross_neutral_bearish_points = input.float(-1.0, title="Ponderación Precio/Kijun Bearish neutral", step=0.1)
pk_cross_strong_bearish_points = input.float(-2.0, title="Ponderación Precio/Kijun Bearish fuerte", step=0.1)

// == PK Cross / weak bullish (below kumo) ==
pk_cross_score := (pk_cross_bull and pk_cross_below_kumo) ? pk_cross_weak_bullish_points : pk_cross_score

// == PK Cross / neutral bullish (inside kumo)
pk_cross_score := (pk_cross_bull and pk_cross_inside_kumo) ? pk_cross_neutral_bullish_points : pk_cross_score

// == PK Cross / strong bullish (above kumo)
pk_cross_score := (pk_cross_bull and pk_cross_above_kumo) ? pk_cross_strong_bullish_points : pk_cross_score

// == PK Cross / strong bearish (below kumo)
pk_cross_score := (pk_cross_bear and pk_cross_below_kumo) ? pk_cross_strong_bearish_points : pk_cross_score

// == PK Cross / neutral bearish (inside kumo)
pk_cross_score := (pk_cross_bear and pk_cross_inside_kumo) ? pk_cross_neutral_bearish_points : pk_cross_score

// == PK Cross / weak bearish (above kumo)
pk_cross_score := (pk_cross_bear and pk_cross_above_kumo) ? pk_cross_weak_bearish_points : pk_cross_score

// == Kumo Breakouts ==

kumo_bull = (ta.crossover(close, LeadingSpan_A[26]) and LeadingSpan_A[26] > LeadingSpan_B[26]) or (ta.crossover(close, LeadingSpan_B[26]) and LeadingSpan_B[26] > LeadingSpan_A[26])
kumo_bear = (ta.crossunder(close, LeadingSpan_B[26]) and LeadingSpan_A[26] > LeadingSpan_B[26]) or (ta.crossunder(close, LeadingSpan_A[26]) and LeadingSpan_B[26] > LeadingSpan_A[26])

kumo_breakout_score = 0.0
kumo_breakout_score := resolve(kumo_breakout_score[1], 0)

kumo_breakout_weight = input.float(1.0, title="Importancia a salida de la nube", step=0.1)

kumo_breakout_bullish_points = input.float(1.0, title="Salida de la nube: Bullish", step=0.1)
kumo_breakout_bearish_points = input.float(-1.0, title="Salida de la nube: Bearish", step=0.1)

price_below_kumo = (close < LeadingSpan_B and close < LeadingSpan_A)
price_above_kumo = (close > LeadingSpan_B and close > LeadingSpan_A)
price_inside_kumo = (not price_below_kumo) and (not price_above_kumo)

kumo_breakout_score := (kumo_bull and price_above_kumo) ? kumo_breakout_bullish_points : kumo_breakout_score
kumo_breakout_score := (kumo_bear and price_below_kumo) ? kumo_breakout_bearish_points : kumo_breakout_score


lead_line_cross_bull = ta.crossover(LeadingSpan_A, LeadingSpan_B)
lead_line_cross_bear = ta.crossunder(LeadingSpan_A, LeadingSpan_B)
price_below_kumo := (close < LeadingSpan_B and close < LeadingSpan_A)
price_above_kumo := (close > LeadingSpan_B and close > LeadingSpan_A)
price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)

span_cross_score = 0.0
span_cross_score := resolve(span_cross_score[1], 0)
span_cross_weight = input.float(1.0, title="Ponderación al cruce de nubes (Span A vs Span B)", step=0.1)

span_cross_weak_bullish_points = input.float(0.5, title="Ponderación de cruce de nubes Bullish débil", step=0.1)
span_cross_neutral_bullish_points = input.float(1.0, title="Ponderación de cruce de nubes Bullish neutral", step=0.1)
span_cross_strong_bullish_points = input.float(2.0, title="Ponderación de cruce de nubes Bullish fuerte", step=0.1)

span_cross_weak_bearish_points = input.float(-0.5, title="Ponderación de cruce de nubes Bearish débil", step=0.1)
span_cross_neutral_bearish_points = input.float(-1.0, title="Ponderación de cruce de nubes Bearish neutral", step=0.1)
span_cross_strong_bearish_points = input.float(-2.0, title="Ponderación de cruce de nubes Bearish fuerte", step=0.1)

// == Senkou Span Cross / weak bullish (price below kumo) ==
span_cross_score := (lead_line_cross_bull and price_below_kumo) ? span_cross_weak_bullish_points : span_cross_score

// == Senkou Span Cross / neutral bullish (price inside kumo) ==
span_cross_score := (lead_line_cross_bull and price_inside_kumo) ? span_cross_neutral_bullish_points : span_cross_score

// == Senkou Span Cross / strong bullish (price above kumo) ==
span_cross_score := (lead_line_cross_bull and price_above_kumo) ? span_cross_strong_bullish_points : span_cross_score

// == Senkou Span Cross / weak bearish (price above kumo) ==
span_cross_score := (lead_line_cross_bear and price_above_kumo) ? span_cross_weak_bearish_points : span_cross_score

// == Senkou Span Cross / neutral bearish (price inside kumo) ==
span_cross_score := (lead_line_cross_bear and price_inside_kumo) ? span_cross_neutral_bearish_points : span_cross_score

// == Senkou Span Cross / strong bearish (price below kumo) ==
span_cross_score := (lead_line_cross_bear and price_below_kumo) ? span_cross_strong_bearish_points : span_cross_score


past_price = close[26]
lag_line_bull_cross = close > close[26]
lag_line_bear_cross = close < close[26]


past_price_below_kumo = (past_price < LeadingSpan_B[26] and past_price < LeadingSpan_A[26])
past_price_above_kumo = (past_price > LeadingSpan_B[26] and past_price > LeadingSpan_A[26])
past_price_inside_kumo = (LeadingSpan_B[26] < past_price and past_price < LeadingSpan_A[26]) and (LeadingSpan_A[26] < past_price and past_price < LeadingSpan_B[26])

lag_line_cross_score = 0.0
lag_line_cross_score := resolve(lag_line_cross_score[1], 0)

lag_line_cross_weight = input.float(1.0, title="Ponderación Chikou Span", step=0.1)

lag_line_cross_weak_bullish_points = input.float(0.5, title="Ponderación cruce Chikou/precio Bullish débil", step=0.1)
lag_line_cross_neutral_bullish_points = input.float(1.0, title="Ponderación cruce Chikou/precio Bullish neutral", step=0.1)
lag_line_cross_strong_bullish_points = input.float(2.0, title="Ponderación cruce Chikou/precio Bullish fuerte", step=0.1)

lag_line_cross_weak_bearish_points = input.float(-0.5, title="Ponderación cruce Chikou/precio Bearish débil", step=0.1)
lag_line_cross_neutral_bearish_points = input.float(-1.0, title="Ponderación cruce Chikou/precio Bearish neutral", step=0.1)
lag_line_cross_strong_bearish_points = input.float(-2.0, title="Ponderación cruce Chikou/precio Bearish fuerte", step=0.1)

// == Chikou Span Cross / weak bullish (price below kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_below_kumo) ? lag_line_cross_weak_bullish_points : lag_line_cross_score

// == Chikou Span Cross / neutral bullish (price inside kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bullish_points : lag_line_cross_score

// == Chikou Span Cross / strong bullish (price above kumo)
lag_line_cross_score := (lag_line_bull_cross and past_price_above_kumo) ? lag_line_cross_strong_bullish_points : lag_line_cross_score

// == Chikou Span Cross / weak bearish (price above kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_above_kumo) ? lag_line_cross_weak_bearish_points : lag_line_cross_score

// == Chikou Span Cross / neutral bearish (price inside kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bearish_points : lag_line_cross_score

// == Chikou Span Cross / strong bearish (price below kumo)
lag_line_cross_score := (lag_line_bear_cross and past_price_below_kumo) ? lag_line_cross_strong_bearish_points : lag_line_cross_score

// == lag line releative to cloud ==


lag_line_location_weight = input.float(1.0, title="Importancia relativa del Chikou Span a la nube", step=0.1)

lag_line_location_above_points = input.float(1.0, title="Ponderación a Chikou Span sobre la nube", step=0.1)
lag_line_location_inside_points = input.float(0, title="Ponderación a Chikou Span al interior de la nube", step=0.1)
lag_line_location_below_points = input.float(-1.0, title="Ponderación a Chikou Span debajo la nube", step=0.1)

lag_line_placement_score = 0.0
lag_line_placement_score := resolve(lag_line_placement_score[1], 0)

lag_line_placement_score := past_price_above_kumo ? lag_line_location_above_points : lag_line_placement_score
lag_line_placement_score := past_price_inside_kumo ? lag_line_location_inside_points : lag_line_placement_score
lag_line_placement_score := past_price_below_kumo ? lag_line_location_below_points : lag_line_placement_score

// == price relative to cloud ==

price_placement_score = 0.0
price_placement_score := resolve(price_placement_score[1], 0)

price_location_weight = input.float(1.0, title="Importancia relativa del precio a la nube", step=0.1)

price_location_above_points = input.float(1.0, title="Ponderación al precio sobre la nube", step=0.1)
price_location_inside_points = input.float(0, title="Ponderación al precio en la nubes", step=0.1)
price_location_below_points = input.float(-1.0, title="Ponderación al precio debajo de la nube", step=0.1)

price_below_kumo := (close < LeadingSpan_B[26] and close < LeadingSpan_A[26])
price_above_kumo := (close > LeadingSpan_B[26] and close > LeadingSpan_A[26])
price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)

price_placement_score := price_above_kumo ? price_location_above_points : price_placement_score
price_placement_score := price_inside_kumo ? price_location_inside_points : price_placement_score
price_placement_score := price_below_kumo ? price_location_below_points : price_placement_score

// == plot score ==

Ichilemoku_funct() =>
    total_score = ( tk_cross_weight * tk_cross_score)   (pk_cross_weight * pk_cross_score)   (kumo_breakout_weight * kumo_breakout_score)    (span_cross_weight * span_cross_score)   (lag_line_cross_weight * lag_line_cross_score)   (price_location_weight * price_placement_score)
    total_score := total_score   (lag_line_location_weight * lag_line_placement_score)   

// Arreglos MTF
test_tf_4H = request.security(syminfo.tickerid, timeframe = '240', expression = Ichilemoku_funct())

plot(test_tf_4H, linewidth=3, title="Total Score", color = color.yellow)
plot(0, title="Base", style=plot.style_stepline, color = color.rgb(223, 223, 223, 20))

CodePudding user response:

Ideally, you need to ensure that the operations you are performing are being done correctly within the context of the security call. Most likely you are calculating something in the global context that is still being passed through to the security call, but that should be done within the context of the security call.

Take this kijun calculation for example. This ensures that the kijun is being calculated correctly as if it was being done on the 4H chart, using 4H chart high/low values using 26 4H periods. This will give you the same result as if you were doing the calculation on the global scope while the chart's timeframe is 4H.

f_kijun(_high, _low) =>
    _kijun = (ta.highest(_high, 26)   ta.lowest(_low, 26)) / 2
    _kijun

mtf_kijun = request.security(syminfo.tickerid, "240", f_kijun(high, low))

plot(mtf_kijun)

It may be necessary to wrap your entire calculation into a function that is passed to the security call to ensure that everything that is referenced in the calculation for total_score is calculated correctly.

  • Related