📄 ema.java
字号:
package com.jsystemtrader.indicator;
import com.jsystemtrader.platform.*;
/**
* Exponential Moving Average.
*/
public class EMA extends Indicator {
private final int length;
private final double multiplier;
private final int endBar;
public EMA(long time, QuoteHistory qh, int length, int endBar) {
super(time, qh);
this.length = length;
multiplier = 2. / (length + 1.);
this.endBar = endBar;
}
public EMA(long time, QuoteHistory qh, int length) {
this(time, qh, length, qh.size() - 1);
}
public double calculate() {
// Start bar will be the seed (the starting value of EMA). To allow
// the sufficient time for final value to settle to the true value,
// we start with a seed that is two times the EMA length away from
// the final bar.
int startBar = Math.max(endBar - 2 * length, 0);
double ema = qh.getPriceBar(startBar).getClose();
for (int bar = startBar; bar <= endBar; bar++) {
double barClose = qh.getPriceBar(bar).getClose();
ema += (barClose - ema) * multiplier;
}
value = ema;
return value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -