Before continuing I would reccomend to read the previous steps where I explained the bases and some core concepts. You can find them following these links: part 1 here and part 2 here.

The fourth element

The concept is always the same: everything has a clear (eventually complex) function but has to be as much simple as possible. At this stage I'm going to implement the order engine, so meaning the whole code structure that allows to open positions in the given instrument.
As I already explained, any MA-based strategy is definetly not stable: always ends with an overall losing result, no matter what timeframe or instrument, but in the same time there are really good opportunities happening in particular conditions.
To simplify and improve my trade decisions, I need the maximum amount of informations and commands in the same chart so I found useful to sligthly modify a simple tool found in the Strategy API: the widget.

Chart widget

What triggered in my mind this idea is simple: since the full automation doesn't work (for this MA-based strategy), I will execute the trades manually, but I want to do that in the fastest way possible. I know, JForex already provides us the possibility to buy/sell an instrument with just one click, but what I need is something different!

First of all I want a button right on the chart, then I want the possibility to customize SL-TP and size depending on the instrument. Of course, I want all this available with just one click. I know I'm never satisfied... . Let's see the code:

The widget has to be created "onStart" method, so it will be loaded once when executing the strategy, and it will basically remain until an opposite command will close it. In the following cut, we see how to create a button which once clicked, draws a rectangle of 50 pips height and 12 bars width:

As explained in comments, it actually draws 2 rectangles: one above last tick, and one below. Another 2 buttons were required to open a position on market price, with pre-fixed size, SL and TP distance:

Once we click on buy or sell buttons, the strategy executes the 'openPOS' which (again) I prefered to describe in a 'private void::

In this way, I have 3 important commands right in my hands and always in front of my eyes, and I can execute them with just one single click, directly from the chart. In next picture we see how it will appear:

How it works

The essential part is about the 2 rectangles: I want it to draw the profit/loss area. The strategy concept is to catch good entries for an average 40-60pips distance from the eMA65. If for some reasons price goes further than 40pips against my entry, there is a large probability the position is wrong so better cut it immediatly and wait for the opposite direction to come or for a strong continuation signal.

Another important aspect is about timing and duration of the bounce: I recognized that once price meets the entry area, it must bounce on the eMA65 and meet with target price within 10-14 m15 candles (about 3hours). In strong trend conditions, price will touch the eMA and immediatly bounce back in a matter of 1-2 candles. The longer price stays flat against or near the line, the more probabilities that a (short term) reversal is behind the corner.

Drawing these rectangles, as you may understand, comes helpful to remind this important rule and if price goes out of the planned time without significant signs to proof my position safe, it's time to cut and watch for another opportunity (or to reload the same entry after a brief check-up...?).

In the attached video, we see a quick example of drawing the position area, and opening the position. Sorry for low quality due to my slow processor.

The fifth element

I have in one single chart a lot of informations and a few very important commands: I see price behavior in a 15min timeframe, I know what is happening in daily (first step), i know where is main direction by reading eMA (second + third step), I have visual alerts appearing when something is gonna change and when price is near a possible great entry area for a 40-60 pips trade, I can open the trade by clicking directly from the chart and also I can have a visual sign to alert me in case price is taking too much time to respect the direction so eventually suggesting the strength of trend could be weakening.

Still it's missing the most important aspect! To understand it I have to go back at the beginning of this series: when there's a clean direction everything works fine, while a price near main resistances/supports causes a lot of difficulties. When trading using the MA this is even more notable and we find price printing something like this next chart:

Every red arrow is to show every failed approach to the entryMA, while every green arrow is to show the good opportunities. A full automated code, when occuring this particular scenario, will end with at least 6 losing trades and 3 or maybe 4 profitable ones. As you can see, in the chart I drawn also 2 levels and 2 timemarkers. Let me show the same chart seen in 4hours timeframe:

As you can see, around 170.00 there is an important previous support which could be used as resistance more than once, while with lower rectangle we have an important support around 168.50 which is about 100pips below the horizontal line I drawn in previous m15 chart. Now, the clue is about understanding the importance of major levels. If I go back to the 15min chart, with this new informations, it actually appears like this next:

From the right, we have price reconquering previous important support (seen in h4), and showing a perfect continuation to the upside. Later price meets what we (should) know is a strong resistance near 170: we don't want to buy a resistance, right?! Indeed we should forget to buy until price shows us a continuation breakout, or eventually at a lower level. In this case, the lower support 168 was not tested anymore, so definetly the whole red rectangle in the middle of chart, shows us the whole time we have to stay out of the instrument until a clear sign of continuation or reversal will be happening. Of course, a skilled trader could sell the instrument near the resistance...

So we could end with 3 or 4 profitable trades and no losings, while the full automation ended with a net 2 losings!

Wondering what is the fifth element? IT'S ABOUT US!! There is no way to build a full automated strategy based on moving averages and end profitable in the long run. This could happen for a week or two, then in 3 days all profits will be erased. What makes the difference is about techincal and fundamental analysis! We must know which is the strongest currency, why, and how much this strength could keep staying on market, analyzing major levels and overall price behavior.

Looking at the last chart, we want to wait a strong confirmation bearish after price reached the resistance in 170.40, but we don't want to buy just below it! Eventually, we will buy again the instrument IF and ONCE market breakes the resistance.


Coding all these particularities about fundamental and technical analysis is actually not possible. At least it's not in my abilities! And even if one day I will be able to make it, I will not certainly do that in an MA-based strategy!

WIth this article series, I exposed what I consider the best way to use this kind of strategy, helping the decisions when prices are in strong trend or after an important breakout, sharing some small improvements I made in order to simplify all this process. Basically I could just not even watch charts, and wait for the red/green arrow to appear and decide if that's a good place where execute the trade.

I am already working in another improvement about this code, but due to some time limitations (...) this had to be postponed a 2-3 weeks.

Any comment, request, suggestion... is highly appreciated! Thank you for reading.
Translate to English Show original