Pine Script v5中获取指定标签UNIX时间及解决时间显示异常
问题原因
你遇到的1969-12-31 19:00是因为**label.get_x()返回的是K线的索引值,不是UNIX时间戳**。str.format_time()需要的是毫秒级UNIX时间(对应Pine里的time变量),而小数值的bar索引会被解析为UNIX纪元初期的时间(时区偏移导致显示1969年底)。
解决方案
不要通过标签获取时间,而是在检测到枢轴高点/低点时直接记录对应的时间戳,和枢轴价格同步存储。
关键修改点
- 添加变量存储枢轴对应的时间戳:
ph_time0、ph_time1、ph_time2 - 在检测到新的枢轴高点时,同步记录枢轴实际出现K线的时间(
time[rightLenH],因为ta.pivothigh在右侧满足条件时才确认枢轴) - 用存储的时间戳调用
str.format_time()生成正确的日期时间
修改后的完整代码
//@version=5 indicator("LittleQ", shorttitle="LQ", overlay=true, max_labels_count=500,max_bars_back = 5000) lengthGroupTitle = "LENGTH LEFT / RIGHT" colorGroupTitle = "Text Color / Label Color" leftLenH = input.int(title="Pivot High", defval=10, minval=1, inline="Pivot High", group=lengthGroupTitle) rightLenH = input.int(title="/", defval=10, minval=1, inline="Pivot High", group=lengthGroupTitle) textColorH = input(title="Pivot High", defval=color.white, inline="Pivot High", group=colorGroupTitle) labelColorH = input(title="", defval=color.blue, inline="Pivot High", group=colorGroupTitle) leftLenL = input.int(title="Pivot Low", defval=10, minval=1, inline="Pivot Low", group=lengthGroupTitle) rightLenL = input.int(title="/", defval=10, minval=1, inline="Pivot Low", group=lengthGroupTitle) textColorL = input(title="Pivot Low", defval=color.white, inline="Pivot Low", group=colorGroupTitle) labelColorL = input(title="", defval=color.red, inline="Pivot Low", group=colorGroupTitle) //Trend Filter ema1 = ta.ema(close,200) Ctrend = if ema1[5] > ema1[4] and ema1[4]>ema1[3] and ema1[3]>ema1[2] and ema1[2]>ema1[1] and ema1[1]>ema1[0] "Bearish" else if ema1[5]<ema1[4] and ema1[4]<ema1[3] and ema1[3]<ema1[2] and ema1[2]<ema1[1] and ema1[1]<ema1[0] "Bullish" // table input showTable = input.bool(true, "Show Table") tablePosInput = input.string(title="Position", defval="Top Right", options=["Bottom Left", "Bottom Right", "Top Left", "Top Right"], tooltip="Select where you want the table to draw.") var tablePos = tablePosInput == "Bottom Left" ? position.bottom_left : tablePosInput == "Bottom Right" ? position.bottom_right : tablePosInput == "Top Left" ? position.top_left : tablePosInput == "Top Right" ? position.top_right : na ph = ta.pivothigh(leftLenH, rightLenH) pl = ta.pivotlow(leftLenL, rightLenL) // 存储枢轴价格 var float ph0 = 0 var float ph1 = 0 var float ph2 = 0 var float pl0 = 0 var float pl1 = 0 var float pl2 = 0 // 新增:存储枢轴对应的UNIX时间戳 var float ph_time0 = 0 var float ph_time1 = 0 var float ph_time2 = 0 ph_non_na = nz(ph,0) if ph_non_na != 0 ph2 := ph1 ph1 := ph0 ph0 := ph_non_na // 同步记录枢轴K线的时间:ta.pivothigh在右侧满足条件时确认,所以枢轴的K线是bar_index[rightLenH] ph_time2 := ph_time1 ph_time1 := ph_time0 ph_time0 := time[rightLenH] pl_non_na = nz(pl,0) if pl_non_na != 0 pl2 := pl1 pl1 := pl0 pl0 := pl_non_na drawLabel(_offset, _pivot, _style, _color, _textColor) => if not na(_pivot) label.new(bar_index[_offset], _pivot, str.tostring(_pivot, format.mintick), style=_style, color=_color, textcolor=_textColor) lph0 = drawLabel(rightLenH, ph, label.style_label_down, labelColorH, textColorH) drawLabel(rightLenL, pl, label.style_label_up, labelColorL, textColorL) // table color strongRed = color.rgb(255, 25, 25, 25) strongGreen = color.rgb(19, 200, 25, 25) blue = color.rgb(20, 123, 134, 25) var dataTable = table.new(tablePos, columns = 4, rows = 5, border_color = color.rgb(40, 40, 40, 25), border_width = 2) if showTable table.cell(dataTable, 1, 1, text = str.tostring(Ctrend),bgcolor =str.contains(Ctrend,"Bull")? strongGreen : strongRed,text_color = color.white) table.cell(dataTable, 1, 2, text = "Last1", bgcolor = blue, text_color = color.white) table.cell(dataTable, 1, 3, text = "Last2", bgcolor = blue, text_color = color.white) table.cell(dataTable, 1, 4, text = "Last3 ", bgcolor = blue, text_color = color.white) table.cell(dataTable, 2, 1, text = "Value ", bgcolor = blue, text_color = color.white) table.cell(dataTable, 3, 1, text = "Date Time", bgcolor = blue, text_color = color.white) table.cell(dataTable, 2, 2, text = str.tostring(ph0), bgcolor = blue, text_color = color.white) table.cell(dataTable, 2, 3, text = str.tostring(ph1), bgcolor = blue, text_color = color.white) table.cell(dataTable, 2, 4, text = str.tostring(ph2), bgcolor = blue, text_color = color.white) // 使用存储的时间戳格式化,避免初始状态显示无效时间 table.cell(dataTable, 3, 2, text = ph_time0 != 0 ? str.format_time(ph_time0,"yyyy-MM-dd HH:mm",syminfo.timezone) : "", bgcolor = blue, text_color = color.white) table.cell(dataTable, 3, 3, text = ph_time1 != 0 ? str.format_time(ph_time1,"yyyy-MM-dd HH:mm",syminfo.timezone) : "", bgcolor = blue, text_color = color.white) table.cell(dataTable, 3, 4, text = ph_time2 != 0 ? str.format_time(ph_time2,"yyyy-MM-dd HH:mm",syminfo.timezone) : "", bgcolor = blue, text_color = color.white)
说明
- 当
ta.pivothigh返回非NA值时,枢轴高点实际出现在bar_index[rightLenH]的位置,因此对应的时间是time[rightLenH] - 时间戳变量与价格变量同步更新,确保时间和枢轴价格一一对应
- 格式化时间时增加非零判断,避免初始状态显示无效内容
内容的提问来源于stack exchange,提问作者memet foe
相关产品推荐
相关产品推荐

