如何为Pine Script直方图的彩色柱状条设置Offset=-2向左偏移
问题描述
我尝试将代表零线(Zeroline)上下分形的彩色直方图柱状条向左偏移2个单位(offset=-2),该偏移在普通plot中可正常生效,但无法在直方图上实现。我无脚本编写经验,仅会复制粘贴代码及简单调试,已为此卡壳数周,尝试将offset命令应用于直方图相关代码行但无效。
解决方案
由于Pine Script中plot.style_columns不支持直接使用offset参数,我们需要通过手动偏移数据和分形判断逻辑来实现效果。以下是修改后的完整代码:
//@version=4 study("TSI histogram Fractal Alerts", shorttitle='TSI Alerts', precision=1) long = input(title="Long Length", type=input.integer, defval=16) short = input(title="Short Length", type=input.integer, defval=8) sig1 = input(title="Sig1 Length", type=input.integer, defval=6) price = close pc = change(price) src = input(close, "source") Zeroline = 0 check = input(true, "Histogram Alert Points") offset_val = 2 // 定义向左偏移的单位数 double_smooth(src, long, short) => fist_smooth = ema(src, long) ema(fist_smooth, short) double_smoothed_pc = double_smooth(pc, long, short) double_smoothed_abs_pc = double_smooth(abs(pc), long, short) tsi_value = 100 * (double_smoothed_pc / double_smoothed_abs_pc) hist = tsi_value - ema(tsi_value, sig1) signal = ema(tsi_value, sig1) // ////////////////////////////////////////////////////////////// // 调整分形判断的索引,同步偏移逻辑 h1 = hist[4 + offset_val] h2 = hist[3 + offset_val] h3 = hist[2 + offset_val] h4 = hist[1 + offset_val] h5 = hist[0 + offset_val] l1 = hist[4 + offset_val] l2 = hist[3 + offset_val] l3 = hist[2 + offset_val] l4 = hist[1 + offset_val] l5 = hist[0 + offset_val] histhi = h1 < h2 and h2 < h3 and h3 > h4 and h4 > h5 histlo = l1 > l2 and l2 > l3 and l3 < l4 and l4 < l5 // histogram pattern ----- // 使用偏移后的直方图数据匹配颜色逻辑 hist_offset = hist[offset_val] hist_up_plus = hist_offset >= 0 and histhi hist_down_plus = hist_offset <= 0 and histlo hist_up_minus = hist_offset <= 0 and histhi hist_down_minus = hist_offset >= 0 and histlo hist_color = hist_up_plus ? color.rgb(234, 54, 54) : hist_down_plus ? color.rgb(39, 154, 89) : hist_up_minus ? color.rgb(67, 85, 187) : hist_down_minus ? color.rgb(183, 172, 50) : color.rgb (242, 244, 244) // plot ----- // 绘制偏移后的直方图 plot(hist_offset, "Histogram", hist_color, 1, plot.style_columns) plot(signal, "Signal", #f1cb0a, 1, transp=0, linewidth=1) plot(tsi_value, "TSI", #0094ff, 1, transp=95, linewidth=1) // alert ----- plotchar(check and (hist[1] < 0 and hist[0] > 0), "Histogram Crossover", "▔", location.top, color.rgb(247, 225, 61), 20) plotchar(check and (hist[1] > 0 and hist[0] < 0), "Histogram Crossunder", "▁", location.bottom, color.rgb(247, 225, 61), 20) plotchar(check and (crossover(signal, +0)), "Signal Line Crossover", "▔", location.top, color.rgb(96, 162, 243), 20) plotchar(check and (crossunder(signal, -0)), "Signal Line Crossover", "▁", location.bottom, color.rgb(96, 162, 243), 20) // Line plots hline(0, title="Zeroline", color=color.rgb(50, 49, 49, transp=50), linewidth=1) hline(40, title="Overbought", color=color.rgb(36, 140, 36, transp=50), linewidth=1) hline(-40, title="Oversold", color=color.rgb(197, 49, 49, transp=50), linewidth=1) hline(20, title="Neutral Zone_T", color=color.rgb(50, 49, 49, transp=50), linewidth=1) hline(-20, title="Neutral Zone_B", color=color.rgb(50, 49, 49, transp=50), linewidth=1)
关键修改说明
- 统一偏移量控制:新增
offset_val = 2变量,后续可直接修改数值调整偏移幅度 - 分形判断同步偏移:将分形逻辑中用到的
hist索引全部加上偏移量,确保分形形态的判断对应到偏移后的柱子位置 - 直方图数据偏移:创建
hist_offset变量存储偏移后的直方图数据,绘制时使用该变量实现柱子左移效果 - 颜色逻辑匹配:颜色判断使用偏移后的
hist_offset,保证分形颜色与偏移后的柱子正确对应
内容的提问来源于stack exchange,提问作者Roadside
相关产品推荐
相关产品推荐

