我尝试使用 line() 函数绘制每月前 2 天的最高和最低价格线,如下所示:
//@version=5
indicator("Highest and lowerest lines of the first 2 days of month", overlay = true)
var float hh = na
var float ll = na
var int days_elapsed = na
var int x1 = na
new_month = ta.change(month) != 0
new_day = ta.change(dayofmonth) != 0
if new_month
hh := high
ll := low
days_elapsed := 1
x1 := bar_index
if new_day and not new_month
days_elapsed += 1
if days_elapsed <= 2
hh := math.max(hh, high)
ll := math.min(ll, low)
bgcolor(new_month ? color.yellow : na)
line.new(x1, hh, bar_index, hh, xloc = xloc.bar_index, color = color.green)
line.new(x1, ll, bar_index, ll, xloc = xloc.bar_index, color = color.red)
但是,运行时,它仅绘制当月的线条,而不绘制前几个月的线条。我该如何解决这个问题,以便它能够在显示的时间范围内绘制所有月份的线条?
您可以使用
var
变量创建行,然后它将保存之前的行。您还需要使用 if 语句来设置 x2 变量。您当前的方式是,每个新柱都会绘制一条新线,因此由于 max_line 限制,您将无法看到太多历史记录。
//@version=5
indicator("Highest and lowerest lines of the first 2 days of month", overlay = true, max_lines_count = 500)
var line hLine = na
var line lLine = na
var float hh = na
var float ll = na
var int days_elapsed = na
var int x1 = na
new_month = ta.change(month) != 0
new_day = ta.change(dayofmonth) != 0
bgcolor(new_month ? color.yellow : na)
if new_month
hh := high
ll := low
days_elapsed := 1
x1 := bar_index
hLine := line.new(x1, hh, bar_index, hh, xloc = xloc.bar_index, color = color.green)
lLine := line.new(x1, ll, bar_index, ll, xloc = xloc.bar_index, color = color.red)
if new_day and not new_month
days_elapsed += 1
if days_elapsed <= 2
hh := math.max(hh, high)
ll := math.min(ll, low)
hLine.set_y1(hh), hLine.set_y2(hh)
lLine.set_y1(ll), lLine.set_y2(ll)
if bar_index > bar_index[1] and not new_month
hLine.set_x2(bar_index), lLine.set_x2(bar_index)