如何绘制水平线

发布于 2025-01-24 12:32:13 字数 1271 浏览 2 评论 0原文

我正在尝试绘制水平线为-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)

I am trying to draw horizontal lines where it's -8%, -15%, -21%, and -35% of the previous high.

I managed to get the highest plot, but I can't seem to get the horizontal lines drawn with the new complexity of pinescript-v5

Instead of V5, I've changed it to V3. I roughly got the idea but I would like to base the highest as float instead of a series

This is my current script:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2025-01-31 12:32:13

问题仅仅是Hline功能的性质。它不能从一系列数据中获取。第二个问题是,您不能以这种方式将系列转换为单个数据点,以解决Hline函数的问题。

但是,有一个解决方案,而是使用自定义 s。
请注意,我正在使用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

按照该顺序将所有内容放在一起,而您提出的代码将有效,并将包括您想要的动态水平线!

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

“我的代码的结果”

The issue is simply the nature of the hline function. It cannot draw from a series of data. The second issue is that you cannot convert a series into a single data point in such a way to resolve the issue as far as the hline function goes.

There is however a solution to this, and it's to instead use custom lines.
Note I am using pinescript v5 because I am more familiar with it.

First, we draw the fill color, since this function is able to use a series of data.

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

This will draw the fill colors for you but will avoid drawing the series because of the display=display.none parameter. Now the more complex part; drawing the horizontal lines between them.

To do this, we start by creating empty line variables, importantly using the var keyword before the line keyword.

//horizontal lines

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

Without the var keyword, every update of the chart data will mess with our line variables in ways we don't want.

Next, we check for the specific conditions in which we want to update the line variables with the appropriate position data, using if statements.

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)

Lastly, we delete the lines every time a bar closes:

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

// end of script here

Put all that together in that order and the code you presented will work and will include the dynamic horizontal lines you wanted!

Note, the reason we use the series to draw the fill colors instead of the dynamic horizontal lines is because of similar technical reasons to your original issue; the fill function cannot use line variables as input.

result of my code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文