Anatomy of a JForex strategy, Part 2

We looked at four of the six methods in the IStrategy Interface in a previous post. The last two methods, onTick and onBar, is where your strategy connect with market data. Either one, or both, of these methods is where you put your trading algorithm in. Your strategy would then be able to process the market data as they arrive one tick/bar at a time. Recall that IStrategy Interface is the skeleton of your strategy. And that IContext object is the heart of your strategy. onTick/onBar is the head of your strategy, which contains your trading algorithm, which is the brain.

onTick

Here's the method definition of onTick.

onTick(Instrument instrument, ITick tick) throws JFException { }

Important:onTick is called for each and every instrument that your JForex platform is subscribed to (the instrument list in your workspace box). Let me say that again, onTick is called for each and every instrument that your JForex platform is subscribed to. The standard practice is to filter out ticks for instruments that you don't want with a simple IF-return statement. if (instrument != myInstrument) return; Actual tick data is passed to your strategy using the ITick object from the onTick method's parameter. Take a look at the ITick javadoc entry to see what it offers.

onBar

public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { }

onBar works in a similar manner to onTick. In which onBar is called for each and every subsribed instrument and period known to JForex. Similarly, you have to filter out all the unwanted instruments and periods or else there will be expected results from your strategy. Another point to note is that onBar provides both a IBar askBar and IBar bidBar, representing the ask and bid bars. Question: What happens when two or more periods overlap as in 13:45? 1, 5, and 15-minutes bars are all arriving at the same time (not to mention the periods in seconds too). Answer: According to Dukascopy Support in the forum, "They come in a strict order, for example (1min 1min 1min 1min 1min 5min 1min 1min 1min 1min 1min 5min ..) They come in cycles, where smaller periods comes first."

JForex Support Forum

As you program your strategy with JForex, you will no doubt come up with questions of your own. The best place to ask is at the official JForex Support Forum. This is the last of the three essential JForex resources that I alluded to earlier. Even if you don't have any specific question, there are sample codes, coding discussion, and hundreds of existing Q&A from other JForex developers posted in the forum.

Summary

The discussion so far has been very high level. To show you what you can actually do in an IStrategy, we will dissect a working strategy in the next post. And what else better to examine than the most popular JForex strategy of them all -- MA_Play.java.