如何在Pine Script中创建一个矩形,显示前90分钟的范围?

huangapple go评论66阅读模式
英文:

How to create a rectangle in Pine Script showing the range of the first 90 minutes?

问题

I am using a 5-minute chart. I'd like to draw a transparent rectangle from bar no. 19 of the session all the way to the final bar of the session, showing the range of the first 90 minutes (ie, the first 18 bars in a 5-minute chart).

indicator("18-bar range", overlay = true)

var float rangeHigh = 0
var float rangeLow = 0
box myBox = na

newDay = ta.change(time("D"))
barsCount = ta.barssince(newDay)

if (barsCount == 0)
    rangeHigh := high
    rangeLow := low

if (barsCount < 18)
    if (high > rangeHigh)
        rangeHigh := high
    if (low < rangeLow)
        rangeLow := low

if (barsCount >= 18)
    myBox := box.new(left = bar_index + 1, top = rangeHigh, right = bar_index, bottom = rangeLow, border_color = color.new(#cce2cc, 90), bgcolor = color.new(#cce2cc, 50))

如何在Pine Script中创建一个矩形,显示前90分钟的范围?

The problem I have is that the rectangle for some weird reason is starting from bar 28, not bar 19 as intended. What am I doing wrong?

英文:

I am using a 5-minute chart. I'd like to draw a transparent rectangle from bar no. 19 of the session all the way to the final bar of the session, showing the range of the first 90 minutes (ie, the first 18 bars in a 5 minute chart).

//@version=5
indicator(&quot;18-bar range&quot;, overlay = true)

var float rangeHigh = 0
var float rangeLow = 0
box myBox = na

newDay = ta.change(time(&quot;D&quot;))
barsCount = ta.barssince(newDay)

if (barsCount == 0)
    rangeHigh := high
    rangeLow := low

if (barsCount &lt; 18)
    if (high &gt; rangeHigh)
        rangeHigh := high
    if (low &lt; rangeLow)
        rangeLow := low

if (barsCount &gt;= 18)
    myBox := box.new(left = bar_index + 1, top = rangeHigh, right = bar_index, bottom = rangeLow, border_color = color.new(#cce2cc, 90), bgcolor = color.new(#cce2cc, 50))

如何在Pine Script中创建一个矩形,显示前90分钟的范围?

The problem I have is that the rectangle for some weird reason is starting from bar 28, not bar 19 as intended. What am I doing wrong?

答案1

得分: 1

这是由于Pine可以绘制的默认框数限制所导致的。根据您的代码设置,您在超过18根柱形图之后会绘制新的框。默认的最大框数是50。您可以通过以下两种方法来修复这个问题。

第一种方法是在您的指标函数中使用max_boxes_count

indicator("18-bar range", overlay = true, max_boxes_count = 500)

第二种方法是使用var来设置您的框变量,然后使用set_right,这样每天只会有一个框,并且会随着每个新的柱形图而扩展到右侧。

var box myBox = na

if (barsCount == 18)
    myBox := box.new(left = bar_index + 1, top = rangeHigh, right = bar_index, bottom = rangeLow, border_color = color.new(#cce2cc, 90), bgcolor = color.new(#cce2cc, 50))

if barsCount >= 18
    myBox.set_right(bar_index)
英文:

This is due to the default number of boxes that Pine can draw. The way your code is set up you are drawing a new box on every new bar past barsCount 18. The maximum default number of boxes is 50. You can fix this two ways.

First using the max_boxes_count in your indicator function.

indicator(&quot;18-bar range&quot;, overlay = true, max_boxes_count = 500)

Second way you can set your box variable using var var box myBox = na and then the set_right this will only have one box per day and just continue to extend the right with every new bar

if (barsCount == 18)
    myBox := box.new(left = bar_index + 1, top = rangeHigh, right = bar_index, bottom = rangeLow, border_color = color.new(#cce2cc, 90), bgcolor = color.new(#cce2cc, 50))
if barsCount &gt;= 18
    myBox.set_right(bar_index)

huangapple
  • 本文由 发表于 2023年5月22日 05:34:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定