Pine Script用timestamp函数动态获取昨日日期及过滤当日标签方法
实现方案
直接在原有timestamp函数的日参数上减1即可,Pine Script的timestamp会自动处理跨月、跨年的日期偏移,无需额外校准年/月数值,就能得到动态更新的昨日15:30时间戳,天然过滤当日的标签渲染。
核心修改仅需替换endDate行代码:
endDate=timestamp("UTC+5:30",year,month,dayofmonth - 1, 15,30,00)
如果需要兼容节假日、周休等非交易日场景,避免日期计算偏差,可以启用你注释的换日判断逻辑优化,完整修改后的代码如下(兼容Pine Script v4):
study("temp",overlay=true) startDate=input(title="Start Date",type=input.time,defval=timestamp("01 Jan 2000 09:15:00 UTC+5:30")) newDay=change(time("D"))!= 0 // 取上一个交易日的年/月/日参数,非交易日不会触发日期更新 _year=valuewhen(newDay,year[1],0) _month = valuewhen(newDay, month[1], 0) _yday = valuewhen(newDay, dayofmonth[1], 0) endDate=timestamp("UTC+5:30",_year,_month,_yday, 15,30,00) labelSession=input(title="Time session to analysis",type=input.session,defval="0915-1525") inSession(sess)=> na(time(timeframe.period,sess + ":23456")) == false and time >= startDate and time<= endDate dayLow()=>security(syminfo.ticker,"D",low,lookahead=true) if inSession(labelSession) and not inSession(labelSession)[1] label.new(bar_index,dayLow()-20,"Hello",style=label.style_label_left,textalign=text.align_left,color=#d0cec2,textcolor=color.black)
如果你使用的是Pine Script v5版本,只需把
study改为indicator、security改为request.security即可正常运行。
内容的提问来源于stack exchange,提问作者Vignesh A
相关产品推荐
相关产品推荐

