public
Created

MACD calculator. Not a standalone Java program but some Java code as good as pseudocode.

  • Download Gist
MACDCalculator.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import java.util.ArrayList;
 
public class MACDCalculator extends SimpleCalculator {
 
private float ema1alpha;
private float ema2alpha;
private float signalalpha;
private float[] pema1;
private float[] pema2;
private float[] psignal;
 
MACDCalculator(ArrayList<Float> args) {
this.ema1alpha = args.get(0);
this.ema2alpha = args.get(1);
this.signalalpha = args.get(2);
this.pema1 = null;
this.pema2 = null;
this.psignal = null;
}
 
@Override
public float[] calculate(float[] values) {
float[] res;
if (this.pema1 == null){
this.pema1 = values.clone();
this.pema2 = values.clone();
this.psignal = values.clone();
res = values.clone();
}
else{
res = new float[values.length];
for (int i = 0; i < values.length; i++){
this.pema1[i] = this.ema1alpha * values[i] + (1-this.ema1alpha) * this.pema1[i];
this.pema2[i] = this.ema2alpha * values[i] + (1-this.ema2alpha) * this.pema2[i];
this.psignal[i] = this.signalalpha * (this.pema1[i]-this.pema2[i]) + (1-this.signalalpha) * this.psignal[i];
res[i] = this.pema1[i] - this.pema2[i] - this.psignal[i];
}
}
return res;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.