如何在单个脚本中为不同指标设置多时间周期?
实现方案
要让ZLSMA使用15分钟周期计算,同时AO、HVI保持5分钟周期,且图表以5分钟展示,核心是用TradingView的request.security()函数跨周期获取数据。以下是修改后的完整代码及说明:
修改后的完整脚本
indicator(title='Scalp BTC M5', timeframe="5") // 第一个指标:ZLSMA(15分钟周期) length = input(title='Length', defval=50) offset = input(title='Offset', defval=0) // 跨周期获取15分钟的收盘价作为ZLSMA的数据源 src = request.security(syminfo.tickerid, "15", close) lsma = ta.linreg(src, length, offset) lsma2 = ta.linreg(lsma, length, offset) eq = lsma - lsma2 zlsma = lsma + eq plot(zlsma, color=color.new(color.yellow, 0), linewidth=3) // 第二个指标:AO(5分钟周期,保持原逻辑) ao_fast = input(5, 'AO Fast EMA Length') ao_slow = input(34, 'AO Slow EMA Length') ao = ta.ema(hl2, ao_fast) - ta.ema(hl2, ao_slow) color_ao = ta.change(ao) > 0 ? #115500 : #992211 plot(ao, style=plot.style_columns, color=color_ao, transp=0) // 第三个指标:HVI(5分钟周期,保持原逻辑) len = input(10) HV = ta.highest(volume, len) HVI = volume * 100 / HV[1] plot(HVI, color=color.new(color.black, 0), title='HVI') band1 = hline(100) band0 = hline(50) fill(band1, band0, color=color.new(color.purple, 90))
关键修改说明
- 图表周期维持:脚本开头的
indicator(timeframe="5")保持不变,确保图表以5分钟周期展示所有指标。 - ZLSMA跨周期计算:通过
request.security(syminfo.tickerid, "15", close)获取当前标的15分钟周期的收盘价,以此为基础计算ZLSMA,最终得到的15分钟周期ZLSMA值会同步显示在5分钟图表的对应K线上(一个15分钟K线对应3个5分钟K线,所以ZLSMA值会连续重复3次)。 - AO、HVI保留原逻辑:这两个指标直接使用5分钟周期的
hl2(高低价均值)和volume数据计算,无需修改。
内容的提问来源于stack exchange,提问作者user19748509
相关产品推荐
相关产品推荐

