I'm actually studying Java programming to improve my personal background. I started some time ago, but I had to postpone due to many difficulties and my knowledge is still at an elementary level, so in case you notice some mistakes on the code, please forgive me in advance.

With this article I want to show how I automated a simple MA-based strategy. So I also take the opportunity to share it and to show my approach in detecting main factors and then all steps I considered while developing and testing the code.


I decided to start coding the very first strategy I used because I have a good familiarity with all its good & bad aspects, and to use it in this article since gives a clear perception of the main aspects I'm presenting. However, the code can be modified to look for different MA periods in different timeframes and instruments.
  • Instrument: EUR/USD;
  • Timeframe: 15 Minutes;
  • Indicators: EMA(14) & EMA(65);

The strategy is simple and people could laugh thinking about it, while I really suggest to give it a deeper consideration.
This is how the chart setup appears:

The rule is clear: sell when green line (ema14) cross down the red one (ema65), buy when it cross up. But speaking in terms of automated strategy, things become different: main purposes are to achieve the best entry possible, a good R/R ratio with as tight stoploss as possible.

If I build a simple code buying or selling when the 2 moving averages cross (I did) I will find my self in this situation:

In this example we see how the program has to wait the new candle before opening the position, since the cross is actually confirmed only after candle close. In this particular case, after the position is open, price retraces up for 23 pips + spread (25pips indeed), and only after 20 bars the position goes in profits of about 21pips to retrace back up again for another drawdown.

The core-concept

What I need is to wait price to reach the slower EMA to open the position, so in one single step the whole situation immediatly becomes better. See the same picture now:

In this way we can get 2 trades, both with about 30 pips profits for a total of 60 pips, with very small drawdown.

The code

As I mentioned in the premise, my programming skills are not great and I encountered some issues. Main difficulty was about detecting the EMA. To make this possible I tried many different ways and I decided to follow a simple logic: I don't need the position to be opened exactly at the EMA value. Let me show all steps.

  • I defined a 'range area' near the slower EMA. This area is shown in the picture below by the 2 red-dotted lines around the 65EMA(red):

  • The 'range area' basically uses the slower EMA values and add or detract a pre-defined variable called "levels" (in this example 4 pips). In case of faster setups however, would be better considering a smaller range.

  • I coded the 2 indicators (EMA 65 and 14 ) separately:

  • I defined the "PRice" value as follow:

  • Finally I built main rules as follow: if faster EMA is above slower one, and price is within the range area, buy the instrument with selected stop loss and take profits orders. Opposite order in opposite scenario.


During these operations as I explained above, I tried many approaches. For example should be also possible to work through a boolean and create two separated environments buy/sell -> true/false, but I prefered to follow the way shown in this article and eventually change my direction later in case of particular needs.

How it works

Before starting, the strategy asks to set main parameters such as instrument, timeframe, position size etc... shown in the following picture the setups I'm actually using:

And here below is the chart showing where positions are opened, using JForex tester in visual mode. I actually achieved my main goal: positions are opening exactly where I expect it to happen!

The first back-test

At this point I can start back-testing to obtain some results. In my opinion, the first back test has to be run in short term period (max 2 weeks) because I don't need to see the profitability and to calculate the win/loss ratio. Not yet! I actually want to know what kind of losing trades the strategy produces, if there are some basic mistakes in the code, and just solve them before running a longer term back-test.

Here the first test setups and results:

In this case the test returned a great result, far better than expected: losses happen when market reverses, but then they are everytime recovered by immediate opposite position. This is a very rare case, because the test was randomly executed in last week (from 01 to 08 april), when this instrument did not experience bad 'spiky' moves or tight range-trends. The chart below indeed, shows that moves are very clean and a bit-experienced trader, knows this happens once or twice in a quarter (maybe three times).


At this point the first stage is completed:

I achieved the main goal to make the program detect the slower EMA price, and use it as entry level both for buy and sell. From here the strategy has to be tested, implemented and stabilized in many different ways. Also it can be used in different instruments, timeframes or even with different EMA periods etc. Basically the concept is open to many different possible combinations but in this article series I will consider only the setup EMA14-EMA65 in 15m charts.


I decided to write this article since I have been unable to find this particular MA-based strategy so I built it by-my-own. At this stage the code can be used and implemented by anyone, there is not even need to ask. Of course, an "I like" or a "Thanks" in case you found it interesting and/or observations are always appreciated.
In next article I will consider a serious back-test and all steps I'm following to optimize the strategy and eventually obtain a good tool to use in my trading activity.

Thank you for reading.
Translate to English Show original