MQL5 TUTORIAL BASICS – 102 SIMPLE BUY CROSSOVER STOP LOSS

video
play-sharp-fill

 

In this video, we are going to open some test positions, buy positions, and those positions will be closed when the crossover of these two moving averages happens. So let’s find out how to do that with MQL5. Please click on the little icon here or press F4 on your keyboard. Now, you should see the Metaeditor window and here you want to click on file, New file, Expert Advisor from Template. I will call this file simple by cross over Stop Loss.

 

Click on Continue, continue and finish. Now you can delete everything above the ontick function and let’s also remove the two comment lines here. We start by including the file Trade.mqh and we want to create an instance of the class Ctrade that is called trade and will provide some simplified trading functions. We first need to calculate the Ask price. That is done by using Symbolinfodouble for the current symbol on the chart, we want to use symbol, underscore, ask and with Normalize double and underscore digits we automatically calculate the right number of digits behind the dot. Let’s also repeat that for the Bit price. And now we create a string variable for the signal for a crossover.

 

We need two arrays, one for the small and one for the big moving average.

 

The small moving average definition is created by using the IMA function for the current symbol on the chart and the currently selected period on that chart, we are going to use twenty candles. No shift value in this case. I’m using ModeUnderscoreSMA for simple moving average.

 

But you also could use ModeUnderscoreEMA for exponential moving average and the calculation should be done based on the close price. The definition for the big moving average is very similar, but in this case we are using 50 candles. Now we use copybuffer to fill our array for the small and for the big moving average.

 

According to the two definitions that we have created. We do that for buffer zero from Candle zero. That’s the current candle, and we copy the price data for three candles and store them in the arrays for the small and for the big moving average. And whenever the big moving average array for candle one has a higher value than the small moving average array for candle one, we consider that to be a buy signal. In the other case, if the big moving average array for candle one is bigger than the small moving average array for candle one, that would be a sell signal.

 

The idea is to close all buy positions for the current currency pair. When we have a sell signal and when the return value for PositionsTotal is bigger than zero. That means we have open buy positions and want to close them, but we need to have something to close. So I’m going to open test positions when the signal equals buy and when we have no open positions, that’s when we are going to use trade.Buy to buy 10 Microlot. In our case, we are using Stop Loss value of 200 points and a Take Profit value of one hundred and fifty points.

 

Please remember, this is just a test position, you wouldn’t do that on a real account and to see what’s going on, I would like to have a comment statement that will output the text. The current signal is now followed by the signal. Well, this function does not exist, so we need to create it now. The name of the function is close all buy positions this pair. We use void because we don’t require a return value. Inside of the position we are going to use a for loop to count down until there are no positions left.

 

We need to get the ticket number, that is done by using position get ticket. We want to identify the current currency pair. That can be done by position, get symbol. In both cases we are using the current counter for the current position. Now let’s get the position direction. That is done by using position, get integer and the parameter here is position underscore type. And if the position direction equals position type buy, we have a buy position. Now we want to check if the current symbol on the chart equals the position symbol.

 

You could use symbol with an underscore, that stands for my current currency pair. And if this is equal to the currency pair for the position that we have identified, we want to use Ctrade dot position close, to close the current position. Now, let’s close the For loop and the function. And this is basically 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 or maybe even the Premium Course on our website might be interesting for you.

 

For now, please click on the Compile button or press F7. Oh, I have an additional bracket that I need to remove here. Now, let’s recompile the code. This time we have no errors, 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 cross over Stop Loss.ex5 mark the option for the visual mode here and start a test.

 

Here we are. We have a buy position and as soon as we see a cross over this position should be closed. And that should happen right now. So our little Expert Advisor is working as expected, and in this little video, you have learned how to use a moving average crossover to close open buy positions, and you have coded it yourself with a few lines of MQL5 code.