Pine Script:如何按16分30秒等自定义时段绘制CPR线?
自定义时段CPR线绘制修复方案
问题说明
现有Pine Script代码仅能正确绘制第一个自定义时段(如16分30秒)的CPR线,后续时段无法正常生成。核心需求是将完整交易时段拆分为多个自定义时长的子时段(如15分钟、225分钟等),并在每个子时段生成对应的CPR线。
错误根源
- 时间计算错误:原代码中自定义时段的毫秒数计算逻辑错误,将16分30秒误算为16分钟+30分钟,正确计算应为
(16*60 + 30)*1000 = 990000毫秒。 - 时段结束点未适配自定义时长:原
END_POINT仅支持固定时段,未根据传入的自定义时段动态计算时长。 - 重复标签覆盖:使用
var label导致新时段标签覆盖旧标签,无法保留多时段标记。
修改后的完整代码
//@version=5 indicator(title="Custom Period CPR", shorttitle="CPR Custom", overlay=true, max_labels_count=500, max_lines_count=500, max_boxes_count=500) T0 = "Small font size recommended for mobile app or multiple layout" Transparency = input(defval=75) i_s_font = input.string("normal", "Font size", tooltip=T0, options=["tiny", "small", "normal", "large", "huge"]) i_timeZone = input('GMT+05:30') f_print(_txt) => var _lbl = label.new(bar_index, ta.highest(10)[1], _txt, xloc.bar_index, yloc.price, #00000000, label.style_none, color.black, size.tiny, text.align_left) label.set_xy(_lbl, bar_index, ta.highest(10)[1]) label.set_text(_lbl, _txt) f_print(str.tostring(dayofmonth(timenow, i_timeZone), '00-') + str.tostring(month(timenow, i_timeZone), '00-') + str.tostring(year(timenow, i_timeZone), '00')) // 检测新时段函数 is_newbar(res) => t = time(res) ta.change(t) != 0 ? 1 : 0 // 输入设置 Label_Color = input.color(defval=color.new(color.white,100), title='Label Background Color') // 自定义时段1:16分30秒(对应990秒,用"990S"作为时间框架) Want_Custom1 = input.bool(defval=true, title='16min30s CPR', inline='Custom1') Want_Custom1_Lines = input.bool(defval=true, title='Show Lines', inline='Custom1') Custom1_Color = input.color(defval=color.new(color.blue, 85), title='Line Color', inline='Custom1') Custom1_Width = input.int(defval=1, title='Line Width', inline='Custom1') // 自定义时段2:225分钟(对应"225"作为时间框架) Want_Custom2 = input.bool(defval=false, title='225min CPR', inline='Custom2') Want_Custom2_Lines = input.bool(defval=true, title='Show Lines', inline='Custom2') Custom2_Color = input.color(defval=color.new(color.orange, 85), title='Line Color', inline='Custom2') Custom2_Width = input.int(defval=1, title='Line Width', inline='Custom2') // CPR计算与绘制函数 drawCPR(TF, lineColor, lineWidth, showLines) => // 获取前一个时段的高低收数据 PDH = request.security(syminfo.ticker, TF, high[1], barmerge.gaps_off, barmerge.lookahead_on) PDL = request.security(syminfo.ticker, TF, low[1], barmerge.gaps_off, barmerge.lookahead_on) PDC = request.security(syminfo.ticker, TF, close[1], barmerge.gaps_off, barmerge.lookahead_on) Pivot = (PDH + PDL + PDC)/3 // 计算时段时长(毫秒) periodMs = switch TF "D" => 24*60*60*1000 "990S" => 990*1000 // 16分30秒 "225" => 225*60*1000 // 225分钟 => ta.time_to_seconds(time(TF))*1000 // 自动计算其他标准时段 // 新时段开始时绘制CPR线和标签 if is_newbar(TF) if showLines // 绘制当前时段的Pivot线 line.new(time, Pivot, time + periodMs, Pivot, color=lineColor, xloc=xloc.bar_time, width=lineWidth) // 生成唯一标签,避免覆盖 label.new(bar_index+1, Pivot, color=Label_Color, textcolor=lineColor, style=label.style_label_left, text=str.tostring(TF + ' PIV: ' + str.tostring(Pivot, format.mintick)), size=i_s_font) // 调用绘制函数 if Want_Custom1 drawCPR("990S", Custom1_Color, Custom1_Width, Want_Custom1_Lines) if Want_Custom2 drawCPR("225", Custom2_Color, Custom2_Width, Want_Custom2_Lines)
关键修改点
- 修正时间计算:针对16分30秒的时段,使用
990S作为时间框架字符串(对应990秒),并正确计算时段毫秒数。 - 动态适配时段时长:通过
switch语句处理不同时间框架,自动计算时段对应的毫秒数,支持任意标准Pine Script时间框架(如"15"、"225"、"990S"等)。 - 避免标签覆盖:移除
var label声明,每次新时段生成新标签,保留所有时段的CPR标记。 - 优化新时段检测:简化
is_newbar函数,直接通过ta.change(time(res))检测新时段,适配所有标准时间框架。
内容的提问来源于stack exchange,提问作者Abhishek
相关产品推荐
相关产品推荐

