如何绘制水平线

How to draw horizontal lines

我正在尝试在前一个高点的 -8%、-15%、-21% 和 -35% 处绘制水平线。

我设法得到了最高的情节,但我似乎无法用pinescript-v5的新复杂度绘制水平线

我已将其从 V5 更改为 V3。我大致明白了,但我想将最高点作为浮点数而不是系列

这是我当前的脚本:

//@version=3
study(title="The Adam Khoo Magic", overlay=true)

//Input options
highlength = input(title="High Length", defval=20, type=integer)

//calculate values
highhighs = highest(high, length=highlength)

minuseight = highhighs*0.92
minusfifteen = highhighs*0.85
minustwentyone = highhighs*0.79
minusthirtyfive = highhighs*0.65

p8 = plot(minuseight, title="-8%", color=olive, linewidth=2, style=line)
p15 = plot(minusfifteen, title="-15%", color=purple, linewidth=2, style=line)
p21 = plot(minustwentyone, title="-21%", color=navy, linewidth=2, style=line)
p35 = plot(minusthirtyfive, title="-35%", color=black, linewidth=2, style=line)

fill(p8, p15, color=red)
fill(p15, p21, color=blue)
fill(p21, p35, color=green)

//plot values on the chart
plot(series=highhighs, color=green, linewidth=1)
plot(minuseight, title="-8%", color=olive, linewidth=2, style=line)
plot(minusfifteen, title="-15%", color=purple, linewidth=2, style=line)
plot(minustwentyone, title="-21%", color=navy, linewidth=2, style=line)
plot(minusthirtyfive, title="-35%", color=black, linewidth=2, style=line)

问题只是 hline 函数的性质。它不能从一系列数据中得出。第二个问题是,就 hline 函数而言,您无法通过这种方式将系列转换为单个数据点来解决问题。

但是有一个解决方案,那就是使用自定义 lines。 注意我用的是pinescript v5,因为我比较熟悉。

首先,我们绘制填充颜色,因为这个函数可以使用一系列数据。

//@version=5
indicator(title="The Adam Khoo Magic", overlay=true)

//Input options

highlength = input.int(20, "High Length")

//color fill

highhighs = ta.highest(high, highlength)

p8 = plot(highhighs*0.92, display=display.none, editable=false)
p15 = plot(highhighs*0.85, display=display.none, editable=false)
p21 = plot(highhighs*0.79, display=display.none, editable=false)
p35 = plot(highhighs*0.65, display=display.none, editable=false)

fill(p8, p15, color=color.new(color.red, 90))
fill(p15, p21, color=color.new(color.blue, 90))
fill(p21, p35, color=color.new(color.green, 90))

这将为您绘制填充颜色,但由于 display=display.none 参数,将避免绘制系列。现在是更复杂的部分;在它们之间绘制水平线。

为此,我们首先创建空行变量,重要的是在 line 关键字之前使用 var 关键字。

//horizontal lines

var line minuseight = na
var line minusfifteen = na
var line minustwentyone = na
var line minusthirtyfive = na

如果没有 var 关键字,图表数据的每次更新都会以我们不希望的方式扰乱我们的 line 变量。

接下来,我们使用 if 语句检查我们想要用适当的位置数据更新行变量的特定条件。

if not barstate.isconfirmed or (barstate.isrealtime and barstate.islast and not barstate.isconfirmed)
    minuseight := line.new(x1=bar_index[1], y1=highhighs*0.92, x2=bar_index, y2=highhighs*0.92, width=1, extend=extend.both)
    minusfifteen := line.new(x1=bar_index[1], y1=highhighs*0.85, x2=bar_index, y2=highhighs*0.85, width=1, extend=extend.both)
    minustwentyone := line.new(x1=bar_index[1], y1=highhighs*0.79, x2=bar_index, y2=highhighs*0.79, width=1, extend=extend.both)
    minusthirtyfive := line.new(x1=bar_index[1], y1=highhighs*0.65, x2=bar_index, y2=highhighs*0.65, width=1, extend=extend.both)
    
    line.set_color(id=minuseight, color=color.white)
    line.set_style(id=minuseight, style=line.style_solid)
    
    line.set_color(id=minusfifteen, color=color.white)
    line.set_style(id=minusfifteen, style=line.style_solid)
    
    line.set_color(id=minustwentyone, color=color.white)
    line.set_style(id=minustwentyone, style=line.style_solid)
    
    line.set_color(id=minusthirtyfive, color=color.white)
    line.set_style(id=minusthirtyfive, style=line.style_solid)

最后,我们在每次收盘时删除这些行:

if barstate.isconfirmed
    line.delete(id=minuseight)
    line.delete(id=minusfifteen)
    line.delete(id=minustwentyone)
    line.delete(id=minusthirtyfive)

// end of script here

按顺序将所有这些放在一起,您提供的代码将起作用,并将包括您想要的动态水平线!

注意,我们使用系列来绘制填充颜色而不是动态水平线的原因是因为与您的原始问题类似的技术原因;填充函数不能使用行变量作为输入。