📄 rsi.java
字号:
package com.jsystemtrader.indicator;
import com.jsystemtrader.platform.*;
/**
* Relative Strength Index.
*/
public class RSI extends Indicator {
private final int periodLength;
public RSI(long time, QuoteHistory qh, int periodLength) {
super(time, qh);
this.periodLength = periodLength;
}
public double calculate() throws IndicatorCalculationException {
int lastBar = qh.size() - 1;
int firstBar = lastBar - periodLength + 1;
if (firstBar < 0) {
String msg = "Quote history length " + qh.size() + " is insufficient to calculate the indicator.";
throw new IndicatorCalculationException(msg);
}
double aveGain = 0, aveLoss = 0;
for (int bar = firstBar + 1; bar <= lastBar; bar++) {
double change = qh.getPriceBar(bar).getClose() - qh.getPriceBar(bar - 1).getClose();
if (change >= 0) {
aveGain += change;
} else {
aveLoss += change;
}
}
double rs = aveGain / Math.abs(aveLoss);
double rsi = 100 - 100 / (1 + rs);
value = rsi;
return value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -