This is a quick conclusion of previous article series titled "The Fifth Element (part 1-2-3)" where I exposed how I made an automated strategy working in semi-automation and using it just as a visual reminder of possible good trade events, and implementing a widget in a chart to immediatly buy/sell an instrument with pre-fixed SL/TP orders and size. Here you find links to previous series "The Fifth Element".

As you may know, I'm not (yet) an expert Java programmer, and as a great beginner I did some minor errors, indeed the code was working but not perfectly. In this quick article, which opens to my next presentations as well, I am only exposing how I solved these minor issues and how I took the opportunity to add some few improvements, (the sixth indeed).


Trading with MAs is one of the most simple and various ways traders use to detect a price direction, and due to the vast amount of different MA settings, I decided to make it personalizable when strategy starts. But as always once I did this, I realized it would be better to personalize almost everything, such as position size, stop loss, take profits etc... Here in following picture you see the setup window appearing at strategy start:

  1. You can choose preferred instrument and timeframe;
  2. Position size has to be fixed according to your needs: I avoided to impose a money management since every trader has his/her own one. By default, you will be able to open a 10.000 units trade (about 1$ x pip);
  3. Here you can set the TP/SL distance on trade execution: on faster timeframe this will be tighter, while you could decide to trade off a 4h chart and use a 150 or 230 pips distance;
  4. This is to set the minimum price distance from trade opening, before you can set the automatic trailing stop at breakeven (exposed in next paragraph);
  5. I thought was good to set the timemarker colours, depening on your own chart colors setup;
  6. Here you can set which EMAs to look for: you can choose for example a combination of eMA200 and eMA50 to use in M5 charts (quite interesting indeed). The CheckDirection sMA, is the one used as a filter for possible fake moves (exposed in part 2);
  7. Finally you can set the LinearRegressionSlope indicator lookback periods. By default it's a 96 periods (part 1 ) but one can set differently, depending on timeframe and preference. I would not suggest to setup it too much fast, below 30periods I personally don't see any benefit, but as always... i could be wrong!


I always prefer to move stop loss orders at breakeven once the trade goes in my direction. In this particular kind of strategy, once the trade is executed, I expect price to bounce off eMA65 within few 15m candles, and usually the bounce is about 40-50 pips. If this is not happening, I want to move stop at breakeven, because there could be a reversal causing my trade to become a loser. I added a button on the widget, so I can do this with just one click.

As you can see from the picture above, there is an additional button on the widget. In the chart you can see there are 3 positions running, but only 1 of them is labelled "Strategy": in this way, I can later recognize all strategy orders and manage them without affecting any other position eventually opened in the same account.

The "Set breakeven" button, will automatically move all stop loss orders in any of the "Strategy" positions running, once price is in profits of at least 10pips (or the selected distance). There could be 100 positions running and once you click the button, all 100 stop loss will be updated instantly. I would suggest to test it on a demo account.

Furthermore, I made the buy/sell buttons to self-modify depending on the SL/TP distance set on strategy start. In this way, If I select a 100p SL and 50p TP, they will also appear on button labels. The same applies to the "Draw position area" button, but in this case I set as default range the TP one since the strategy is based on a 1:1 R/R ratio. So basically, if you set 50p SL and 100p TP, the area will anyway be drawn as 100p above and below last tick.


In my opinion, when we stop a strategy, all positions have to be closed and all drawings have to be deleted. As anticipated earlier, all positions are now labelled "Strategy" so I can detect them in case of need. In this way, when I stop the strategy, it will close all running positions containing the strategy label, without considering any other trade eventually running in the same instrument or in any other one. In few words, a selective close. Next picture to show how it is made on the code:

As shown in the picture, the very same applies to all strategy drawings: if you stop the strategy, it will automatically delete any drawing excluding the ones not coming from the code itself. So you can draw anything you want in the chart, and once you stop the strategy all your drawings will keep staying there. I was in need of this because many times I have my technical analysis levels drawn on charts, and I don't want to lose them everytime I stop a strategy.

The code is downloadable following this link. Please let me know if works, so eventually I can upload it somewhere else, or send you directly.


The strategy is now completed in all its parts, and does everything as planned. I fixed some mistakes I made, and implemented some particularities in order to give me (or anyone using it) a better and quicker way to manage orders and drawing on a chart. The strategy rules are simple and now you can set any eMA periods you prefer in any instrument and timeframe.

I'm now working to keep improving the code, and also considering the possibility to put it back in full automation. First paper tests results were promising but as always, must be patient and wait for the full results.

I hope this will be interesting for people and eventually to be used as additional tool. Please let me know if you find some problems, or have any suggestion, idea or anything else.

Thank you
Traducir a inglés Mostrar original