I use the following in my strategy... I only recently wrote this myself so it's possible that it's not fool-proof or perhaps even just wrong.
private double CalculateOrderAmount(Instrument instrument, double closePrice, double riskPercentage, double stopLossPips) {
double amountToUseTemp = this.account.getBalance() * riskPercentage; // 10000 * 0.01 = 100
double amountToUseTemp2 = amountToUseTemp / stopLossPips; // 100 / 200 = 0.5
double amountToUseTemp3 = amountToUseTemp2 * 10000; // 0.5 / 0.0001 = 5000
double amountToUse = amountToUseTemp3;
if (instrument.getSecondaryCurrency().getCurrencyCode() != "USD") {
int mod = 1;
if (instrument.getSecondaryCurrency().getCurrencyCode() == "JPY")
mod = 100;
if (instrument.getPrimaryCurrency().getCurrencyCode() == "USD") {
amountToUse /= (closePrice / mod);
} else {
for (Instrument ins : this.latestConversions.keySet()) {
if (ins.getPrimaryCurrency() == instrument.getSecondaryCurrency() && ins.getSecondaryCurrency().getCurrencyCode() == "USD") {
amountToUse /= this.latestConversions.get(ins);
} else if (ins.getSecondaryCurrency() == instrument.getSecondaryCurrency() && ins.getPrimaryCurrency().getCurrencyCode() == "USD") {
amountToUse /= (1 / this.latestConversions.get(ins));
}
}
}
}
double correctedAmount = amountToUse / 1000000; // 0.005
return correctedAmount;
}