Conversion Utils
JFUtils interface provides methods for conversion of amounts between different instruments. There are two methods available:
- convert - converts the amount from one instrument to another.
- convertPipToCurrency - converts the cost of one pip for particular instrument to specified currency.
Note: The functionality is available with JForex-API version 2.6.49.
Get exchange rate between any currencies
This is code example that shows how to get exchange rate between any currencies.
public void onStart(IContext context) throws JFException {
this.console = context.getConsole();
this.utils = context.getUtils();
double rate = utils.getRate(JFCurrency.getInstance("JPY"), JFCurrency.getInstance("CHF"));
console.getOut().println(rate);
context.stop();
}
Conversion between instruments
Consider converting 0.1 of EUR/USD to AUD/CAD.
public void onStart(IContext context) throws JFException {
JFUtils utils = context.getUtils();
double amount1 = utils.convert(Instrument.EURUSD, Instrument.AUDCAD, 0.1);
}
Note that in order to retrieve the prices:
- both EUR/USD and AUD/CAD have to be subscribed,
- at least one of the instruments has to be subscribed in order to retrieve the prices: EUR/AUD, EUR/CAD, AUD/USD, USD/CAD.
There are two additional overload methods available where one can set result precision and OfferSide of the price.
Conversion of pip cost
Consider retrieving pip price of AUD/CAD in your account currency.
public void onStart(IContext context) throws JFException {
JFUtils utils = context.getUtils();
double amount2 = utils.convertPipToCurrency(Instrument.AUDCAD, context.getAccount().getCurrency());
}
Note that in order to retrieve the prices:
- AUD/CAD has to be subscribed,
- Assuming that account currency is USD, one of the two must be the case:
- either AUD/USD or USD/CAD has to subscribed
- or there has to be an instrument x such that x/USD is subscribed and either x/AUD or x/CAD is subscribed.
There is a an overload method available where one can set OfferSide of the price.
Conversion of pip cost - multiple currencies and instruments
Consider a strategy which demonstrates pip cost conversion between multiple instruments and multiple currencies. The strategy works with:
- set of instruments
- set of currencies
It finds pip value of each instrument in each currency. It also prints instrument -> currency rate of each instrument -> currency pair.
private final Set<ICurrency> currencies = new HashSet<>(Arrays.asList(new ICurrency[] {
JFCurrency.getInstance("JPY"),
JFCurrency.getInstance("CHF"),
JFCurrency.getInstance("USD"),
JFCurrency.getInstance("EUR")
}));
private final Set<Instrument> instruments = new HashSet<>(Arrays.asList(new Instrument[] {
Instrument.CHFJPY,
Instrument.EURJPY,
Instrument.EURUSD,
Instrument.USDJPY
}));
public void onStart(IContext context) throws JFException {
this.console = context.getConsole();
this.utils = context.getUtils();
context.setSubscribedInstruments(instruments);
// wait max 1 second for the instruments to get subscribed
int i = 10;
while (!context.getSubscribedInstruments().containsAll(instruments)) {
try {
console.getOut().println("Instruments not subscribed yet " + i);
Thread.sleep(100);
} catch (InterruptedException e) {
console.getOut().println(e.getMessage());
}
i--;
}
//multiple conversion example
for (Instrument instrument : instruments) {
for (ICurrency currency : currencies) {
try {
double pipValue = utils.convertPipToCurrency(instrument, currency, null);
print("%s -> %s, pip value=%.8f (%s/%s rate: %.8f, inverted rate: %.8f)",
instrument, currency, pipValue,
instrument.getSecondaryJFCurrency(), currency,
pipValue / instrument.getPipValue(), (1/pipValue * instrument.getPipValue()));
} catch (JFException e) {
console.getErr().println(String.format("%s->%s conversion failed: %s", instrument, currency, e));
}
}
}
context.stop();
}