Greetings to everybody.
Recently when I visited blog of Paul Lam, I've noticed JFUtil 2.0 alpha demonstration of usage of his library JForex-Utilities.
It is nice example, but very unformatted. It looks like this:
So I've made formatting corrections, and put his sample on my page with formatting:
package jforex.yz;
import java.util. * ;
import com.dukascopy.api. * ;
import com.quantisan.JFUtil. * ;
@
RequiresFullAccess@Library("d:\\Users\\Administrator\\JForex\\files\\JFQuantisan.jar")
public class JfutilDemo implements IStrategy {
private int counter = new Random().nextInt(100); // notice the lack of fields to manage JForex objects
@Override public void onStart(IContext context) throws JFException {
// ** Essential steps **
// must initialize objects once and for all JForexContext.setContext(context);
JForexAccount.setAccount(context.getAccount());
Set < Instrument > set = new HashSet < Instrument > (context.getSubscribedInstruments());
set = context.getSubscribedInstruments(); // get list of subscribed instruments
// subscribe to transitional instruments for currency conversion calculations
Pairer.subscribeTransitionalInstruments(set);
// \ * \ * End of essential steps\ * \ *
Printer.println("-- Quantisan.com JFUtil v2 .0 alpha: Usage demo--");
Printer.println(" ");
}
@Override public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (period != Period.TEN_SECS) {
return; // only run every 10 sec.
}
// *** 1. access IContext and IAccount from anywhere
Printer.println("Account equity = " + JForexAccount.getEquity());
// get an EMA indicator value
double ema = JForexContext.getIndicators().ema(instrument, Period.TEN_SECS, OfferSide.BID, IIndicators.AppliedPrice.MEDIAN_PRICE, 14, 1);
Printer.println(instrument.toString() + " EMA = " + ema); // printing the EMA value
// *** 2. Profit/loss calculation to account currency before placing your order
// Demonstrating currency conversion
double risk = 100 * Pairer.convertPipToAccountCurrency(instrument);
String symbol = JForexAccount.getAccountCurrency().getSymbol();
Printer.println(symbol + risk + " risked in for 1,000 units and 100 pips move in " + instrument.toString());
// ** 3. Simplify order parameters with order ticket builder
// Demonstrating trade ordering
String label = instrument.toString().substring(0, 2) + counter;
OrderTicket mktBuyTicket = new OrderTicket.Builder(label, //setting required ticket info instrument,
instrument, IEngine.OrderCommand.BUY, 0.1).build(); // build ticket
Orderer.placeOrder(mktBuyTicket); // placing order
// market buy order with a 100 pips stop and 100 pips target
double stopPrice = JForexContext.getPrice(instrument) - (100 * instrument.getPipValue());
double targetPrice = JForexContext.getPrice(instrument) + (100 * instrument.getPipValue());
label = instrument.toString().substring(0, 2) + counter;
OrderTicket buySpTicket = new OrderTicket.Builder(label, instrument, IEngine.OrderCommand.BUY, 0.1).
setStopLossPrice(stopPrice). // set stop price to
setTakeProfitPrice(targetPrice) // set target
.build();
// ** 4. Single method to placing orders for all order types and parameters\ * \ * \ *
Orderer.placeOrder(buySpTicket);
}
@Override public void onAccount(IAccount account) throws JFException {
JForexAccount.setAccount(account); // update IAccount to latest
}
@Override public void onStop() throws JFException {
for (IOrder order: Orderer.getOrders()) {
// close all orders
Orderer.close(order);
}
}
public void onMessage(IMessage message) throws JFException {}
public void onTick(Instrument instrument, ITick tick) throws JFException {}
}
As I say in such situations, feel the difference.
BTW, if you need JFQuantisan.jar you can download it.