繁体   English   中英

Tradingview 不能使用可变变量作为 request.security 的参数 function

[英]Tradingview Cannot use a mutable variable as an argument of the request.security function

我试图在多个时间范围内包含一个 plot,但 Tradingview 一直给我这个ht1minute = request.security(syminfo.tickerid, '1', ht) htplot1minute = plot(ht1minute, title='HalfTrend', linewidth=1, color=color.black, transp=30)

我得到的错误是“无法使用可变变量”。 从我到目前为止搜索的内容来看,我需要创建一个可以调用的 function 吗?

但是,如果是这种情况,我不知道该怎么做,非常感谢您的帮助。

//@version=5
// Copyright (c) 2021-present, Alex Orekhov
indicator('HalfTrend', overlay=true)

amplitude = input(title='Amplitude', defval=2)
channelDeviation = input(title='Channel Deviation', defval=2)
showArrows = input(title='Show Arrows', defval=true)
showChannels = input(title='Show Channels', defval=true)

var int trend = 0
var int nextTrend = 0
var float maxLowPrice = nz(low[1], low)
var float minHighPrice = nz(high[1], high)

var float up = 0.0
var float down = 0.0
float atrHigh = 0.0
float atrLow = 0.0
float arrowUp = na
float arrowDown = na

atr2 = ta.atr(100) / 2
dev = channelDeviation * atr2

highPrice = high[math.abs(ta.highestbars(amplitude))]
lowPrice = low[math.abs(ta.lowestbars(amplitude))]
highma = ta.sma(high, amplitude)
lowma = ta.sma(low, amplitude)

if nextTrend == 1
    maxLowPrice := math.max(lowPrice, maxLowPrice)

    if highma < maxLowPrice and close < nz(low[1], low)
        trend := 1
        nextTrend := 0
        minHighPrice := highPrice
        minHighPrice
else
    minHighPrice := math.min(highPrice, minHighPrice)

    if lowma > minHighPrice and close > nz(high[1], high)
        trend := 0
        nextTrend := 1
        maxLowPrice := lowPrice
        maxLowPrice

if trend == 0
    if not na(trend[1]) and trend[1] != 0
        up := na(down[1]) ? down : down[1]
        arrowUp := up - atr2
        arrowUp
    else
        up := na(up[1]) ? maxLowPrice : math.max(maxLowPrice, up[1])
        up
    atrHigh := up + dev
    atrLow := up - dev
    atrLow
else
    if not na(trend[1]) and trend[1] != 1
        down := na(up[1]) ? up : up[1]
        arrowDown := down + atr2
        arrowDown
    else
        down := na(down[1]) ? minHighPrice : math.min(minHighPrice, down[1])
        down
    atrHigh := down + dev
    atrLow := down - dev
    atrLow

ht = trend == 0 ? up : down

var color buyColor = color.blue
var color sellColor = color.red

htColor = trend == 0 ? buyColor : sellColor
htPlot = plot(ht, title='HalfTrend', linewidth=2, color=htColor)



ht1minute = request.security(syminfo.tickerid, '1', ht)
htPlot1minute = plot(ht1minute, title='HalfTrend', linewidth=1, color=color.black, transp=30)```

这是工作代码,将 ht 包装在 function 中。

//@version=5
// Copyright (c) 2021-present, Alex Orekhov
indicator('HalfTrend', overlay=true)

amplitude = input(title='Amplitude', defval=2)
channelDeviation = input(title='Channel Deviation', defval=2)
showArrows = input(title='Show Arrows', defval=true)
showChannels = input(title='Show Channels', defval=true)

f_ht(_amplitude, _channelDeviation) =>
    var int trend = 0
    var int nextTrend = 0
    var float maxLowPrice = nz(low[1], low)
    var float minHighPrice = nz(high[1], high)
    
    var float up = 0.0
    var float down = 0.0
    float atrHigh = 0.0
    float atrLow = 0.0
    float arrowUp = na
    float arrowDown = na
    
    atr2 = ta.atr(100) / 2
    dev = _channelDeviation * atr2
    
    highPrice = high[math.abs(ta.highestbars(_amplitude))]
    lowPrice = low[math.abs(ta.lowestbars(_amplitude))]
    highma = ta.sma(high, _amplitude)
    lowma = ta.sma(low, _amplitude)
    
    if nextTrend == 1
        maxLowPrice := math.max(lowPrice, maxLowPrice)
    
        if highma < maxLowPrice and close < nz(low[1], low)
            trend := 1
            nextTrend := 0
            minHighPrice := highPrice
            minHighPrice
    else
        minHighPrice := math.min(highPrice, minHighPrice)
    
        if lowma > minHighPrice and close > nz(high[1], high)
            trend := 0
            nextTrend := 1
            maxLowPrice := lowPrice
            maxLowPrice
    
    if trend == 0
        if not na(trend[1]) and trend[1] != 0
            up := na(down[1]) ? down : down[1]
            arrowUp := up - atr2
            arrowUp
        else
            up := na(up[1]) ? maxLowPrice : math.max(maxLowPrice, up[1])
            up
        atrHigh := up + dev
        atrLow := up - dev
        atrLow
    else
        if not na(trend[1]) and trend[1] != 1
            down := na(up[1]) ? up : up[1]
            arrowDown := down + atr2
            arrowDown
        else
            down := na(down[1]) ? minHighPrice : math.min(minHighPrice, down[1])
            down
        atrHigh := down + dev
        atrLow := down - dev
        atrLow
    
    [trend, up, down]

[out_trend, out_up, out_down] = f_ht(_amplitude = amplitude, _channelDeviation = channelDeviation)

ht = out_trend == 0 ? out_up : out_down

var color buyColor = color.blue
var color sellColor = color.red

htColor = out_trend == 0 ? buyColor : sellColor
htPlot = plot(ht, title='HalfTrend', linewidth=2, color=htColor)

ht1minute = request.security(syminfo.tickerid, '1', ht)
htPlot1minute = plot(ht1minute, title='HalfTrend', linewidth=1, color=color.new(color.black, 30))

暂无
暂无

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

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