Skip to content

Instantly share code, notes, and snippets.

@trylks
Created January 31, 2012 12:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trylks/1710144 to your computer and use it in GitHub Desktop.
Save trylks/1710144 to your computer and use it in GitHub Desktop.
MACD calculator. Not a standalone Java program but some Java code as good as pseudocode.
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment