【发布时间】: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