求助:获取指定时段内前一日的HH、LL及收盘价(Pine Script)
Pine Script 修正:获取指定时段前一日的高低点及收盘价(IST时区)
原代码存在的问题
- 硬编码时段时间,未使用用户输入的
first_session/second_session参数,且未指定IST时区,导致时段判断不准确 - 使用
var声明的变量不会每日重置,跨天数据会累积,无法正确分离当日与前一日的高低点 - 未实现前一日时段数据的存储逻辑,也未跟踪指定时段的收盘价
修正后的代码
//@version=5 indicator("Previous Day Session High/Low/Close", overlay=true, timezone="Asia/Kolkata") // 用户自定义时段(IST时区) first_session = input.session("0915-1225", title="First Trading Session") second_session = input.session("1230-1530", title="Second Trading Session") // 检测是否处于指定时段(使用输入的session参数+IST时区) is_in_first_session = time(timeframe.period, first_session, "Asia/Kolkata") != na is_in_second_session = time(timeframe.period, second_session, "Asia/Kolkata") != na // 变量声明:区分当日和前一日的时段数据 var float current_first_high = na var float current_first_low = na var float current_first_close = na var float current_second_high = na var float current_second_low = na var float current_second_close = na var float prev_first_high = na var float prev_first_low = na var float prev_first_close = na var float prev_second_high = na var float prev_second_low = na var float prev_second_close = na // 检测新交易日(每日开盘时触发) new_day = ta.change(time("D")) // 新交易日时:保存前一日数据,重置当日数据 if new_day // 保存前一日的时段数据 prev_first_high := current_first_high prev_first_low := current_first_low prev_first_close := current_first_close prev_second_high := current_second_high prev_second_low := current_second_low prev_second_close := current_second_close // 重置当日数据 current_first_high := na current_first_low := na current_first_close := na current_second_high := na current_second_low := na current_second_close := na // 跟踪第一时段的当日高低点及收盘价 if is_in_first_session current_first_high := na(current_first_high) ? high : math.max(current_first_high, high) current_first_low := na(current_first_low) ? low : math.min(current_first_low, low) current_first_close := close // 实时更新为当前时段的最新收盘价 // 跟踪第二时段的当日高低点及收盘价 if is_in_second_session current_second_high := na(current_second_high) ? high : math.max(current_second_high, high) current_second_low := na(current_second_low) ? low : math.min(current_second_low, low) current_second_close := close // 实时更新为当前时段的最新收盘价 // 绘制前一日时段的高低点水平线 var line prev_first_high_line = na var line prev_first_low_line = na var line prev_second_high_line = na var line prev_second_low_line = na if new_day // 删除旧的水平线 line.delete(prev_first_high_line) line.delete(prev_first_low_line) line.delete(prev_second_high_line) line.delete(prev_second_low_line) // 绘制新的前一日时段高低线 prev_first_high_line := line.new(bar_index, prev_first_high, bar_index + 100, prev_first_high, color=color.new(color.green, 0), width=2, style=line.style_dashed) prev_first_low_line := line.new(bar_index, prev_first_low, bar_index + 100, prev_first_low, color=color.new(color.red, 0), width=2, style=line.style_dashed) prev_second_high_line := line.new(bar_index, prev_second_high, bar_index + 100, prev_second_high, color=color.new(color.blue, 0), width=2, style=line.style_dashed) prev_second_low_line := line.new(bar_index, prev_second_low, bar_index + 100, prev_second_low, color=color.new(color.orange, 0), width=2, style=line.style_dashed) // 最后一根K线显示前一日数据标签 if barstate.islast label_text = str.format("前一日第一时段:\n高点: {0}\n低点: {1}\n收盘价: {2}\n\n前一日第二时段:\n高点: {3}\n低点: {4}\n收盘价: {5}", prev_first_high, prev_first_low, prev_first_close, prev_second_high, prev_second_low, prev_second_close) label.new(bar_index + 2, hl2, style=label.style_label_upper_right, color=color.new(color.yellow, 70), size=size.large, text=label_text)
关键修改说明
- 时区与时段处理:指定
timezone="Asia/Kolkata"(IST时区),使用time()函数结合用户输入的session参数判断时段,替代原硬编码的时间逻辑,确保时段判断准确。 - 数据分离与重置:用
new_day检测交易日切换,将当日数据与前一日数据分离,每日开盘时保存前一日数据并重置当日变量,避免跨天数据累积。 - 收盘价跟踪:新增
current_first_close/current_second_close变量,实时更新指定时段的收盘价,并在新交易日时保存为前一日收盘价。 - 可视化优化:添加前一日时段高低点的虚线绘制,方便直观查看,同时优化标签文本格式,清晰展示所有需要的数据。
内容的提问来源于stack exchange,提问作者user10286
相关产品推荐
相关产品推荐

