如何有条件地删除 Pine 脚本中的行
How to conditionally delete line in Pine Script
我正在尝试创建一个 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)
查看代码中的注释。使线条更粗以便更容易查看,并在脚本末尾添加了调试图。
基本思想是在初始化 l
变量时使用非常方便的 var
关键字传播先前创建的行的行 ID。这样,在创建新行之前,我们获取用于创建上一行的 y2
,因此如果它的 y2
与我们将要创建的行匹配(因此是从同一个峰)。
crossunder 峰值检测使用 Pine 内置而不是 for
循环。这样代码会 运行 更快。
//@version=4
study(title='MACD trend2')
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
xDn = crossunder(macd, signal)
// Get macd at at highest xDn in last numBarsBack bars. If no Xdn found, set value to -10e10.
highestXDnMacd = highest(xDn ? macd : -10e10, numBarsBack)
// Get offset to that point.
highestXDnOffset = - highestbars(xDn ? macd : -10e10, numBarsBack)
// Detect if previous xDn meets all criteria.
lastXDnWasHigher = xDn and macd < highestXDnMacd
// Make l persistent, so that it always contains the line id of the last line created.
var line l = na
if lastXDnWasHigher
// Retrieve y2 used to draw previous line.
if line.get_y2(l) == highestXDnMacd
// Last line drawn used same y2 as the one we are about to use; delete it.
// No more than one line back can have same peak since previous ones have already been deleted.
line.delete(l)
// The line id we assign to l here will persist through future bars,
// which is what will allow us to delete the corresponding line using the line.delete() above, if needed.
l := line.new(bar_index[1], macd[1], bar_index - highestXDnOffset, macd[highestXDnOffset], width=3, color=color.black)
plot(title='MACD', series=macd,transp=0,linewidth=2, color=color.yellow)
plot(title='SIGNAL', series=signal,transp=0,linewidth=2, color=color.red)
// Debugging.
plot(highestXDnMacd != -10e10 ? highestXDnMacd : na, "highestXDnMacd", color.silver, 2, plot.style_circles)
plotchar(highestXDnOffset, "highestXDnOffset", "", location.top) // For Data Window display.
bgcolor(lastXDnWasHigher ? color.green : xDn ? color.silver : na, 60)
我正在尝试创建一个 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)
查看代码中的注释。使线条更粗以便更容易查看,并在脚本末尾添加了调试图。
基本思想是在初始化 l
变量时使用非常方便的 var
关键字传播先前创建的行的行 ID。这样,在创建新行之前,我们获取用于创建上一行的 y2
,因此如果它的 y2
与我们将要创建的行匹配(因此是从同一个峰)。
crossunder 峰值检测使用 Pine 内置而不是 for
循环。这样代码会 运行 更快。
//@version=4
study(title='MACD trend2')
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
xDn = crossunder(macd, signal)
// Get macd at at highest xDn in last numBarsBack bars. If no Xdn found, set value to -10e10.
highestXDnMacd = highest(xDn ? macd : -10e10, numBarsBack)
// Get offset to that point.
highestXDnOffset = - highestbars(xDn ? macd : -10e10, numBarsBack)
// Detect if previous xDn meets all criteria.
lastXDnWasHigher = xDn and macd < highestXDnMacd
// Make l persistent, so that it always contains the line id of the last line created.
var line l = na
if lastXDnWasHigher
// Retrieve y2 used to draw previous line.
if line.get_y2(l) == highestXDnMacd
// Last line drawn used same y2 as the one we are about to use; delete it.
// No more than one line back can have same peak since previous ones have already been deleted.
line.delete(l)
// The line id we assign to l here will persist through future bars,
// which is what will allow us to delete the corresponding line using the line.delete() above, if needed.
l := line.new(bar_index[1], macd[1], bar_index - highestXDnOffset, macd[highestXDnOffset], width=3, color=color.black)
plot(title='MACD', series=macd,transp=0,linewidth=2, color=color.yellow)
plot(title='SIGNAL', series=signal,transp=0,linewidth=2, color=color.red)
// Debugging.
plot(highestXDnMacd != -10e10 ? highestXDnMacd : na, "highestXDnMacd", color.silver, 2, plot.style_circles)
plotchar(highestXDnOffset, "highestXDnOffset", "", location.top) // For Data Window display.
bgcolor(lastXDnWasHigher ? color.green : xDn ? color.silver : na, 60)