如何在Pine Script的东京/伦敦/纽约开盘竖线上添加文本/标签
给时段竖线添加文本标签的修正代码
以下是修正后的Pine Script代码,解决了原代码的时间戳错误,并为每个时段竖线添加了对应文本标签:
//@version=5 indicator('Timegap', overlay=true, max_lines_count=500, max_bars_back=4999) // 排除周六周日(非交易日) is_weekday = dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday // 修正时间戳参数:第三个参数应为dayofmonth,而非hour t_tokyo = timestamp("GMT+7", year, month, dayofmonth, 07, 00, 00) // 东京开盘(GMT+7 7:00) t_london = timestamp("GMT+7", year, month, dayofmonth, 14, 00, 00) // 伦敦开盘(GMT+7 14:00) t_ny = timestamp("GMT+7", year, month, dayofmonth, 20, 00, 00) // 纽约开盘(GMT+7 20:00) t_midnight = timestamp("GMT+7", year, month, dayofmonth, 00, 00, 00) // 午夜线 // 定义标签样式函数,简化代码 create_label(time_x, label_text, label_color) => if is_weekday and time == time_x and barstate.isconfirmed // 在竖线右侧、价格上方创建标签 label.new(time_x, ta.highest(high, 20), label_text, color=color.new(label_color, 100), textcolor=label_color, style=label.style_label_left, size=size.small) // 创建竖线及对应标签 if is_weekday and time == t_tokyo and barstate.isconfirmed line.new(t_tokyo, low, t_tokyo, high, xloc.bar_time, extend.both, color.rgb(24, 21, 240), line.style_solid, 1) create_label(t_tokyo, "东京开盘", color.rgb(24, 21, 240)) if is_weekday and time == t_london and barstate.isconfirmed line.new(t_london, low, t_london, high, xloc.bar_time, extend.both, color.rgb(255, 235, 59), line.style_solid, 1) create_label(t_london, "伦敦开盘", color.rgb(255, 235, 59)) if is_weekday and time == t_ny and barstate.isconfirmed line.new(t_ny, low, t_ny, high, xloc.bar_time, extend.both, color.rgb(255, 82, 82), line.style_solid, 1) create_label(t_ny, "纽约开盘", color.rgb(255, 82, 82)) if is_weekday and time == t_midnight and barstate.isconfirmed line.new(t_midnight, low, t_midnight, high, xloc.bar_time, extend.both, color.rgb(54, 58, 69), line.style_solid, 2) create_label(t_midnight, "午夜", color.rgb(54, 58, 69))
关键修改说明
- 时间戳修正:原代码中
timestamp函数的参数顺序错误,将hour放在了day的位置,改为dayofmonth后才能正确生成当日对应时段的时间戳。 - 周末过滤:修正原周末判断逻辑,只在工作日生成线条和标签。
- 标签添加:通过
label.new创建文本标签,设置标签与对应竖线同色,位置选在竖线左侧、近期高点上方,避免遮挡K线。 - 重复创建防护:通过
time == 目标时间戳和barstate.isconfirmed判断,确保每个时段只创建一次线条和标签,避免超出最大线条数量限制。
内容的提问来源于stack exchange,提问作者Riyadl Mufid Isfihanni
相关产品推荐
相关产品推荐

