【问题标题】:Tradingview Cannot use a mutable variable as an argument of the request.security functionTradingview 不能使用可变变量作为 request.security 函数的参数
【发布时间】:2022-10-02 13:33:32
【问题描述】:

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

我得到的错误是\'不能使用可变变量\'。从我到目前为止搜索的内容来看,我需要创建一个可以调用的函数?

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

//@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 变量计算到函数中:ht1minute = request.security(syminfo.tickerid, \'1\', calculate_ht())

标签: pine-script


【解决方案1】:

这是工作代码,将 ht 包装在一个函数中。

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

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
  • 如果我想在 Excel 基础上指示半趋势指标买入/卖出,这可能吗?
【解决方案2】:

找了好久的tis,非常感谢。

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    • 1970-01-01
    • 2017-01-20
    • 2021-06-08
    • 1970-01-01
    • 2017-05-02
    相关资源
    最近更新 更多