使用 'request.security' 函数绘图时的多时间帧问题

MultiTimeFrame problem on ploting with 'request.security' function

我的脚本有问题。 我正在尝试绘制 'total_score' 变量的值,但我希望它来自特定的时间范围。问题是,当我使用 'request.security' 函数绘图时,会弹出一条消息:“不能使用可变变量作为 'request.security' 函数的参数”。那是因为 'total_score' 是一个总是被更新的变量。

为了进行绘图,我尝试创建一个函数来计算这个变量,并将其放入一个新变量 'test_tf_4H'。然后,我尝试绘制此 'test_tf_4H' 变量而不是绘制 request.security 函数,但这都不起作用。同样的消息仍然出现:“不能使用可变变量作为 'request.security' 函数的参数”。

谁能告诉我我做错了什么?请。

// == 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))

理想情况下,您需要确保您正在执行的操作在安全调用的上下文中正确完成。您很可能正在计算仍在传递给安全调用的全局上下文中的某些内容,但这应该在安全调用的上下文中完成。

以这个kijun计算为例。这确保了 kijun 被正确计算,就好像它是在 4H 图表上完成的一样,使用 4H 图表 high/low 值使用 26 个 4H 周期。这将为您提供与在图表时间范围为 4 小时时在全球范围内进行计算相同的结果。

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)

可能需要将您的整个计算包装到传递给安全调用的函数中,以确保 total_score 的计算中引用的所有内容都得到正确计算。