Here is a snippet of a Java program that calculates the Stochastic Momentum Indicator.
Not included is the use of the Overbought/Oversold value.
For references see:
https://www.tradingview.com/script/HLbqdCku-Stochastic-Momentum-Index-SMI/
https://www.dailyfx.com/forex/education/trading_tips/daily_trading_lesson/2013/04/11/StochasticMomentumIndex.html
public double[] SMI;
public double[] Signal;
public StochasticMomentum(double high[], double low[], double close[],
int hiLowPeriod, MAType mat, int maPeriod, int smoothPeriod, int signalPeriod) {
final double cm[] = new double[close.length];
final double hl[] = new double[close.length];
for (int i1 = 0; i1 <= high.length - hiLowPeriod; i1++) {
double hh = -1e9;
double ll = 1e9;
for (int i2 = 0; i2 < hiLowPeriod; i2++) {
if (hh < high[i1 + i2]) {
hh = high[i1 + i2];
}
if (ll > low[i1 + i2]) {
ll = low[i1 + i2];
}
}
final double m = (hh + ll) / 2;
cm[i1 + hiLowPeriod - 1] = close[i1 + hiLowPeriod - 1] - m;
hl[i1 + hiLowPeriod - 1] = hh - ll;
}
final Core core = new Core();
MInteger outBegIdx = new MInteger();
MInteger outNBElement = new MInteger();
core.movingAverage(0, cm.length - 1, cm, maPeriod, mat, outBegIdx, outNBElement, cm);
Realign.realign(cm, outBegIdx);
core.movingAverage(outBegIdx.value, cm.length - 1, cm, smoothPeriod, mat, outBegIdx, outNBElement, cm);
Realign.realign(cm, outBegIdx);
outBegIdx = new MInteger();
outNBElement = new MInteger();
core.movingAverage(0, hl.length - 1, hl, maPeriod, mat, outBegIdx, outNBElement, hl);
Realign.realign(hl, outBegIdx);
core.movingAverage(outBegIdx.value, hl.length - 1, hl, smoothPeriod, mat, outBegIdx, outNBElement, hl);
Realign.realign(hl, outBegIdx);
SMI = new double[hl.length];
for (int i1 = 0; i1 < hl.length; i1++) {
if (hl[i1] == 0) {
continue;
}
SMI[i1] = cm[i1] / (hl[i1] / 2) * 100;
}
outBegIdx = new MInteger();
outNBElement = new MInteger();
Signal = new double[hl.length];
core.movingAverage(0, hl.length - 1, SMI, signalPeriod, mat, outBegIdx, outNBElement, Signal);
Realign.realign(Signal, outBegIdx);
}
No comments:
Post a Comment