Pine Script:line.new仅绘制当月线,如何显示所有月份月初高低线?
问题:Pine Script仅显示当前月份月初两日高低线,如何显示历史所有月份?
我尝试用Pine Script的line()函数绘制每个月前两个交易日的最高价和最低价水平线,代码如下:
//@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)
运行后发现该指标仅绘制当前月份的高低线,无法显示历史所有月份的对应线条,请问如何修改代码,使其能在显示的时间范围内绘制所有月份的月初两日高低线?
解决方案
原代码的核心问题是:每次进入新月份时,会覆盖存储高低价和起始位置的变量,且line.new()在每根K线都会重新生成当前月份的线条,历史月份的线条没有被持久化保存。要显示所有历史月份的线条,需要通过数组存储每个月份的线条对象,并在每个月份的线条确定后固定其终点。
修改步骤:
- 用数组分别存储历史的高价线和低价线对象,避免被新月份的数据覆盖
- 在新月份启动时,初始化当前月份的临时线条,实时更新前两个交易日的高低价
- 当进入下一个月份,或者当前月份的交易日超过2天时,将临时线条的终点固定,并添加到历史数组中
- 可选:添加线条数量限制,防止过多线条占用性能
修改后的代码:
//@version=5 indicator("All Months' First 2 Days High/Low Lines", overlay = true) // 数组存储历史的高低线对象 var line[] high_lines = array.new_line() var line[] low_lines = array.new_line() // 临时变量存储当前月份的线条和数据 var line current_high_line = na var line current_low_line = na var float current_hh = na var float current_ll = na var int current_days_elapsed = na var int current_month_start_idx = na new_month = ta.change(month) != 0 new_day = ta.change(dayofmonth) != 0 // 进入新月份时的处理:保存上一个月的线条,初始化当前月数据 if new_month // 如果存在上一个月的临时线条,固定终点并存入数组 if not na(current_high_line) line.set_x2(current_high_line, bar_index - 1) array.push(high_lines, current_high_line) if not na(current_low_line) line.set_x2(current_low_line, bar_index - 1) array.push(low_lines, current_low_line) // 初始化当前月份的临时数据 current_hh := high current_ll := low current_days_elapsed := 1 current_month_start_idx := bar_index // 创建当前月份的临时线条 current_high_line := line.new(current_month_start_idx, current_hh, bar_index, current_hh, color=color.green, width=2) current_low_line := line.new(current_month_start_idx, current_ll, bar_index, current_ll, color=color.red, width=2) // 新交易日(非新月份)更新天数 if new_day and not new_month current_days_elapsed += 1 // 前两个交易日内更新高低价和线条 if current_days_elapsed <= 2 current_hh := math.max(current_hh, high) current_ll := math.min(current_ll, low) // 更新当前线条的价格和终点 line.set_y1(current_high_line, current_hh) line.set_y2(current_high_line, current_hh) line.set_x2(current_high_line, bar_index) line.set_y1(current_low_line, current_ll) line.set_y2(current_low_line, current_ll) line.set_x2(current_low_line, bar_index) // 当月度过了前两个交易日,固定当前线条的终点(避免后续K线继续更新) if current_days_elapsed > 2 and not na(current_high_line) line.set_x2(current_high_line, bar_index) line.set_x2(current_low_line, bar_index) // 将临时线条转入数组并清空临时变量 array.push(high_lines, current_high_line) array.push(low_lines, current_low_line) current_high_line := na current_low_line := na // 可选:限制数组中线条的数量,避免性能问题(保留最近20个月份的线条) if array.size(high_lines) > 20 line.delete(array.shift(high_lines)) if array.size(low_lines) > 20 line.delete(array.shift(low_lines)) bgcolor(new_month ? color.new(color.yellow, 80) : na)
内容的提问来源于stack exchange,提问作者Sonny Hua
相关产品推荐
相关产品推荐

