In my last article i have informed you where you can get information to learn about Java, JForex API and start to introduce and explain some steps needed to build our strategy and his structure . In this article we are going deeper on this aspects. I'm thinking about a Strategy for automated trading that can be used for Strategy Contest, Demo Accounts trading but don't try it on Live trading (well, you can use, but i'm not responsible for any losses you get with it, but if you get big wins - really big - i ask you to remember me to give me at least the thanks, nothing more).

Going deeper...

Image 1: If You don't have read my last article, this image represents a functional but empty strategy.

On last image we have the beginning of our strategy, sligthy different of the presented on last article because i want to introduce to you some new concepts not present there:
  1. Package name: represents the localization of our strategy if we have a global project with more java files. Usualy match the base directory structure of our project. Working just with our strategy don't is needed.
  2. Import area: usually here we add all standard libraries needed by our strategy. Also after we can add a external library using Java annotations, but i will talk later about annotations in JForex.
  3. Gobal variables area: in this area we declare (and initialize if we want with default values) our global class variables. This means that this variables are available everywhere inside our class.
  4. Annotations: what i have to say about annotations deserve their space. I talk about it latter.
  5. Local method/function  variables: here we declare and initialize local variables. This variables only are available during the life of method/funcion. When it ends, the space on memory occupied by this variables will for Java garbage collector and this space is managed by this - you can think about it as a "recycle bin" of your computer and you clear it when you decide.
  6. Coding: here we have our code doing what we want for each of the specific method/function.
  7. Here we can create our needed methods/functions, classes and other specific code. Don't need to be at this specific location if we respect the curly brackets of other methods/functions and they are between the curly brackets of our strategy class. Note: this item is the last (5) - my mistake.

At first sight, we think that comments are useless on our learning path, they are a harassement and a loss of time (me included at my start in programming - i want to code not to comment). But from my experience they are really a thing we must to get used with them specially if are developing something more complex. I'm not a programmer but my activity is related with it and i do some programming for things i need, sometimes for my friends as a hobbie. As i don't have much time available, sometimes i leave some projects without touching them a long time. When i came to them, when i have comments on code it's easy to get in as this comments explain what i want to do and what the code does in generality and don't need to get again over the code. Also, this comments (if we follow the javadoc specification) we can have our code well documented and this code can be used by everyone without the need to analyse it. In Java, as in other languages, we comment our code the following way (you can see it in red in last image):
  • // (two slashes) : with this the compiler ignores all after including this slashes in the same line.
  • /*comment*/ : /* to begin the coment and */ to end it. The compiler ignores everything after including the /* until it finds the end */ (ignoring it too). This way we can have a multiline comments when needed and also we can place comments in the middle of code to clarify something.
Image 2: At top some code from JForex API - interface IStrategy and it's comments and below part of the resulting javadoc page generated by the comments on code. We can see how that comments are useful and important.


Annotation act like metadata for Java (in version 1.5 and later). With them we can do configuration directives, method and class transactions, version control, unit tests, validate something and even to do some action among others. We can use the default Java annotations or create or own annotations, but in this case we must create the code who is responsible to treat this annotation. In first image we have one example of default Java annotations with the "@override" annotation where we are saying we are rewriting some method/function from the superclass, in this case IStrategy class. Don't make confusion with the "@param" for example in the comments on second image, this are directives for javadoc, not for Java! In JForex API we have some implemented annotations for it and they are very useful to allow us more flexibility on our code. Until now i find three very useful: @Configurable,  @Library and @RequiresFullAccess. Each one allow us to:
  • @RequiresFullAccess : This directive allow us to access all system file structure on our computer, except the protected ones by the system itself. Without this directive we only can have access to the "<user documents>\JForex\Strategies\files" (change the back slashes to slashes if your system use it). It's very useful if we want to save data or configuration files outside of the default access directory and want to access them later.
  • @Library : This directive allow us to load external libraries and use them in our code. The way to indicate the external library (one, or more separated by semi-colon and between quotation marks) is @Library("c:/mail/mail.jar;c:/hsqldb/hsqldb.jar"). In this example we want to load the mail.jar library to send e-mails when something occurs and want to register something at one database managed by hsqldb.jar. Also, if we develop a more complex project where is not feasible to have all code in the strategy base file, we need this to import the resulting libraries done in other files. For that, we need also this directive.
  • @Configurable: This is one of the most used directives because this directive allow us to configure before the start of the strategy some variables if we want diferent values each time than the default ones assigned on the strategy code. It's very useful  as we want to do some tunning on our strategy without have to compile it everytime we want to use it with diferent values for this variables (also on bactesting). We declare this directive just before the variable declaration and assignment the following way - @Configurable("Currency pair:") and next line - public Instrument myInstrument = Instrument.EURUSD; This way when we start the strategy we get a window where we can define the pair we want to use if we don't want to use EUR/USD. Below i will show you one example. Don't forget, we have to declare the "configurable" variables as "public" for the directive to work.

// Selection of the currency pair to trade
@Configurable("Currency Pair:")
public Instrument myInstrument = Instrument.EURUSD;

//  Selection of the time frame used
@Configurable("Time frame:")
public Period myPeriod = Period.ONE_HOUR;

// Selection of the value of the position used to open trades
@Configurable("Position Size:")
public double myPosition = 1d;

// Selection of Stop Loss for each trade
@Configurable("Stop Loss value:")
public int myStopLoss = 20;

// Selection of Take Profit for each trade
@Configurable("Take Profit value:")
public int myTakeProfit = 40;
Image 3: The window that appear when we start our strategy with the code above; on right we see the possibility to change the values.

The last image show us the window that popup when we start the strategy where we can change the values of the @Configurable variables and save this changes on different presets with the name we want if we want to use this values later without to have to define all of them over again.

Note: The @configurable directive is useless in Strategy Contest as the strategy runs on the Dukascopy server and all variables are initialized with the values on the code as we don't have the choice to redefine them. But this directives are useful to see how our strategy acts on Demo or backtesting with diferent values.

Final words

This series of articles are getting bigger than i initially expected. At this time, when writting it, some concepts came to my mind, write about them and i end up without space to begin to build our strategy. But i think they are a good way to understand what we are doing and to avoid loss of time later.  I hope you like this series of articles, and as usual your opinions,  comments or private messages are welcome.

Trade well and prospers in you way!

Translate to English Show original