Not a member yet? Register now and get started.

lock and key

Sign in to your account.

Account Login

Forgot your password?

JForex API tutorial – part 1 – intro

02 Sep Posted by in Uncategorized | Comments

If you want to code trading bot for the JForex patform, JForex API is the solution. Ok, how to get this business started:

First, overall algorithm(quoted from Dukascopy Site).

  1. Open a Demo Account for developing and testing
  2. Run JForex Demo Platform
  3. Develop your strategy based on Java Documentation
  4. Test your strategy with the Historical Tester

Okay, now let’s focus on strategy creation with JForex API. Open account, download and run the Platform. Open Tools->Strategy Editor. Here you’ll see ready Java class – template for your strategy. Let’s see it closer:
1. This class implements IStrategy interface
2. Start-up code should be placed within onStart method.
3. Strategy should encapsulated in onTick or onBar method.

Quick example ) We will write robot submitting BUY order every hour with take profit level = 40 pips from entry && stop loss level = 100 pips from entry (entry is bid, in our case):
1. Add counter for order label to class variables
private int tagCounter = 0;
2. Add new method to class to generate label
protected String getLabel(Instrument instrument) {
String label =;
label = label.substring(0, 2) + label.substring(3, 5);
label = label + (tagCounter++);
label = label.toLowerCase();
return label;

3. Change onBar method contents to:
List orders = engine.getOrders();
double factor = instrument.getPipValue();
engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, 0.100, 0, 0, bidBar.getClose()-factor*100, bidBar.getClose()+factor*40);

That’s all! if(period==Period.ONE_HOUR) checks whether bar is 1 hour bar as all bars (from 10 seconds and so on) are sent to the method. Then we check if any order is not finished yet, if so, no order sent. As different pairs(for example, EUR/USD and EUR/JPY) have different pip value, we get it with instrument.getPipValue()call. engine.submitOrder method submits order with chosen label, currency pair(instrument), command(IEngine.OrderCommand.BUY), volume(0.100 – in millions), stop loss level(bidBar.getClose()-factor*100) and take profit level (bidBar.getClose()+factor*40).

Now time to test! Rename java class from Strategy to RandomStrategy, for example. Save it. Select Tools->Historical Tester in menu. Choose in Stragey dropdown list, set historical period and desirable options(I advise to spend some time on playing with them – it’s quite interesting). Press start. See report in the end.

For August, 2011 and GBP/USD my result is:

Initial deposit 50000
Finish deposit 48025.24
Turnover in USD 16520057
Comission in USD 379.56

Result is negative. It’s not a surprise with such a stupid strategy! ) Next time I’ll show strategy that could be profitable… sometimes )


Leave a comment