【发布时间】:2020-01-24 18:36:58
【问题描述】:
我正在尝试创建一个 TradingView 研究,该研究从当前柱上的交叉线到前一个柱上的交叉线绘制一条线,其中前一个柱小于设置的最大柱数。
我只想绘制具有负斜率的线(即之前的交叉点发生在更高的值),我也不想要多条具有相同起点的线(没有重叠的线)。
我能够正确绘制线条,但我不知道如何在它们重叠时删除线条(具有相同的起点)。
当绘制一条与旧线重叠的新线时,如何获取对旧线的引用以便删除它?
在 pine 脚本中似乎不可能:
- 迭代行序列中的先前值以检查它们的 x,y 值
- 通过 bar_index 之类的索引访问行系列
- 访问前一行值而不创建新行
//@version=4
study(title='MACD trend')
src = input(close)
fast = input(12)
slow = input(26)
smooth = input(9)
numBarsBack = input(50)
fast_ma = wma(src, fast)
slow_ma = wma(src, slow)
macd = fast_ma-slow_ma
signal = wma(macd, smooth)
hist = macd - signal
if (crossunder(macd, signal))
// cross under happened on previous bar
for i = 1 to numBarsBack
// inspect previous bars up to 'numBarsBack'
if (crossunder(macd,signal)[i])
if (macd - macd[i] < 0)
// located a previous cross under with a higher macd value
l = line.new(bar_index[1], macd[1], bar_index[i+1], macd[i+1], width=1, color=color.red)
// drew line from previous cross under to current cross under,
// offset x's by 1 bar since crossunder returns true based on previous bar's cross under
for k = 1 to i
// inspect previous bars up to the starting point of drawn line
if (crossunder(macd, signal)[k] and macd > macd[k])
// if the previous cross under value is less than the current one
line.delete(l[1])
// not sure what the 1 here indexes???
plot(title='MACD', series=macd,transp=0,linewidth=2, color=color.yellow)
plot(title='SIGNAL', series=signal,transp=0,linewidth=2, color=color.red)
【问题讨论】:
标签: pine-script