Suppose the account currency and the instrument being traded are unknown at compile time:
What is the simplest way to open a position with amount = K*equity ?
(where K is a given constant)
Since order amounts are specified in the instrument's primary currency, while account equity is provided in account currency, I am thinking of using this messy conversion:
private double getEquityInPrimaryCurrency(Instrument i) {
Currency pri = i.getPrimaryCurrency();
Currency acc = context.getAccount().getCurrency();
double r;
if(pri.equals(acc)){
r = 1;
} else {
String priCode = pri.getCurrencyCode();
String accCode = acc.getCurrencyCode();
Instrument ratio = Instrument.fromString(priCode + '/' + accCode);
try{
if(ratio != null){
r = 1/context.getHistory().getLastTick(ratio).getBid();
} else {
ratio = Instrument.fromString(accCode + '/' + priCode);
r = context.getHistory().getLastTick(ratio).getBid();
}
} catch (JFException ex){
console.getErr().println("Cannot determine ratio between " + priCode + " and " + accCode + ". Try subscribing to the relevant instrument.");
r = 0;
}
}
return context.getAccount().getEquity()*r;
}
And then to submit:
context.getEngine().submitOrder(label, instrument, orderCommand, K*getEquityInPrimaryCurrency(instrument))
But this is messy and sometimes would require subscription to an extra instrument. Is there in the API a better solution that I am missing?