Anatomy of a JForex strategy, Part 1

Continuing on from Part 1 of this series: Getting started learning JForex programming, now we're ready to discuss the real thing. You build JForex strategies by using the IStrategy Interface (What is an Interface?). Basically, an Interface is a code skeleton with a set of predefined empty methods that you'll need to implement yourself. The six standard methods of the IStrategy Interface are:

  1. onStart
  2. onStop
  3. onMessage
  4. onAccount
  5. onTick
  6. onBar

Below is an empty IStrategy Interface implementation, also known as a JForex strategy. This code will compile fine in JForex and you can even run it. But it doesn't do anything at all because there is no code to run in each of the methods. Each of the six methods will just be called and exit immediately.

import com.dukascopy.api.\*; 
public class EmptyStrategy implements IStrategy { 
    public void onStart(IContext context) throws JFException { } 
    public void onTick(Instrument instrument, ITick tick) throws JFException { } 
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
        throws JFException { } 
    public void onMessage(IMessage message) throws JFException { } 
    public void onAccount(IAccount account) throws JFException { } 
    public void onStop() throws JFException { } 

Each of the method is triggered by a specific event. You can probably guess what they are from their name.

onStart (line 5)

This is the first method that is called when you run your strategy. It will run once and only once at the start of your strategy. Normally you do your initialization in here. The thing to note for onStart is in line 5 of the code. The method signature of onStart is

public void onStart(IContext context) throws JFException

The object in the parameter and given to you in this method is an IContext object. If IStrategy is the skeleton, then IContext is the heart of the strategy. Please take a look at this javadoc link to IContext to see what this object does. Javadoc?: Now is a good time to introduce the second of the three essential resources of a JForex programmer. The JForex Javadoc is the single most up-to-date API documentation explaining each and every object and methods of the JForex API. Think of it like a reference manual. Note that although it's comprehensive, most of the explanation is very sparse and possibly incomplete. IContext is a core JForex object to access many important components of the JForex system, such as the ordering engine, charts, console, indicators... You get the idea. It is important! You typically want to keep a local copy of it as this is the only time (in onStart) that this object will be passed to you in IStrategy.

onStop (line 26)

As the name suggest, this method is called once you send a stop command to your strategy. You do your program wrap-up such as logging and flushing data here. Not much out of the ordinary with this one.

onMessage (line 18)

Whereas we know when onStart and onStop will be called, onMessage is an asynchronous method in that you don't know exactly when it will run. This method is called when the Dukascopy server sends your strategy a message. For example, the server calls onMessage to let you know that your order has been filled. You receive and process the server message by accessing the IMessage object that is passed to you. Important:There is no guarantee that you will receive each and every message sent to your strategy from the server. Perhaps your strategy process is clogged. Or maybe your internet connection had a hiccup. If your strategy onMessage doesn't get called by the server for whatever reason, the server couldn't care less and won't be checking nor trying again. So don't do anything critical like managing your orders in onMessage!

onAccount (line 22)

This method is called whenever your account information update is received. The method provides access to the IAccount object, which you use to get your account information. Say if you have an open position, your account information changes on every tick because your equity is cash + unrealized profit/loss. In that case, onAccount is called every 5 seconds by the server at most to avoid flooding your strategy. More Important:The IAccount object is not connected live to your account in the server. It is merely a snapshot of your account. For example, if you keep a local copy of an IAccount object. Do some trading to change your balance. Then ask the same IAccount for account balance information, you will not see a change. As such, always update your local copy of IAccount within the onAccount method to keep your account information up-to-date for your strategy's use.

To be continued

onStart, onStop, onMessage, and onAccount methods are administrative methods for your strategy. The last two methods that we'll discuss, onTick and onBar, is where the magic happens in a strategy. I am saving the best for last in the next post.