如何在Pine Script中让Daily CPR指标显示所有历史日期标签?
解决Daily CPR指标显示所有历史标签的问题
问题根源
原代码的drawlabel函数在每个K线周期创建新标签后立刻删除前一个,且仅在当日执行,导致仅显示当日标签。要保留所有历史标签,需在CPR数值更新(新交易日启动)时创建标签,且不删除旧标签,同时用变量存储标签对象避免重复创建。
修改后的完整代码
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © pbghosh //@version=5 indicator(title='Daily CPR', shorttitle='D-CPR', overlay=true, format=format.price, precision=2) blue = color.blue fuchsia = color.fuchsia white = color.white var color_TC = input.color(defval=color.new(blue, 0), title='TC', group='CPR') var color_CP = input.color(defval=color.new(fuchsia, 0), title='CP', group='CPR') var color_BC = input.color(defval=color.new(blue, 0), title='BC', group='CPR') var color_R1 = input.color(defval=color.new(#FF4000,0), title='R1', group='CPR') var color_R2 = input.color(defval=color.new(#FF4000 ,0), title='R2', group='CPR') var color_R3 = input.color(defval=color.new(#FF4000, 0), title='R3', group='CPR') var color_R4 = input.color(defval=color.new(#FF4000, 0), title='R4', group='CPR') var color_R5 = input.color(defval=color.new(#FF4000,0), title='R5', group='CPR') var color_S1 = input.color(defval=color.new(#FF4000,0), title='S1', group='CPR') var color_S2 = input.color(defval=color.new(#FF4000, 0), title='S2', group='CPR') var color_S3 = input.color(defval=color.new(#FF4000, 0), title='S3', group='CPR') var color_S4 = input.color(defval=color.new(#FF4000,0), title='S4', group='CPR') var color_S5 = input.color(defval=color.new(#FF4000,0), title='S5', group='CPR') var color_PDH = input.color(defval=color.new(white, 0), title='PDH', group='CPR') var color_PDL = input.color(defval=color.new(white, 0), title='PDL', group='CPR') // 获取日线数据(禁用未来函数,避免数据偏差) h = request.security(syminfo.tickerid, 'D', high[1], lookahead=barmerge.lookahead_off) l = request.security(syminfo.tickerid, 'D', low[1], lookahead=barmerge.lookahead_off) c = request.security(syminfo.tickerid, 'D', close[1], lookahead=barmerge.lookahead_off) showlabel = true // 用var数组存储每个级别的历史标签,永久保存不丢失 var label[] tc_labels = array.new_label() var label[] cp_labels = array.new_label() var label[] bc_labels = array.new_label() var label[] r1_labels = array.new_label() var label[] r2_labels = array.new_label() var label[] r3_labels = array.new_label() var label[] r4_labels = array.new_label() var label[] r5_labels = array.new_label() var label[] s1_labels = array.new_label() var label[] s2_labels = array.new_label() var label[] s3_labels = array.new_label() var label[] s4_labels = array.new_label() var label[] s5_labels = array.new_label() var label[] pdh_labels = array.new_label() var label[] pdl_labels = array.new_label() // 绘制历史标签:仅当级别数值变化时创建新标签 draw_historical_label(label_array, level, title, color) => // 检测CPR级别是否更新(新交易日触发) if level != level[1] // 在当前K线左侧创建标签 new_label = label.new(bar_index, level, title + " (" + str.tostring(level, format.mintick) + ")", color=color.new(color.black, 100), style=label.style_label_left, textcolor=color) array.push(label_array, new_label) notHigherTimeframe = timeframe.period != '30' and timeframe.period != '45' and timeframe.period != '60' and timeframe.period != '120' and timeframe.period != '180' and timeframe.period != '240' and timeframe.period != 'D' and timeframe.period != 'W' and timeframe.period != 'M' calculatePivot() => (h + l + c) / 3 calculateBC() => (h + l) / 2 calculateTC() => calculatePivot() - calculateBC() + calculatePivot() truncate(number, decimals) => factor = math.pow(10, decimals) int(number * factor) / factor pivot = calculatePivot() tc = calculateTC() bc = calculateBC() // 交换TC和BC位置(确保TC在BC上方) if tc < bc tc, bc = bc, tc r1Level = truncate(2 * pivot - l, 2) r2Level = truncate(pivot + h - l, 2) r3Level = truncate(h + 2 * (pivot - l), 2) r4Level = truncate(h + 3 * (pivot - l), 2) r5Level = truncate(h + 4 * (pivot - l), 2) s1Level = truncate(2 * pivot - h, 2) s2Level = truncate(pivot - (h - l), 2) s3Level = truncate(l - 2 * (h - pivot), 2) s4Level = truncate(l - 3 * (h - pivot), 2) s5Level = truncate(l - 4 * (h - pivot), 2) // 绘制CPR水平线 plot(series=notHigherTimeframe ? truncate(tc, 2) : na, title='TC', color=color.new(color_TC, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? truncate(pivot, 2) : na, title='CP', color=color.new(color_CP, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? truncate(bc, 2) : na, title='BC', color=color.new(color_BC, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? r1Level : na, title='R1', color=color.new(color_R1, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? r2Level : na, title='R2', color=color.new(color_R2, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? r3Level : na, title='R3', color=color.new(color_R3, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? r4Level : na, title='R4', color=color.new(color_R4, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? r5Level : na, title='R5', color=color.new(color_R5, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? s1Level : na, title='S1', color=color.new(color_S1, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? s2Level : na, title='S2', color=color.new(color_S2, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? s3Level : na, title='S3', color=color.new(color_S3, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? s4Level : na, title='S4', color=color.new(color_S4, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? s5Level : na, title='S5', color=color.new(color_S5, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? h : na, title='PDH', color=color.new(color_PDH, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) plot(series=notHigherTimeframe ? l : na, title='PDL', color=color.new(color_PDL, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1) // 触发历史标签绘制:仅在日内周期且非高级别周期执行 if showlabel and timeframe.isintraday and notHigherTimeframe draw_historical_label(tc_labels, truncate(tc,2), 'TC', color_TC) draw_historical_label(cp_labels, truncate(pivot,2), 'CP', color_CP) draw_historical_label(bc_labels, truncate(bc,2), 'BC', color_BC) draw_historical_label(r1_labels, r1Level, 'R1', color_R1) draw_historical_label(r2_labels, r2Level, 'R2', color_R2) draw_historical_label(r3_labels, r3Level, 'R3', color_R3) draw_historical_label(r4_labels, r4Level, 'R4', color_R4) draw_historical_label(r5_labels, r5Level, 'R5', color_R5) draw_historical_label(s1_labels, s1Level, 'S1', color_S1) draw_historical_label(s2_labels, s2Level, 'S2', color_S2) draw_historical_label(s3_labels, s3Level, 'S3', color_S3) draw_historical_label(s4_labels, s4Level, 'S4', color_S4) draw_historical_label(s5_labels, s5Level, 'S5', color_S5) draw_historical_label(pdh_labels, h, 'PDH', color_PDH) draw_historical_label(pdl_labels, l, 'PDL', color_PDL)
关键修改说明
- 标签持久化存储:每个CPR级别对应一个
var label[]数组,永久保存创建的标签对象,不会在图表刷新时丢失。 - 精准触发逻辑:仅当CPR级别数值变化(新交易日启动)时创建新标签,避免每个K线周期重复创建。
- 移除删除逻辑:删除原代码中
label.delete语句,保留所有历史标签。 - 优化数据获取:将
lookahead=barmerge.lookahead_on改为lookahead_off,禁用未来函数,确保指标数据准确性。
内容的提问来源于stack exchange,提问作者cryptoweeknd
相关产品推荐
相关产品推荐

