繁体   English   中英

可变变量 + 安全性 function - tradingview 松树错误

[英]Mutable variable + security function - tradingview pine error

我收到错误:不能使用可变变量作为安全 function 的参数

这是将 3 条线显示为阻力线、中间线和支撑线的代码。

//@version=4

srch = input(high, "Highs Source")
srcl = input(low, "Lows Source")
mn = input(21, "Short Range", minval=1)
F = input(5, "Long Range Factor", minval=2)
mx = min(mn * F, 300) // necessary to avoid PineScript's hardcoded iteration-limit on looped code
mn := floor(mx / F)
of = input(0, "Offset", minval=0)
mx := mx + of
mn := mn + of
nr = input(2, "Noise Reduction", minval=0)
sm = nr + 1
exp = input(false, "Use logarithmic scale")
sr = input(true, "Show Resistance (Red)")
ss = input(true, "Show Support (Green)")
sa = input(true, "Show Average (Yellow)")
rl = input(true, "Show Reference Lines")
cr = input(false, "Show Criss-cross")
tl = input(false, "Show Trace Lines")

SRCH = ema(srch, sm)
SRCL = ema(srcl, sm)

gap = 2

hhh = 0.0
lll = 1000000000000.0

mx4 = 100

h2 = hhh
th2 = 0
l2 = lll
tl2 = 0
h1 = hhh
th1 = 0
l1 = lll
tl1 = 0
for i = of to mx
    if i > mn - gap // find closest high and low
        h2 := max(h2, SRCH[i])
        if h2 == SRCH[i]
            th2 := i
        l2 := min(l2, SRCL[i])
        if l2 == SRCL[i]
            tl2 := i
    
    if i < mn + gap // find farthest high and low
        h1 := max(h1, SRCH[i])
        if h1 == SRCH[i]
            th1 := i
        l1 := min(l1, SRCL[i])
        if l1 == SRCL[i]
            tl1 := i

a1 = avg(h1, l1)
ta1 = round(avg(th1, tl1))
a2 = avg(h2, l2)
ta2 = round(avg(th2, tl2))

PLT(P1, P2, T1, T2) =>
    if exp
        t = T1 - T2
        slope = (log10(P1) - log10(P2)) / (0 - t)
        y = slope * T1 + log10(P1)
        pow(10, y)
    else
        ((P2 - P1) / (T1 - T2)) * (T2) + P2

PLC = (barstate.islast or tl) and of == 0

Resistance_line_red = PLC ? PLT(h1, h2, th1, th2) : na
Support_line_green = PLC ? PLT(l1, l2, tl1, tl2) : na
Average_line_orange = PLC ? PLT(a1, a2, ta1, ta2) : na

sty =  tl ? plot.style_stepline : plot.style_circles
lin = tl ? 1 : 2 

plot(Resistance_line_red, "Resistance point", color=#ff0088, transp=0, style=sty, linewidth=lin)
plot(Average_line_orange, "Avg point", color=#000000, transp=0, style=sty, linewidth=lin)
plot(Support_line_green, "Support point", color=#00ff88, transp=0, style=sty, linewidth=lin)

L(T1, H1, T2, H2, CLR, W, X) =>
    line.new(bar_index - max(T1, 0), H1, bar_index - max(T2, 0), H2, color=CLR, width=W, extend=X ? extend.both : extend.none)

//RED
L1b = L(th1, h1, th2, h2, #ff0088, 1, true), line.delete(L1b[1])
L1a = L(th1, h1, th2, h2, #ff0088, 3, false), line.delete(L1a[1])

//GREEN
L2b = L(tl1, l1, tl2, l2, #00ff88, 1, true), line.delete(L2b[1])
L2a = L(tl1, l1, tl2, l2, #00ff88, 3, false), line.delete(L2a[1])

//ORANGE
L3b = L(ta1, a1, ta2, a2, #ff8800, 1, true), line.delete(L3b[1])
L3a = L(ta1, a1, ta2, a2, #ff8800, 3, false), line.delete(L3a[1])

当我写这个时我得到了错误

red_W =  security(syminfo.tickerid, "1W", Resistance_line_red, barmerge.gaps_off, barmerge.lookahead_on)

green_W =  security(syminfo.tickerid, "1W", Support_line_green, barmerge.gaps_off, barmerge.lookahead_on)

orange_W =  security(syminfo.tickerid, "1W", Average_line_orange, barmerge.gaps_off, barmerge.lookahead_on)

我已经阅读了许多类似的问题,以了解我可能需要将它们全部放入 function,因此它可能有效,但我不知道如何应用于此特定代码。

请帮忙,我想让它们在较低的时间范围图表中使用,如下所示:

if (close < red_W) 
   //
if (close < green_W) 
   //
if (close < orange_W) 
   //

我试过你的代码。
它似乎没有抛出任何错误。

//@version=4
study("test", "test", true)

srch = input(high, "Highs Source")
srcl = input(low, "Lows Source")
mn = input(21, "Short Range", minval=1)
F = input(5, "Long Range Factor", minval=2)
mx = min(mn * F, 300) // necessary to avoid PineScript's hardcoded iteration-limit on looped code
mn := floor(mx / F)
of = input(0, "Offset", minval=0)
mx := mx + of
mn := mn + of
nr = input(2, "Noise Reduction", minval=0)
sm = nr + 1
exp = input(false, "Use logarithmic scale")
sr = input(true, "Show Resistance (Red)")
ss = input(true, "Show Support (Green)")
sa = input(true, "Show Average (Yellow)")
rl = input(true, "Show Reference Lines")
cr = input(false, "Show Criss-cross")
tl = input(false, "Show Trace Lines")



SRCH = ema(srch, sm)
SRCL = ema(srcl, sm)

gap = 2

hhh = 0.0
lll = 1000000000000.0

mx4 = 100

h2 = hhh
th2 = 0
l2 = lll
tl2 = 0
h1 = hhh
th1 = 0
l1 = lll
tl1 = 0
for i = of to mx
    if i > mn - gap // find closest high and low
        h2 := max(h2, SRCH[i])
        if h2 == SRCH[i]
            th2 := i
        l2 := min(l2, SRCL[i])
        if l2 == SRCL[i]
            tl2 := i
    
    if i < mn + gap // find farthest high and low
        h1 := max(h1, SRCH[i])
        if h1 == SRCH[i]
            th1 := i
        l1 := min(l1, SRCL[i])
        if l1 == SRCL[i]
            tl1 := i

a1 = avg(h1, l1)
ta1 = round(avg(th1, tl1))
a2 = avg(h2, l2)
ta2 = round(avg(th2, tl2))

PLT(P1, P2, T1, T2) =>
    if exp
        t = T1 - T2
        slope = (log10(P1) - log10(P2)) / (0 - t)
        y = slope * T1 + log10(P1)
        pow(10, y)
    else
        ((P2 - P1) / (T1 - T2)) * (T2) + P2

PLC = (barstate.islast or tl) and of == 0

Resistance_line_red = PLC ? PLT(h1, h2, th1, th2) : na
Support_line_green = PLC ? PLT(l1, l2, tl1, tl2) : na
Average_line_orange = PLC ? PLT(a1, a2, ta1, ta2) : na

red_W =  security(syminfo.tickerid, "1W", Resistance_line_red, barmerge.gaps_off, barmerge.lookahead_on)
green_W =  security(syminfo.tickerid, "1W", Support_line_green, barmerge.gaps_off, barmerge.lookahead_on)
orange_W =  security(syminfo.tickerid, "1W", Average_line_orange, barmerge.gaps_off, barmerge.lookahead_on)

sty =  tl ? plot.style_stepline : plot.style_circles
lin = tl ? 1 : 2 

plot(Resistance_line_red, "Resistance point", color=#ff0088, transp=0, style=sty, linewidth=lin)
plot(Average_line_orange, "Avg point", color=#000000, transp=0, style=sty, linewidth=lin)
plot(Support_line_green, "Support point", color=#00ff88, transp=0, style=sty, linewidth=lin)

L(T1, H1, T2, H2, CLR, W, X) =>
    line.new(bar_index - max(T1, 0), H1, bar_index - max(T2, 0), H2, color=CLR, width=W, extend=X ? extend.both : extend.none)

//RED
L1b = L(th1, h1, th2, h2, #ff0088, 1, true), line.delete(L1b[1])
L1a = L(th1, h1, th2, h2, #ff0088, 3, false), line.delete(L1a[1])

//GREEN
L2b = L(tl1, l1, tl2, l2, #00ff88, 1, true), line.delete(L2b[1])
L2a = L(tl1, l1, tl2, l2, #00ff88, 3, false), line.delete(L2a[1])

//ORANGE
L3b = L(ta1, a1, ta2, a2, #ff8800, 1, true), line.delete(L3b[1])
L3a = L(ta1, a1, ta2, a2, #ff8800, 3, false), line.delete(L3a[1])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM