We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.
BOOK YOUR FREE STRATEGY SESSION NOW >>How do you create an effective trading system using exponential moving averages?
Find out in this video, in which one of our coaches begins with a simple system based on the Moving Average Convergence Divergence (MACD) indicator and adjusts it by inserting some conditions related to the convergence and divergence between the MACD and its exponential moving average.
The results obtained by testing this strategy on a well-diversified portfolio of instruments are very encouraging, and it seems that the MACD has some interesting surprises in store for us!
Watch the video to find out how this strategy is structured!
Hi everyone! Today we're going to continue talking about Moving Average Convergence Divergence.
Welcome back. I am one of the coaches of Unger Academy, and in this video I’d like to continue with the topic of MACD that we started in the previous video (I'll leave the link right here at the top), where we went over the characteristics of the MACD and how to build it.
In this video, we will dive into a practical example, starting from what can be considered to be the classic approach.
Here’s a very simple code with very few lines. Now let's do a quick analysis of the script starting from the two main lines of code, which are found at 7 and 8.
So, with this line of code we are simply going to calculate the MACD with a function called MACD where we’re going to insert the close of the prices, a length to calculate the fast exponential moving average, and another length to calculate the slow moving average as inputs to this function.
As we saw in the previous video, these two quantities are 12 and 26, respectively, and just out of curiosity if we wanted to go inside this function to see how MultiCharts approaches the calculation of this indicator, we would find that it is a very normal difference of two exponential moving averages.
XAverage is the call to the exponential moving average function. If we were to get into this function, we would see here that this is the very function that we talked about in the previous video.
This var0, which is 2 divided by n+1, is the number of bars considered for the exponential moving average. And as you can see, we find in Power Language or Easy Language everything that we saw at a theoretical level in the previous video.
Getting back to the main script, here we have the calculation of our MACD value, which we will call myMACD. Then we calculate the MySignal, which you know by now very well to be the exponential moving average of my MACD, then of the value of the MACD considered, for a length equal to 9.
We now turn to the two lines that identify short and long market entries. As per the literature, we will go long in this row if the MACD crosses above the MySignal and we will go short if the MACD crosses below the MySignal.
In this case you then will see a small addition of MyContracts contracts. Here we are going to define the number of contracts to buy based on the amount of risk we want to apply. Which are these two lines here. We do this simply to compare multiple instruments of different sizes to then try to allocate roughly the same risk to each instrument.
The only thing left to do is backtest this strategy. To do the backtest, we will use a very diverse basket of tools. We'll be testing from about the beginning of 2010 to the present day.
Let's click through to see what this kind of strategy would have done. The beginning doesn't look too promising. It seems like a very random equity to me. It doesn't go up, it doesn't go down. It does go down slightly, but it's nothing special.
Let's see now what we can do starting from this idea, from this code, to understand if this indicator is completely wasted or not.
So let's try to analyze this indicator in depth. What could we do?
Well, first of all if you think about it we are in the market 100% of the time because we only coded entries. So to get out of a position I need to have an opposite signal. To exit a long position, for example, I need to enter a short position and vice versa.
So the first thing we can do is add stops or profits. To do this, obviously dealing with different instruments, we can arrange stops and profits according to the average currency range of each instrument.
Let's compile the strategy and see how it would have performed after adding this little trick.
Already we see that the music begins to change, meaning we are no longer talking about a strategy that's always in the market, but about a strategy that attempts some entries and then goes to take a stop or a profit depending on how the market moves after the entry.
But still obviously this kind of equity with crazy drawdowns of over $100,000 is not satisfying at all. We need to find something different, something that gives us some sort of operational filter to these entries. To do this, if you think about it we could exploit the MACD itself. What do I mean by that
Well, we know, because we saw earlier that it is formed out of a difference between two exponential moving averages, one fast and one slow. This could mean that we could somehow filter short entries, for example, only if we are faced with a fast exponential moving average that is below a slow exponential moving average.
So we could go to define a short entry only if the MACD is negative and inversely long only if the MACD is positive. Let's go ahead and insert this condition into our script. So If myMACD crosses below the MySignal level and we have MACD below zero, then we will go short. Conversely, if we have a positive cross and the MACD will be greater than zero, we will make long entries.
We compile and go as always to see what effect it might have on our backtest and as you can see it's improving. Let's start by going over what the performance of our strategy is. We have a pretty good average trade already, $150 out of $1,223. Let's also take a look at the drawdown, which unfortunately is still a bit high at around $75.000, but still the equity has a good average trade and it means that we are going in the right direction.
So what else could we do? Until now we have used a simple profit and a simple loss as exits, but what if we used the MACD as well, to further filter the exits Let's check out the graph.
At the moment, we have the classic MACD strategy, the one set up and viewed on a basket of instruments. Here we are looking at Gold, as an example. Of course, the timeframe (we haven't mentioned it yet) is one day, so 1,440 minutes.
So what's it look like to you What's going on here We have a cross between the yellow line and the blue line, which are the MACD and the MySignal respectively. So here we have a cross below by the MACD, which signals a short entry. The MACD is even negative so we go effectively short. We're on a correct trend but eventually this trend ends, retraces and comes back up.
We don't take the target in this case and the trend comes back up and even goes to take a stop loss. So what we could put in the code, that I think would be useful is the following why don't we try to exit the position when we have a convergence between the MACD chart and its exponential moving average
What do I mean by that?
As you can see, after a cross, the two lines tend to diverge, so the MACD falls faster than the MySingal line, or its 9-period exponential moving average.
After that, at some point these two lines converge again. What we are going to do now is to encode this divergence and convergence to go out of the position, short in this case, if the divergence between these two lines slows or even inverts to a convergence.
How do we put this into code?
Starting from the script that we have used until now, we are going to add between the lines dedicated to the stop loss and take profit, and add another two lines to those dedicated to the entries. These two lines you see here correspond to the exit for short positions and the exit for long positions.
These market position less than zero and market position greater than zero are just to indicate the activation of everything that we are going to write later, namely that these conditions will be verified together with the condition in which I am actually long. The same applies to the short position above.
Here is the code for the convergence of the two lines of the MACD and MySignal. As you can see here I have done nothing more than to identify the difference between the two, therefore the difference of MySignal regarding the MACD and then checked if the current bar's difference is greater than or less than the difference on the previous bar.
Along with this condition another one is created, completely identical but shifted backwards by one bar, so I'm going to check if the bar before the one I'm evaluating now saw a difference between MySignal and MyMACD that was lower or higher than what was seen as the difference at the still previous bar.
So there you have it, if two such conditions occur, then the first and the second simultaneously, because they are distinguished by and and therefore it will be necessary for both conditions to be true to trigger the exit, which we see here at the bottom being buy to cover because we were talking about a short position. It will instead be sell for the exit of the long position.
And so let's see what happens when applying this type of exit.
One final note concerning the profit target, which we have expanded. In fact you see that the multiplier is now 2 instead of 1. This is because we have added new exits and therefore can afford to give more space to the divergences between MACD and MySignal.
Let's go back to our Portfolio Trader page. Let's activate the new strategy called Revised and run the backtest. Wow, now we're talking! We're looking at some very good equity that is growing pretty steadily.
Let’s have a look at how this strategy performs instrument by instrument. Here’s Heating Oil, which has really good equity with very low and contained drawdowns.
Other insruments such as Live Cattle unfortunately work in reverse but to avoid overfitting should not be removed, regardless. Let's say that in general this strategy, which we absolutely did not modify from a theoretical point of view, because we considered 12, 26 and 9 as periods for the calculation of the indicator, so we tried to leave things as standard as possible, we still managed to get with our instruments something that could be useful.
Of course, using fixed numbers (12, 26, 9) and not being able to make an optimization we have been sort of limited in these demonstrations, so I want to drive home the importance of experimenting further with MACD because it has great potential to give you something really interesting.
Well unfortunately this in-depth MACD video ends here.
Goodbye for now, thank you for following me here. See you next time with a new technical video.
We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.
BOOK YOUR FREE STRATEGY SESSION NOW >>Andrea Unger here and I help retail traders to improve their trading, scientifically. I went from being a cog in the machine in a multinational company to the only 4-Time World Trading Champion in a little more than 10 years.
I've been a professional trader since 2001 and in 2008 I became World Champion using just 4 automated trading systems.
In 2015 I founded Unger Academy, where I teach my method of developing effecting trading strategies: a scientific, replicable and universal method, based on numbers and statistics, not hunches, which led me and my students to become Champions again and again.
Now I'm here to help you learn how to develop your own strategies, autonomously. This channel will help you improve your trading, know the markets better, and apply the scientific method to financial markets.
Becoming a trader is harder than you think, but if you have passion, will, and sufficient capital, you'll learn how to code and develop effective strategies, manage risk, and diversify a portfolio of trading systems to greatly improve your chances of becoming successful.