As traders we need all tools we can get the hands on to obtain a “edge” over the market. If we are a Dukascopy Bank trader we have severall ways to implement our trading activity. We can access our accounts and do our trading activity in different ways: by web interface, by smart phones (iphone phones and now android phones too), by JForex Client, some other platforms builded using JForex API. Also we can do our activity with Metatrader bridge but don’t is our objective to talk about it. With this article we are trying to introduce the JForex API to you, the reader, as we use JForex Client ou other any JForex API based platform. With it we can implement tools, change the aspect, build indicators or, what you are probabily more interessed, strategies (for automated trading or to help us on manual trading). For that we need to learn Java.

Where i can learn Java?

Image 1:  Professor Mehran Sahami in a Stanford University class about java and programming methodology.

JForex Client and JForex API is developed using Java programming language and it's VM. To do anything useful with them, we need to know this programming language. Great number of us maybe don’t know about this programming language, but want to know about it. Maybe we don’t have the time to seach information or to find the useful information about it. I will provide some references i think useful to learn about Java programming language:

  • Programming methodology and Java video course: one of the best free courses i found in internet and available to anyone is presented by Stanford University in their YouTube channel. It’s a great course and worth the time spended to see it (28 class lessons with more than 40 minutes each one). If all teachers are as the one who gives this course certainly we have better schools and students. You can access this course here

  • Free book about Java: Thinking in Java by Bruce Eckel 3ª edition is a good learning book and you can get it here (4ª edition don’t is available for free).

  • Other great free book about Java and if you are a totally newcomer to computers in general this is book is for you: Java Notes 6ª edition by David J. Eck.

  • Java resources and examples: This site is well organized by category and with good examples to achieve what we need. You can access it here.
  • At last but not least the Oracle (the actual owner and developer of Java ) reference itself can be accessed here.

Of course this are only suggestions and you can find other sources of information by yourself, but they are a good start.  To work with Java usually we need the Java Development Kit (JDK for simplicity - you can get it here) and  use one Java editor. There are the “purists” who say you to use notepad on your learning path, but if we can have tools to make our life easiest why not to use them? The best tools to work with Java are Java IDE (Integrated Development Environment) software. JForex client have a good editor and is very usable, but for me, as i’m habituated to it, i prefer Netbeans or Eclipse and they are available to all operating systems where Java works. If you search Google you find them easilly.

Image 2: Java editors from right to left: JForex client Editor, Netbeans IDE and Eclipse IDE.

So, i know about Java, what i need more ?

With some of the needed knowledge and tools available we need to know where to find the information about JForex API. This information is only available officially at Dukascopy Site (and maybe on other sites where JForex is the theme):

  • Dukascopy wiki: great source of information about JForex API parts with good examples and use cases (examples of utilization). You can access it here.

  • JForex javadoc: Documentation generated by information provided on original code of the api with the information how to use specific API items. In Dukascopy site this information is always on date with javadoc for last version available. Also you get it in zip format inside the JForex API download file and you can read it directly or associate it with your project on the IDE editor you use. You can access it online here.

  • Dukascopy forum: Here you can get answers to problems encontered, doubts about the API or how to achieve something. Also you can ask the Support Team to build a strategy that match your specification but i think if you have the oportunity to build it yourself you will do it. The community and the Support Team are very responsive. You can access this forum here.

Yeah, yeah, i'm tired of reading... lets go for the fun part!

Yes, it's time to begin with... The first thing we need to do is to give a name to our strategy. This will be the name of the file where we create our strategy and the name of or "Class" (if you don't know what a class is... well, begin again in the first line of the article). For this example we consider this adequate name: MyFirstStrategy. We create the file with this name (make attention, the file name must match the name of our class name, it's case sensitive) with the extension ".java" and begin to build our strategy something like bellow:


public class MyFirstStrategy {


[End file]

Not hard heh ? Well, it's just the beginning. To connect to JForex API strategy parts we need to say to our "strategy" where it must go to find it. For that we add a line before the class declaration with the instructions for it: import com.dukascopy.api.*; . The asterisk after the "api." means we get access to all objects in the API. To build a strategy we must implement the abstract class "IStrategy" present on the API. For this, we add after the name of our class: ... implements IStrategy ... . This will say to use interface IStrategy of JForex API. This interface acts like a template for some needed methods or functions our class must have to work correctly as strategy. The next image we see all methods or functions required in our class.

Image 3: All functions needed by implemeting IStrategy interface on our class.

The needed methods are: onStart, onTick, onBar, onMessage, onAccount and onStop. Public keyword tell that method can be accessed everywhere and void keyword tell us that this method don't return anything. The words between the brackets after the name inform the parameters needed by that method in particular. Our code is implemented between the curly brackets after the parameters. The throws JFException just act as a security when something goes wrong on that method and is used to say us what type of problem we have (in a simple way). Bellow we talk about each method in particular:
  1. onStart(...)
  2. : Every time we run our strategy this is the first method executed. Is used to initialize objects and variables needed on our strategy based on received Icontext (the interface that allows interaction with the platform itself) and other variables or use other methods.
  3. onTick(..) : This method is executed every time a tick is received from data stream from Dukascopy. In this case the parameters are the instrument in use (for each one subscribed) and the Tick information itself. Usually we use this method to calculations on every tick received and do whatever we want with it.
  4. onBar(...) : This method is executed every time a bar is finished for each subscribed instrument. Also this method acts the last one but on bar completion. To work correctly it needs the instrument in use, the period of the bar, and the values for ask and bid bar objects where is filled with values of open, high, low and close price, the timestamp of the start of the bar and the volume value  that occurred on this bar.
  5. onMessage(...) : This method is executed on every message sent by the platform itself (the parameter). Most of the time is used to control the orders status and receive informations about when something occurs with and manage them. Also we can use it to get market news for example. See javadoc for more information.
  6. onAccount(...) : Is executed when something changes our account, change on equitity for example, get profit or loss of orders etc (javadoc and wiki are your friends).
  7. and finally onStop() : is executed when we stop our strategy. Can be used to do something we need at the end of the strategy: save configurations, print logs, close windows or objects etc. 
Final words

I don't have more space in this article to continue, so stay tunned for next articles where we build a strategy for automated trading (can be used on Strategy Contest) and all explanations needed to understand how to build a strategy for it.

I hope you like this article.

Trade well and good luck!


Translate to English Show original