PineScript 在 Tradingview 中计算未来一根蜡烛的 EMA [英] PineScript calculate EMA for One Candle in Future in Tradingview

查看:235
本文介绍了PineScript 在 Tradingview 中计算未来一根蜡烛的 EMA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to calculate an exponential-moving-average (EMA) with the EMA for one Candle in the future just by duplicating the last candle.

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

Means I want to plot an EMA with an offset of 1 and the value, that is in the offset 1 bar is calculated based on the current candle.

不幸的是,我认为我对该系列有错误的理解,为什么它不起作用.但希望我的代码显示了我想要做的:

Unfortunately I think I have a wrong understanding of the series, why it doesn't work. But hopefully my code shows what I wanted to do:

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)

非常希望得到您的帮助.

Hope a lot on your help.

提前致谢

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

Maybe the following Picture shows what I want to do:

图片

推荐答案

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

See How Is the Exponential Moving Average (EMA) Formula Calculated? for the formula used.
It's based on yesterday's ema, and uses the current price to calculate today's ema.
When we consider the latest bar's ema as yesterday's ema, then we simply have to use the latest bar's price as today's price to calculate the new 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

Code update to show custom ema only on last bar

//@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)

这篇关于PineScript 在 Tradingview 中计算未来一根蜡烛的 EMA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆