Skip to content

Instantly share code, notes, and snippets.

@Quantisan
Created June 18, 2012 21:31
Show Gist options
  • Save Quantisan/2950849 to your computer and use it in GitHub Desktop.
Save Quantisan/2950849 to your computer and use it in GitHub Desktop.
JForex risk manager 1.0
/*
RiskManager.java
version 1.0
Copyright 2010 Quantisan.com
*/
package jforex;
import java.math.*;
import com.dukascopy.api.*;
public class RiskManager implements IStrategy {
private IEngine engine;
private IConsole console;
private IContext context;
private double maxEquity = 0;
private double[] lot = new double[Instrument.values().length];
@Configurable("Max. Drawdown Percentage [2%]")
public double riskPct = 2.0;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.context = context;
}
public void onAccount(IAccount account) throws JFException {
double equity, lossPct;
equity = account.getEquity();
if (equity > maxEquity) {
maxEquity = equity;
return;
}
lossPct = (equity - this.maxEquity) / this.maxEquity * 100.0;
if (lossPct < (-1.0 * riskPct)) {
console.getOut().println("Max. drawdown reached! Stopping strategy!");
this.context.stop();
}
}
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException {
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment