You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TradingView绘制印度股市15:00-15:30递增移动平均线?

实现印度股市15:00-15:30递增移动平均线

核心思路

这个需求本质是时段内的滚动累计平均:仅在15:00-15:30的1分钟K线周期内,每根新K线生成时,计算从15:00到当前所有收盘价的算术平均。常规SMA因固定周期长度,无法实时更新递增均值,因此需要自定义累计逻辑。

Pine Script 实现代码

//@version=5
indicator("IST 15:00-15:30 递增均线", overlay=true)

// 定义印度标准时间(IST)的时段:15:00-15:30
istOffset = 330 // IST = UTC+5:30,单位分钟
sessionStart = timestamp("UTC", year, month, dayofmonth, 9, 30) // 15:00 IST 对应 UTC 09:30
sessionEnd = timestamp("UTC", year, month, dayofmonth, 10, 0)   // 15:30 IST 对应 UTC 10:00

// 判断当前K线是否在目标时段内
inSession = time >= sessionStart and time < sessionEnd

// 计算时段内的累计收盘价之和
cumulativeClose = ta.sum(close, ta.barssince(not inSession) + 1)

// 计算时段内已走过的K线数量(N从1到30)
barCount = ta.barssince(not inSession) + 1

// 生成递增均值,非时段内返回空值
incrementalMA = inSession ? cumulativeClose / barCount : na

// 绘制均线
plot(incrementalMA, color=color.new(color.blue, 0), linewidth=2, title="递增均线")

代码解释

  • 时段判断:通过timestamp将IST时间转换为UTC时间(适配TradingView时区逻辑),用time函数精准锁定目标窗口。
  • 累计求和:ta.sum(close, ta.barssince(not inSession) + 1) 会从时段第一根K线开始,持续累计到当前的收盘价总和,ta.barssince用于统计时段内已生成的K线数量。
  • 均值计算:用累计和除以当前时段内的K线数量,得到实时更新的递增平均;非时段内返回na,避免绘制无效数据。

简化版写法(需匹配图表时区)

如果图表已设置为印度标准时间(IST),可以直接通过小时和分钟判断,代码更简洁:

//@version=5
indicator("IST 15:00-15:30 递增均线(简化版)", overlay=true)

// 直接判断IST时间窗口
inSession = hour == 15 and minute >= 0 and minute < 30

// 累计求和与计数
cumulativeClose = ta.sum(close, ta.barssince(not inSession) + 1)
barCount = ta.barssince(not inSession) + 1
incrementalMA = inSession ? cumulativeClose / barCount : na

plot(incrementalMA, color=color.red, linewidth=2)

内容的提问来源于stack exchange,提问作者Lokesh Giri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 07:10:28