![](/img/trans.png)
[英]PineScript create a source with n Candles in future for forcasts in Tradingview
[英]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.