简体   繁体   English

Pine Script:不同时间框架上的 SMA

[英]Pine Script: SMAs on different timeframes

Assume I'm on the 4h chart.假设我在 4 小时图表上。 What I would like to do is to calculate a few SMAs on the daily chart and plot them in the 4h chart.我想做的是在日线图上计算一些 SMA,在 4 小时图上计算 plot。 For example, I would like to calculate the 100 day SMA and draw it on the 4h chart.例如,我想计算 100 天 SMA 并将其绘制在 4 小时图上。 I would also like to do this for the 200 day SMA.我也想为 200 天 SMA 做这个。 Etc.等等。

I know how to do this using request.security() (see "direct variant" in the code snippet below).我知道如何使用request.security()来做到这一点(请参阅下面代码片段中的“直接变体”)。 However, if I want to plot 5 different SMAs, this would mean that I have to call request.security() 5 times.但是,如果我想 plot 5 个不同的 SMA,这意味着我必须调用request.security() 5 次。 To me, this seems inefficient.对我来说,这似乎效率低下。 I would rather like to call request.security() once to obtain the daily data and then calculate the SMAs on that.我宁愿调用request.security()一次来获取每日数据,然后计算 SMAs。 However, this doesn't seem to work.但是,这似乎不起作用。 See this snippet:看到这个片段:

//@version=5
indicator(title="test", shorttitle="test", overlay=true)

// initialize
smaPeriod = 1400

// direct variant
twoHundreedWeekSma = ta.sma(close, smaPeriod)
dailyChart200WSmaDirect = request.security(syminfo.tickerid, "D", twoHundreedWeekSma) 

// indirect variant
dailyChartClose = request.security(syminfo.tickerid, "D", close)
dailyChart200WSmaIndirect = ta.sma(dailyChartClose, smaPeriod)

// plotting
plot(dailyChart200WSmaDirect, color = color.red) // <- this is correct
plot(dailyChart200WSmaIndirect, color = color.blue) // <- this is wrong. It is the same as if I did plot(ta.sma(close, smaPeriod), color = color.yellow)

我不应该看到两条不同的线,而是两条线重叠在一起

The "indirect variant" appears to have no effect. “间接变体”似乎没有效果。 I get the same results if I just calculate the desired SMA on the current 4h close values (ie, if I just did ta.sma(close, smaPeriod) ).如果我只计算当前 4h 收盘值的所需 SMA(即,如果我只是做了ta.sma(close, smaPeriod) ),我会得到相同的结果。 What am I not understanding?我不明白什么?

You can use tuples and combine multiple expressions for the same timeframe.您可以使用元组并在同一时间范围内组合多个表达式。

sma_50 = ta.sma(close, 50)
sma_100 = ta.sma(close, 100)
sma_200 = ta.sma(close, 200)

[sma_daily_50, sma_daily,100, sma_daily_200] = request.security(syminfo.tickerid, "D", [sma_50, sma_100, sma_200])

plot(sma_daily_50, color = color.green)
plot(sma_daily_100, color = color.yellow)
plot(sma_daily_200, color = color.red)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM