求助:修正Previous Day High/Low指标表格单元格背景色逻辑
功能实现验证与代码修正需求
我需要实现以下功能:当当前价格处于PrevDayHigh水平的1%范围内时,表格对应单元格背景设为红色,其余情况设为绿色;该逻辑需同样应用到PrevDayLow、PrevSecondDayHigh等所有其他价格水平。请问最后一行代码是否实现了该功能?以下是对应的Pine Script v5代码:
//@version=5 indicator(title="Previous Day/Second Day/Week High/Low", overlay=true) // INPUT labelTextColor = input.color(title='Header Text Color', defval=#FFFFFF) labelBgColor = input.color(title='Cell Color', defval=color.black) labelTextCol = input.color(title='Text Color', defval=#26A69A ) Red = color.rgb(255, 25, 25, 25) Green = color.rgb(19, 200, 25, 25) tablePosition = input.string(title='Labels Position', defval='Top Right', options=['Top Right', 'Top Left', 'Bottom Left', 'Bottom Right']) on_off_levels = input.bool(true, title="show/hide High/Low levels") float PrevDayHigh = request.security(syminfo.tickerid, 'D', high[1], lookahead = barmerge.lookahead_on) float PrevDayLow = request.security(syminfo.tickerid, 'D', low[1], lookahead = barmerge.lookahead_on) float PrevSecondDayHigh = request.security(syminfo.tickerid, 'D', high[2]) float PrevSecondDayLow = request.security(syminfo.tickerid, 'D', low[2]) float PrevWeekHigh = request.security(syminfo.tickerid, 'W', high[1]) float PrevWeekLow = request.security(syminfo.tickerid, 'W', low[1]) TT1_def = "Previous Day's High for the current stock" TT2_def = "Previous Day's Low for the current stock" TT3_def = "Previous Second Day's High for the current stock" TT4_def = "Previous Second Day's Low for the current stock" TT5_def = 'Previous Weeks High for the current stock' TT6_def = 'Previous Weeks Low for the current stock' // LEVELS time_ = request.security(syminfo.tickerid,"D", time[1]) if barstate.islast and on_off_levels hi_line = line.new(time_, PrevDayHigh, time_+(time-time[1]), PrevDayHigh, xloc.bar_time, extend.right, color=color.red) lo_line = line.new(time_, PrevDayLow, time_+(time-time[1]), PrevDayLow, xloc.bar_time, extend.right, color=color.green) label.new(x=time_, y=high, text="PDH", xloc=xloc.bar_time, yloc=yloc.abovebar, color=color.red, style=label.style_none, size =size.small, textcolor=color.red) label.new(x=time_, y=high, text="PDL", xloc=xloc.bar_time, yloc=yloc.belowbar, color=color.green, style=label.style_none, size =size.small, textcolor=color.green) if high >= (PrevDayHigh - PrevDayHigh * 0.01) label.new(bar_index, high, text="PDH $0,00", yloc=yloc.abovebar, color=color.blue, style=label.style_triangledown, size =size.small, textcolor=color.blue) if low <= (PrevDayLow + PrevDayLow * 0.01) label.new(bar_index, high, text="PDL $0,00", yloc=yloc.belowbar, color=color.blue, style=label.style_triangleup, size =size.small, textcolor=color.blue) // TABLE var table box = table.new(tablePosition == 'Top Left' ? position.top_left : tablePosition == 'Top Right' ? position.top_right : tablePosition == 'Bottom Left' ? position.bottom_left : position.bottom_right, 10, 7, labelBgColor, labelBgColor, 0, color.rgb(116, 116, 117), 1) if barstate.islast table.cell(box, 1, 0, 'PDH', text_color=labelTextColor) table.cell(box, 1, 1, 'PDL', text_color=labelTextColor) table.cell(box, 1, 2, 'P2DH', text_color=labelTextColor) table.cell(box, 1, 3, 'P2DL', text_color=labelTextColor) table.cell(box, 1, 4, 'PWH', text_color=labelTextColor) table.cell(box, 1, 5, 'PWL', text_color=labelTextColor) table.cell(box, 2, 0, text = str.tostring(PrevDayHigh), text_color=labelTextCol, tooltip = TT1_def) table.cell(box, 2, 1, text = str.tostring(PrevDayLow), text_color=labelTextCol, tooltip = TT2_def) table.cell(box, 2, 2, text = str.tostring(PrevSecondDayHigh), text_color=labelTextCol, tooltip = TT3_def) table.cell(box, 2, 3, text = str.tostring(PrevSecondDayLow), text_color=labelTextCol, tooltip = TT4_def) table.cell(box, 2, 4, text = str.tostring(PrevWeekHigh), text_color=labelTextCol, tooltip = TT5_def) table.cell(box, 2, 5, text = str.tostring(PrevWeekLow), text_color=labelTextCol, tooltip = TT6_def) //New code added here testing table.cell(box, 3, 0, text = str.tostring(math.round_to_mintick(PrevDayHigh)), bgcolor = bar_index < (PrevDayHigh - PrevDayHigh * 0.01) and bar_index > (PrevDayHigh - PrevDayHigh * 0.01) ? Red : Green, text_color = color.white)
代码分析与修正
最后一行代码没有实现预期功能,核心问题如下:
- 错误用
bar_index(K线索引)和价格水平做对比,逻辑完全偏离需求,应该用当前价格(如close/high)与目标价格区间对比 - 条件
bar_index < X and bar_index > X是矛盾的,永远不会成立,导致单元格背景始终为绿色 - 仅处理了
PrevDayHigh一个价格水平,未覆盖其他需要应用逻辑的项
修正思路
- 定义通用的价格区间判断函数,复用逻辑
- 对所有6个价格水平分别应用区间判断,设置对应单元格背景色
- 使用当前收盘价(
close)作为判断基准,也可根据需求替换为high/low
修正后的完整代码
//@version=5 indicator(title="Previous Day/Second Day/Week High/Low", overlay=true) // INPUT labelTextColor = input.color(title='Header Text Color', defval=#FFFFFF) labelBgColor = input.color(title='Cell Color', defval=color.black) labelTextCol = input.color(title='Text Color', defval=#26A69A ) Red = color.rgb(255, 25, 25, 25) Green = color.rgb(19, 200, 25, 25) tablePosition = input.string(title='Labels Position', defval='Top Right', options=['Top Right', 'Top Left', 'Bottom Left', 'Bottom Right']) on_off_levels = input.bool(true, title="show/hide High/Low levels") // 获取各周期高低价 float PrevDayHigh = request.security(syminfo.tickerid, 'D', high[1], lookahead = barmerge.lookahead_on) float PrevDayLow = request.security(syminfo.tickerid, 'D', low[1], lookahead = barmerge.lookahead_on) float PrevSecondDayHigh = request.security(syminfo.tickerid, 'D', high[2]) float PrevSecondDayLow = request.security(syminfo.tickerid, 'D', low[2]) float PrevWeekHigh = request.security(syminfo.tickerid, 'W', high[1]) float PrevWeekLow = request.security(syminfo.tickerid, 'W', low[1]) // 提示文本定义 TT1_def = "Previous Day's High for the current stock" TT2_def = "Previous Day's Low for the current stock" TT3_def = "Previous Second Day's High for the current stock" TT4_def = "Previous Second Day's Low for the current stock" TT5_def = 'Previous Weeks High for the current stock' TT6_def = 'Previous Weeks Low for the current stock' // 绘制高低价水平线 time_ = request.security(syminfo.tickerid,"D", time[1]) if barstate.islast and on_off_levels hi_line = line.new(time_, PrevDayHigh, time_+(time-time[1]), PrevDayHigh, xloc.bar_time, extend.right, color=color.red) lo_line = line.new(time_, PrevDayLow, time_+(time-time[1]), PrevDayLow, xloc.bar_time, extend.right, color=color.green) label.new(x=time_, y=high, text="PDH", xloc=xloc.bar_time, yloc=yloc.abovebar, color=color.red, style=label.style_none, size =size.small, textcolor=color.red) label.new(x=time_, y=high, text="PDL", xloc=xloc.bar_time, yloc=yloc.belowbar, color=color.green, style=label.style_none, size =size.small, textcolor=color.green) if high >= (PrevDayHigh - PrevDayHigh * 0.01) label.new(bar_index, high, text="PDH $0,00", yloc=yloc.abovebar, color=color.blue, style=label.style_triangledown, size =size.small, textcolor=color.blue) if low <= (PrevDayLow + PrevDayLow * 0.01) label.new(bar_index, high, text="PDL $0,00", yloc=yloc.belowbar, color=color.blue, style=label.style_triangleup, size =size.small, textcolor=color.blue) // 创建表格 var table box = table.new(tablePosition == 'Top Left' ? position.top_left : tablePosition == 'Top Right' ? position.top_right : tablePosition == 'Bottom Left' ? position.bottom_left : position.bottom_right, 10, 7, labelBgColor, labelBgColor, 0, color.rgb(116, 116, 117), 1) // 通用判断函数:当前价格是否在目标价格的1%区间内 isInRange(float currentPrice, float targetPrice) => lowerBound = targetPrice * 0.99 upperBound = targetPrice * 1.01 currentPrice >= lowerBound and currentPrice <= upperBound if barstate.islast // 表头 table.cell(box, 1, 0, 'PDH', text_color=labelTextColor) table.cell(box, 1, 1, 'PDL', text_color=labelTextColor) table.cell(box, 1, 2, 'P2DH', text_color=labelTextColor) table.cell(box, 1, 3, 'P2DL', text_color=labelTextColor) table.cell(box, 1, 4, 'PWH', text_color=labelTextColor) table.cell(box, 1, 5, 'PWL', text_color=labelTextColor) // 价格值单元格 table.cell(box, 2, 0, text = str.tostring(PrevDayHigh), text_color=labelTextCol, tooltip = TT1_def) table.cell(box, 2, 1, text = str.tostring(PrevDayLow), text_color=labelTextCol, tooltip = TT2_def) table.cell(box, 2, 2, text = str.tostring(PrevSecondDayHigh), text_color=labelTextCol, tooltip = TT3_def) table.cell(box, 2, 3, text = str.tostring(PrevSecondDayLow), text_color=labelTextCol, tooltip = TT4_def) table.cell(box, 2, 4, text = str.tostring(PrevWeekHigh), text_color=labelTextCol, tooltip = TT5_def) table.cell(box, 2, 5, text = str.tostring(PrevWeekLow), text_color=labelTextCol, tooltip = TT6_def) // 应用背景色逻辑的单元格(使用close作为当前价格,可替换为high/low/open) table.cell(box, 3, 0, text = str.tostring(math.round_to_mintick(PrevDayHigh)), bgcolor = isInRange(close, PrevDayHigh) ? Red : Green, text_color = color.white) table.cell(box, 3, 1, text = str.tostring(math.round_to_mintick(PrevDayLow)), bgcolor = isInRange(close, PrevDayLow) ? Red : Green, text_color = color.white) table.cell(box, 3, 2, text = str.tostring(math.round_to_mintick(PrevSecondDayHigh)), bgcolor = isInRange(close, PrevSecondDayHigh) ? Red : Green, text_color = color.white) table.cell(box, 3, 3, text = str.tostring(math.round_to_mintick(PrevSecondDayLow)), bgcolor = isInRange(close, PrevSecondDayLow) ? Red : Green, text_color = color.white) table.cell(box, 3, 4, text = str.tostring(math.round_to_mintick(PrevWeekHigh)), bgcolor = isInRange(close, PrevWeekHigh) ? Red : Green, text_color = color.white) table.cell(box, 3, 5, text = str.tostring(math.round_to_mintick(PrevWeekLow)), bgcolor = isInRange(close, PrevWeekLow) ? Red : Green, text_color = color.white)
关键修正点
- 新增
isInRange函数,统一处理价格区间判断,避免重复代码 - 用
close替代bar_index进行价格对比,符合需求逻辑 - 对所有6个价格水平都应用了背景色判断逻辑
- 修正了原代码中矛盾的条件判断,确保区间判断正确
内容的提问来源于stack exchange,提问作者Kevin Hunter
相关产品推荐
相关产品推荐

