繁体   English   中英

PineScript 在 Tradingview 中计算未来一根蜡烛的 EMA

[英]PineScript calculate EMA for One Candle in Future in Tradingview

我想通过复制最后一根蜡烛来计算未来一根蜡烛的指数移动平均线 (EMA)。

意味着我想绘制一个偏移量为 1 的 EMA,而偏移量 1 柱中的值是根据当前蜡烛图计算的。

不幸的是,我认为我对这个系列有一个错误的理解,为什么它不起作用。 但希望我的代码显示了我想做的事情:

CustomEma(source, length) =>
alpha = 2 / (length + 1)
ema = 0.0
// iterate through the length e.g. calculate with a length of 20
for i = 1 to length
    y=length-i
    //now calculate the ema for bar 21,20,19 until 1 based on source from 20 to 0
    ema[y+1] = alpha * source[y] + (1 - alpha) * nz(ema[y+1])
//now calculate the last EMA by duplicating the source 0
ema[0]= alpha * source[0] + (1 - alpha) * nz(ema[1])
ema

esaF = CustomEma(close, 20)
plot(esaF , color=color.white,offset=1)

非常希望得到您的帮助。

提前致谢

也许下图显示了我想要做的事情:

图片

查看指数移动平均线 (EMA) 公式是如何计算的? 对于使用的公式。
它基于昨天的ema ,并使用当前价格来计算今天的ema
当我们将最新柱的ema视为昨天的ema ,我们只需使用最新柱的价格作为今天的价格来计算新的ema

//@version=4
study("Custom EMA", "cema", overlay=true)

// Source: https://www.investopedia.com/ask/answers/122314/what-exponential-moving-average-ema-formula-and-how-ema-calculated.asp
// EMA = Price(t)×k + EMA(y)×(1−k)
// where:
// t = today
// y = yesterday
// N = number of days in EMA
// k = 2÷(N+1)
//
// In our case, the 'yesterday' ema is the current ema for the latest candle.
// The 'today' ema is calculated using that 'yesterday' ema as 'previous' ema.
// For the 'today' price we take the latest candle (which is essentially a 'copy' of the last bar).
// When we plug that into the formula, we get the new ema.

custom_ema(src, length) =>
    k = 2 / (length + 1)
    new_ema = (src * k) + (ema(src, length) * (1 - k))
    
esaF = custom_ema(close, 20)
plot(esaF , color=color.white,offset=1)

编辑:代码更新以仅在最后一个栏上显示自定义 ema

//@version=4
study("Custom EMA", "cema", overlay=true)

// Source: https://www.investopedia.com/ask/answers/122314/what-exponential-moving-average-ema-formula-and-how-ema-calculated.asp
// EMA = Price(t)×k + EMA(y)×(1−k)
// where:
// t = today
// y = yesterday
// N = number of days in EMA
// k = 2÷(N+1)
//
// In our case, the 'yesterday' ema is the current ema for the latest candle.
// The 'today' ema is calculated using that 'yesterday' ema as 'previous' ema.
// For the 'today' price we take the latest candle (which is essentially a 'copy' of the last bar).
// When we plug that into the formula, we get the new ema.

var float cema = na
var float rema = na
var float mema = na


custom_ema(src, length) =>
    k = 2 / (length + 1)
    new_ema = (src * k) + (ema(src, length) * (1 - k))

real_ema(src, length) => ema(src, length)

mixed_ema(src, length) =>
    if barstate.isconfirmed
        rema
    else
        cema

cema := custom_ema(close, 20)
rema := real_ema(close, 20)
mema := mixed_ema(close, 20)
    
plot(cema, "custom ema", color=color.white)
plot(rema, "real ema",   color=color.green)
plot(mema, "mixed ema",  color=color.red)

暂无
暂无

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

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