【问题标题】:How to draw horizontal lines如何画水平线
【发布时间】:2022-07-01 07:28:08
【问题描述】:

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

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

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

这是我当前的脚本:

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

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    问题只是 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
    

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

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

    【讨论】:

    • 这当然显示了如何画一条直线谢谢!我找到了一种解决方法,方法是使用 for...loop maxhigh = for i = 1 to highlength by 1 maxhigh = 0 if highesthights[i] > maxhigh maxhigh = highesthights[i] maxhigh 绘制波浪线
    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2014-05-04
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    相关资源
    最近更新 更多