Skip to main content


Showing posts from 2018

Implementing Trailing Stop Loss in MQL4

Cut your losses early, and let your winners runs.
Trading circles Introduction This is the most common quote you'll probably hear in trading circles. Which basically means, that as soon as you open a position, you need to monitor it and make a decision against it or for it. When you move into the algotrading regime, I would modify this quote into the following one:
Cut your losses early, and let your winners run... to your trailing stop loss.
Roy Meshulam That's because you don't want to spend your time in front your PC monitoring the order. The contrary, you want to have a fully automated expert adviser that does it for you, opening and closing order according to the logic you built.

Trailing stop loss a strong utility to have. It is basically the logic to update the stop loss value and move it together with the price. So, if the price is in your favour, you move the stop loss, thus, capturing more profit. If it is against, you keep the stop loss with the same value. Hence…

How to Communicate Between MetaTrader Terminals using Files


Do you need different MetaTrader terminals to be able to communicate between themselves?

Some reasons to implement this logic could be:

Trading with different accounts which need to know on each other status.
Trading with different brokers which needs to have the same orders open.
Using one terminal to notify another terminal upon a certain trading condition.

The use case which enforced me to communicate between different terminals, was a strategy running on one account that had to notify another terminal to open orders once certain conditions were met. The second strategy was a derivative of the first one, so it had a dependency on its behavior. But I didn’t want to run both strategies in the same terminal as I wanted the order book to be clean and not a mixture of these two strategies.

Important: this solution will only work if the terminals are running on the same PC, otherwise, you need to consider using other methods like reading remote URL or using FTP.


Using Common…

How I make Money trading Forex

Did you know that on average 80% of retail traders lose money when trading Forex?

But, if you've been following my blog, you realized by now that I make my income from trading Forex, and to be specific from trading EURUSD only. This is the only product I trade and live from, which is quite counter intuitive, how is it possible to trade only one product with all it's ups and downs and live from it? In this post, I will explain exactly how I make a living from trading Forex and which traps to avoid.

Still, even that it worked for me and the setup is quite simple, there's no guarantee it will work for you the reader. We are all in a different situations in life, with more or less to risk, knowledge and so on. But still, I suggest to read this post to understand which traps are out there and which path you can follow to avoid losing your money.

The Traps

But first, let's start with the typical pitfalls out there, unfortunately, in some of them I fell myself and lost money:


How I use Properties Files in Expert Advisers


The classic way of defining input parameters for an expert adviser is using a variable, for example:
input double Lots=0.01; //Default lots to use
In this case the input variable will hold the default value to use in the expert adviser throughout the execution. If you want to change the value, you’d have to restart the expert adviser to enter the new value.
But, what if you could have avoided restarting the expert adviser, in some cases, the expert adviser is doing something or holding some valuable piece of information which might get lost if you restart it again.

My way of using parameters solves exactly that challenge, in my solution which I will explain later on, I am using properties files and updating the expert adviser on the fly, thus, avoiding the unnecessary restart of the expert adviser.

File Structure

As I am using the plain text files as properties files, the structure is mostly as key=value structure. For example, the Lots input parameter mentioned above will be wr…

MQL Functions to Close All Orders

This code and much utility functions are available for purchase as part of the Common library.


It is a common practice in my code to close all the orders once a certain condition is fulfilled. Normally, a profit target is reached, but there could be other reasons. In this post I will introduce some of the functions I am using to perform that. The main  common feature to all of these functions is the counting starts from OrdersTotal()-1 and goes down to 0. This to prevent closing already closed orders as noted in this article -



The standard function to close all the orders is simply going through the open ones and close them one by one, starting from the last towards the first:

bool CloseAllOrders(string argSymbol="") export
for(int i=OrdersTotal()-1; i>=0; i--)
if(OrderSelect(i,SELECT_BY_POS) && (StringLen(argSymbol)==0 || StringCompare(argSymbol,OrderSymbol())==0) && (OrderProfit()+OrderC…

FX Algotrading Q&A Session 16/08/2018

On the 16th August I delivered a webinar on the topic of "introduction to FX Algotrading". In this 40 minutes I introduced myself, the strategies I am running and the platform I am using.

Here is the replay:

Toggling the Auto Trading Button in MQL4


In this post, I will introduce an MQL4 code I use to control remotely the auto trading button on/off status, this code is part of the RemoteControl library product available for purchase here.

There are some scenarios where we would like to stop trading completely such as:

Extreme market conditions or
Below a certain equity threshold.
Negative P&L below a certain threshold.

But when we are not near the PC, this could be a challenging task to do. We can either close all of our positions and take some loss, or, let the positions run and hope for the best…

In this post, I will introduce a code snippet which you can plant in your code, which upon certain conditions can mimic halt trading by toggling the auto trading button on / off:

So, upon executing this code, the button will be toggled between on and off, and you will get notified about the new state.


First of all, we need to modify the WinUser32.mqh which is shipped as part of the MetaEditor installation, by addi…

Linear Hedging Framework for MQL4


When opening an order, we are all hoping that our analysis was correct and our position will make a profit. But, when this is not the case, there are in general three possibilities to continue:

Close the order upon a predefined stop-loss, if we were able to close the order on the opening price, then it's even better and no harm was done - The challenge with this approach is to know where to set the stop loss point, not to make too tight so it doesn't get hit quick, or too wide and then the loss is massive.
Let the order run and hope that at some case the trend will reverse and our analysis was correct - Challenging as well, since we will never know with 100% accuracy that this level will be reached again, so in the meantime our P&L gets more and more negative and we make loss on the swap if it is against us.
Hedge the order - meaning opening an opposite order upon a predefined gap and close it once the orders cancel each other profit wise.

I will dedicate this post…

FX Trading Q&A


Since I joined Quora platform, my profile is overflooded with questions to do with FX Trading, investig and algotrading. The goal of this post is to summarize the different quetsions I answered already so there is single point to search.

Questions & Answers

How long did it take you to become a profitable trader?
I started in 2008, but only in the last three years my hard work started to pay off. Until then, it was mostly losses, mistakes, wrong strategies and decisions.

The main things I wish I knew back then:

Choose the right broker, one that has the best execution, minimal spreads and excellent customer service. Those who offer gifts and bonuses were mainly causing me more damage than benefit. I did find Darwinex but it took me ages.
Don't buy robots, develop your own strategy. All the robots I bought was a complete disaster and cause me more losses, I learned the hard way that no one sells a successful strategy.
Keep the leverage as low as possible, never use the full …

Moving Averages Trader Implementation for MetaTrader 4

Introduction Moving averages trading is the most basic and common trading strategy. It is quoted in most of the trading books and articles and is very easy to follow and implement. The basic strategy is two moving averages, a fast one which is adjusting quicker to the current trend than a slow one.

A classic setup is looking at the 50 days moving average of a certain product and check whether it is above or below the 200 days moving average. If it is above, it is an indicator for going long with this product, if it is below, it is an indicator to short the product.

In this post, I will introduce a basic implementation of an expert adviser, which based on the inputs will trade the product using the price moving averages. You may use it as a basis for something else, modify it as you wish etc.

Input Parameters These input parameters are controlling the moving average calculation:

input int FastMovingAveragePeriod=50;//Fast Moving Average Period input int SlowMovingAveragePeriod=100;//s…

5 Checks to Enhance your MQL4 Code


Throughout my years as an algotrader, I gained a lot experience with writing optimized MQL4 code. In this post, I am going to share some patterns I am using in my on robots. Some check for connectivity, some for spread, some for volatility etc. Embedding these code snippets in your own, will make your robot more resilient and you will more relax, as you will add self monitoring capabilities to it as well as better trade entry rules.

Server Connectivity Check

From time to time, MetaTrader station losses connectivity with the broker's server. And when that happens, normally, the only thing which works is to restart the platform (or hopefully wait the problem will fix itself). But how can you monitor the platform and get a notification once it is happening?

That's exactly the solution I built for myself. Since the function OnTick() is not called anymore as there is no connectivity. The only solution was to place the monitoring in the OnTimer() function and check every X …