Pine Editor V5:如何在每周五最后一根K线绘制竖线及代码优化
问题1:在Pine Editor V5中,如何在每周五的最后一根日K线处绘制竖线?
分两种场景实现,对应不同图表周期:
场景1:图表为日K线周期
日K线本身就是单日的最后一根K线,只需判断当前K线为周五即可:
//@version=5 indicator("周五日K竖线", overlay=true) // 判断当前K线是否为周五 isFriday = dayofweek == dayofweek.friday // 在周五K线位置绘制覆盖图表上下范围的竖线 if isFriday line.new(bar_index, ta.highest(ta.highest, 100), bar_index, ta.lowest(ta.lowest, 100), color=color.orange, width=2, style=line.style_solid)
bar_index作为竖线的横坐标,对应周五K线的位置ta.highest(ta.highest, 100)/ta.lowest(ta.lowest, 100)取最近100根K线的高低点,确保竖线覆盖可见图表范围,可按需调整数值
场景2:图表为日内周期(如小时线、分钟线)
需要同时判断「周五」和「当日最后一根K线」(以美股16:00收盘为例,需根据品种调整时间):
//@version=5 indicator("周五日内最后K线竖线", overlay=true) // 合并判断条件 isFridayLastCandle = dayofweek == dayofweek.friday and hour == 16 and minute == 0 if isFridayLastCandle line.new(bar_index, ta.highest(ta.highest, 100), bar_index, ta.lowest(ta.lowest, 100), color=color.orange, width=2)
问题2:修改代码将三角形改为竖线,及更简洁实现方案
修改后的基础代码(替换三角形为竖线)
将原代码的plotshape替换为line.new(),同时合并冗余条件:
//@version=5 indicator("周五竖线", overlay=true) // 合并条件:周五且是当日最后一根K线 isTargetCandle = dayofweek == dayofweek.friday and hour == 16 and minute == 0 // 满足条件时绘制竖线 if isTargetCandle line.new(bar_index, ta.highest(ta.highest, 200), bar_index, ta.lowest(ta.lowest, 200), color=color.orange, width=2, style=line.style_dashed)
更简洁的优化方案(避免线条堆积)
如果只需要显示最新的周五竖线,可通过var声明单根线条,每次满足条件时更新位置,避免图表堆积大量线条:
//@version=5 indicator("周五竖线(简洁版)", overlay=true) isTargetCandle = dayofweek == dayofweek.friday and hour == 16 and minute == 0 // 声明单根竖线变量,仅初始化一次 var line fridayLine = na // 满足条件时更新竖线 if isTargetCandle // 删除旧线条(如果存在) if not na(fridayLine) line.delete(fridayLine) // 创建/更新竖线位置 fridayLine := line.new(bar_index, ta.highest, bar_index, ta.lowest, color=color.orange, width=2)
var确保变量仅初始化一次,避免重复创建- 每次触发条件时先删除旧线条,再生成新线条,保持图表整洁
如果是实时看盘场景,只需针对最新K线绘制,可进一步简化:
//@version=5 indicator("周五最后K线竖线(极简版)", overlay=true) // 周五且是当前图表最后一根K线 if dayofweek == dayofweek.friday and ta.barstate.islast line.new(bar_index, ta.highest(ta.highest, 100), bar_index, ta.lowest(ta.lowest, 100), color=color.orange)
内容的提问来源于stack exchange,提问作者LMiller_1022
相关产品推荐
相关产品推荐

