This video, we are going to create an Expert Advisor that is able to open buy trades or close them when two moving averages are crossing. So let’s find out how to do that with MQL4. To get started please click on a little icon here or press F4 on your keyboard.
Now, you should see the Metaeditor window and you want to click on file, new file, Expert Advisor from Template, continue. I will call this file simple Buy Cross Over Stop. Click on Continue, continue and finish. Now you can delete everything above the Ontick function and the two comment lines here. We start by creating a string variable for the signal. That is called signal and we don’t assign a value here. Afterwards, we want to calculate the small moving average. That is done by using the IMA function for the current symbol on the chart and the currently selected period on that chart.
The small moving average should be calculated for twenty candles. We don’t use a shift value here. We use Mode_SMA, that stands for simple moving average. Of course, you could also use something like EMA for exponential moving average.
The results should be calculated based on the close price and we want to calculate the result for candle one. Now we want to repeat that for the big moving average. The big moving average in our case will be calculated for fifty candles. Everything else is the same, except for the variable name here and now we can calculate the signal, because when the big moving average is bigger than the small moving average for candle one, that would be a sell signal for us.
So we assign the words sell to our signal. In the other case, if the big moving average is below the small moving average for candle one, that would be a buy signal. So now we assign the word buy to our signal. This is about closing positions and to close something we need to open buy positions by using ordersend. We want to do that when the signal equals buy and when the return value for OrdersTotal is zero. That would mean we have no open orders and now we want to open a test position by using Ordersend for the current symbol on the chart, we are going to open a position for ten Microlot. We have Take Profit value of one hundred and fifty points.
Of course you only would do this on a demo account, not on a real account. And we do it to close positions when we have a cross over. So when the signal equals sell and we have open orders, that would be true when OrdersTotal returns a value that is above zero, then we want to call a function called close-buy positions this pair. And to see what’s going on, we want to use a comment statement to output the text.
The current signal is followed by the calculated signal directly on our chart. So far so good. But this function doesn’t exist so far, so we need to create it now. The name of the function is close buy positions this pair.
We use void because we don’t require a return type and now we use a for loop to go through all the open orders. Afterwards, we use order select to select a position for the current counter value of the for loop. We need to get the current currency pair. That can be done by using Order symbol. And now we want to check if the symbol on the chart and the currency pair are equal. And as we only want to close buy positions, we also check if the order type equals OP_BUY.
And if those conditions are true, we want to close the order. That is done by using order close for the current order ticket and the current order lots, we are going to close the order for the Bit price. The slippage is three and the last parameter here would be the color. I haven’t assigned a particular color here, but usually it’s red. Now let’s close the for loop and close the function and that’s about it. If this was too fast for you or if you have no idea what all the code here does, maybe you want to watch one of the other videos in the basic video series first, or maybe even the Premium Course on our website might be interesting for you.
For now, please click on the Compile button or F7 on your keyboard. You shouldn’t get any errors here, and if that is the case, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader, we want to click on View Strategy tester or press Control and R, please pick the new file simple buy crossover stop dot EX4 mark the option for the visual mode here and start your test. Here we are.
The Expert Advisor is running, but we don’t see any moving averages, so now we stop it. Here are the moving averages and now we right click into the chart, select Template, save Template and save it as tester.tpl. Because this is the Template that Metatrader is going to use in the next strategy test. You can override the current one now. Restart the test and now you should see the moving averages appear right away. And as soon as we see a crossover here, this position should be closed.
I think this is going to happen right now. Here we are, so our little Expert Advisor is working as expected. And then this little video, you have learned how to create an Expert Advisor that is able to open buy positions or close them when we see a cross over for two moving averages and you have coded it yourself with a few lines of MQL4 code.