MQL5 TUTORIAL – SIMPLE MACD STANDALONE EXPERT ADVISOR

video
play-sharp-fill

 

MQL5 TUTORIAL BASICS – 57 HOW TO CREATE METAQUOTES DEMO ACCOUNTS INSIDE METATRADER

 

Platin System

 

In this video we are going to create an Expert Advisor that is actually going to trade. It´s going to trade the MACD indicator. That´s the little indicator here. You see that it changes directions and whenever we have a crossover from above to below, that would be a sell signal. In the other case, when the signal is below the line and it crosses the line from below, that´s a buy signal. Now, how can we create an automated Expert Advisor that actually is going to trade the MACD signal? To do that, please click on the little button here or press F4 on your keyboard. And now you should see the Meta Editor window and here you want to select File, New, Expert Advisor from template, Continue. I will call this file SimpleMACDStandAlone, click on Continue, Continue an Finish. And now you can delete everything that is above the on tick function, and let´s also delete the document lines here. First we need to include the Trade.mqh file. It comes with a few useful trading functions. So let´s create an instance of the class CTrade. We will call it trade and we will use it later on to actually open trades. Inside of the on tick function we create a string for the signal. That will be also called signal and we don´t asign a value here, because we need to calculate that later on. Let´s calculate the AskPrice and the BidPrice. That can be done by using SymbolInfoDouble for the current Symbol and we use SYMBOL_ASK to calculate the AskPrice. And SYMBOL_BID to calculate the BidPrice. And with NormalizeDouble and _Digits we make sure that we have the right amount of digits, because there are currency pairs with three digits and others with five digits behind the dot. Now we need to create a Price Array. That will hold the Price Data. We use ArraySetAsSeries for our PriceArray to sort the PriceArray from the current candle downwards. And now we are going to use the iMACD function to create a MACDDefinition for the current symbol and the currently selected period on the chart. We have three additional parameters here. 12, 26 and 9. And that´s also what we see here in the indicator window, because when you right click on the MACD and select Properties, you will see it uses a Fast EMA of 12, a Slow EMA of 26 and a MACD SMA of 9. That´s exactly what we use here. And the calculation is based on the close price. So we are going to use PRICE_CLOSE for the calculation. We use CopyBuffer to fill our PriceArray. According to the MACDDefinition that we have created here. And we are going to use Buffer 0 from the current candle, that’s candle 0, for 3 candles. That´s the 3 here. And we want to store the result in our PriceArray. Now we are able to calculate the Expert Advisor for the current candle, that´s candle 0 in our PriceArray. And we assign the result to the variable MACDValue. Let´s do that again, this time for candle 1. That´s the candle before. And this result will be stored in the last MACDValue. And whenever the MACDValue is above 0, that would be the case, when the signal ist above the dotted line here. And when the last MACDValue was below the 0 line, that would be a crossover from below, and that´s a buy signal. So we assign the word buy to our signal. In the other case: If the current MACDValue is below 0 and if the last MACDValue was above 0, that would be a sell signal. So we assign the word sell to our signal. If the signal equals buy and positions total is less than 1, that would be the case when we have no open positions. We use trade.buy to buy 10 microlot. Otherwise, if the signal equal sell and we have no open positions, we would use trade.sell to sell 10 microlot. And finally we want to create a chart output. That is done by using the comment function. It will output the words: “The current signal is”, followed by the calculated signal. Ok, please click on the Compile button or press F7. That should work without any errors here. We have two minor warnings that we could check the type conversion here. But for this simple example we are done. So now we click or press F4 to go back to Meta Trader. And in Meta Trader we click on view, Strategy Tester or press CTRL+R. And here you want to pick the SimpleMACDStandAlone.ex5 file. Mark the visualisation option here and start a test. And here is our little Expert Advisor at work. We actually have a MACD Signal. We see the output for the current signal here. And we have an open trade. Actually we already made a profit. And now you know how to create a Stand Alone Expert Advisor for MetaTrader 5, that actually is going to open and close real trades. And you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 TUTORIAL - SIMPLE MACD STANDALONE EXPERT ADVISOR”

SimpleMacDStandalone.txt – Downloaded 463 times – 1.57 KB