Introduction

Previous related articles:
-Creating your first strategy.

Finally we begin to structurate our strategy. For this introduction, our objective is to create a strategy for automated trading where we can use any indicator available. We begin with simple concepts and later we add some more functionalities after implementing the last ones, for example, adding money manegement, send email messages on entry and exit, and eventually what you will suggest in comments. Also we try to explain how to backtest and do some tunning on the strategy itself. To not  fill the articles with repeated code, i show you on next image a empty  strategy with the main areas identified on this image. I will provide the code you need to insert on each of this areas. I will create a thread on forum to provide complete code. So, let's begin.


Image 1: The strategy base structure with the identified areas where we write the code excerpts presented in this article and the next ones.


The needed JForex API "objects"

To access the Dukascopy stream data servers and interact with the client platform or the api we need to create the objects who are responsible for receiving this data and to interact with them. This objects are defined on the JForex API and and we must declare/initialize them to do something useful with the api. The api have a huge number of objects, but for start i concentrate only on the principal ones. This objects are: IContext, IEngine, IAccount, IIndicators, IOrder, IHistory and IConsole. Each one being responsible for:
  • IContext : this is the main object where we need to go to access and get all parts of JForex. Is with this "interface" we can get account info, get/set subscribed instruments, set timeframe, access to (modify/create) screen areas, access to other interfaces i talk below. You can see javadoc about it here.
  • IEngine : This interface allow us to submit, merge and close orders, get all IOrder objects, get in what environment we are (DEMO, LIVE, TEST) and others. Link
  • IAccount : with this interface we can get informations about the account in use: account id, equitity, leverage defined limit, stop loss account limit, and others. Link
  • IIndicators : With this interface we have access to all JForex indicators and we need it to work with any indicator we need, get information about the indicator we use, the parameters needed for it, get the type and values returned by the indicator among others. Link.
  • IOrder : IOrder allow us to know everything about any initiated IOrder object. We can get all information about our order (the price, position, SL/TP/TS, profit in currency value or pips etc), set/change  SL/TP/TS, his state, close all/part of the order, etc. Link.
  • IHistory : Historical (past) tick, bar, feed and order data is accessed with this interface. Link.
  • IConsole : With IConsole interface we can write messages on a "messages" bottom tab of JForex. It's very useful for debugging, show any strategy activity we define and show any errors or warnings.  
So, let's codding them! Don't strange my variable names... On the strategies i have seen usually see the default names, but i like to use my names to avoid conflicting names. The first thing we need is to "create" or "alocate" space for this variables (correctly named variable declaration). Some, we don't need them for now, but we create them anyway. You must write this on AREA 2 (see first image) for this variables be available on all strategy. See image below.

Image 2: Declaration of needed objects (you need to write this on AREA 2).

After this, we go to fill the variables with the respective objects. We do this, initializing this variables on the onStart method/function to be associated with the correspondent objects at the start of the strategy. The code below must be writed on AREA 3.

Image 3: Variable initialization on the onStart method/function. You must write this on AREA 3.

With this "standard" code we are ready to begin "our" code. It's easy to see if everything is ok, for that you  just write the following lines od code after the code on last image:

[CODE]
...
myConsole.getOut().println("I'm a live strategy!"); //get the streamout and print the text
myContext.stop(); //stop the strategy
...
[END CODE]

Running the strategy we get a botton tab named "MyFirstStrategy" with the text inside quotes above. Well, this don't is our objective, so we must go on (don't forget to remove this two lines from our code)...

Configurable parameters for our strategy

For our strategy to do something about trades we must provide the information about what pair i want to trade, the time frame, the position value, SL value, TP value and others if needed. For that we need to say to our strategy what are the places to store this values and put them on each one. We want to have the choice to alter this values on strategy start, that's why we use the @Configurable annotation. For that we must write the following code in AREA 2 to be available to all strategy (the order, related to last code we put there don't is important, can be after or before). The variable names are self explanatory i don't loose time explaining them.

Image 4: Part of needed variables declaration and initialization with predefined values we can change at strategy start. Write it on AREA 2.

Other variables

The previous variables are related with the currency pair, time frame use and the orders this strategy possibly will create. But we need more. Suposing we can only have one order open at any time, we need to create a variable where the strategy knows if it's possible to open a trade and other for order direction (buy or sell). Also we need some parameters for the indicators period, his limit values or triggers. Here we use some generic variables and later when we decide the indicator or indicators we want to use we must redefine them to match the needs for this indicator/s. Also here, i'm suggesting fixed value variables, but as we see on the last example about variables, we can use the @Configurable annotation to change them at strategy start.

Image 5: Some other variables needed for order direction, order permission and indicator levels. The link present on image for easiest access click here. This code can be writed after last one on AREA 2.

Other common code

Until now we are doing code that can be used for any automated trategy if it meet the criteria we need. The exception will be the last three variables on last image. But we have more common code we can use on every strategy we create. One example is the label of the orders if we want to define them, the buy and sell action. Usually we create a method for each of this parts. The code bellow must be writed on AREA 9.

 
Image 6: More common code that can be used on all strategies where this code meet the requirements. Must be placed at AREA 9.

On the first method/function we set the label for the orders submited. The code is self explanatory. We can do it with less lines of code, but less legible. If you want to the date on label string match your time zone you can replace the code "TimeZone.getTimeZone("GMT")" by "TimeZone.getDefault()". The other method/function is self explanatory too, the objective of it is to submit the orders when called with the correct parameters. The only thing less usual is the waiting for the order state to be completed.

Also you need to place some new "imports" at the beginning of the code on AREA 1 if your Java editor don't write them automaticaly. This new "imports" are to be placed after the one present on last examples are on the next image.

Image 7: We need to add the code above at the beginning of our code in AREA 1.

Final words

We are almost there! In next article i hope to finish this basic strategy articles with examples for some indicators. In it i will try to explain how to trigger entries using different methods (crossover, greater than, less than, interval). If you don't have the time or patience to write down this code available on this articles, i'm a nice guy and i provide this code in a java file you can use to complete with whatever you want. You can get it on the foruns using this link here. This code is not complete, it's only the code present on last articles and in this, all together. On next article the code can be changed if needed, but if this occurs i will provide updated code.

I hope you like this articles series, and as always, comments, suggestions, PM are welcome.

Trade well and prospers on your way.

JL