问题
I want to calculate an exponential-moving-average (EMA) with the EMA for one Candle in the future just by duplicating the last candle.
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.
Thanks in advance
Maybe the following Picture shows what I want to do:
Picture
回答1:
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)
Edit: 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)
来源:https://stackoverflow.com/questions/64892606/pinescript-calculate-ema-for-one-candle-in-future-in-tradingview