MQL5 TUTORIAL BASICS – 104 SIMPLE SELL CROSSOVER STOP LOSS

video
play-sharp-fill

 

In this video, we are going to create an Expert Advisor for a simple moving average crossover Sell Stop Loss. So let’s find out how to do that with MQL5. To get started please click on a little button 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 continue, I will call this file simple cross over Stop Loss.

 

Click on Continue, continue and finish. Now you can delete everything above the Ontick function and the two comment lines here. We start with an include statement to include the file Trade.mqh. This one is part of MQL5 and it will help us to use some simplified trading functions.

 

Therefore, we create an instance of the class Ctrade and call it trade. Inside of the ontick function we start by calculating the Ask price. That can be done by using symbolinfodouble for the current symbol on the chart, we are using symbol underscore ask and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot. Now let’s repeat that for the Bid price. This is very similar. But to calculate the Bid price, we are going to use Simbol underscore bid. For the signal, we use a string variable.

 

This is called signal, but we don’t assign any values here because we are going to calculate that later.

 

Now, let’s create two arrays, one for the small moving average array and one for the big moving average array. The small moving average is defined by using the IMA function for the current symbol on a chart and the currently selected period on that chart. We are going to calculate it for twenty candles. The shift value is zero. We use ModeUnderscore SMA for simple moving average and the result should be calculated based on the close price. Let’s repeat that for the big moving average, this time we are going to use the same function, but for 50 candles, everything else is similar.

 

And with Copybuffer, we now can fill the small moving average array according to the definition that we have created here for buffer zero, starting from the current candle zero, we copy the values for three candles and store them in the small moving average array. Now let’s do the same for the big moving average array.

 

Once we have done that, we can now find out if the big moving average array for candle one is below the small moving average array for candle one. If that is the case, we consider that to be a buy signal, so we assign the word buy to our 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 sell signal. And now we assign the word sell to our signal.

 

And if our signal equals buy and we have open positions, we are going to use a function that is called close all sell position this pair. Otherwise, if the signal equals sell and the return value for PositionsTotal equals zero, that would mean that we have no open position. So now we need to open a test position, that is done by using trade.Sell. We are opening a position for 10 Microlot. 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 add a comment statement that will output the text the signal is now followed by the signal. That’s it for the main part, but to use this function, we need to create it. Now, the name of the function is close all sell position this power we are using void because we don’t need to return value. Now we need to use a For loop for all the positions and go through each and every position to get some details.

 

The first detail is the ticket number. We can get that by using position get ticket for the current counter value of the For loop. And we also need to get the currency pair for the position. That is done by using position get symbol for the current counter value. And as we only want to close sell position, we need to find out the position direction, that is done by using position get integer for the position type.

 

So let’s check if the position direction equals position type sell. That would mean we have a sell position. We also want to know if the current symbol on the chart is equal to the currency pair of the position. And if both conditions are true, we are going to close the current position. That is done by using trade dot buy position close for the current ticket number. Finally, we need to close the for loop and the function. And that’s about it.

 

Well, 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 this basic video series or maybe even the Premium Course on our website might be interesting for you. There you will also find a new link for source codes for this basic video stuff. But for now, please click on the Compile button or press F7 on your keyboard. I have made a mistake and I need to remove the additional bracket 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 you want to click on View, Strategy tester or press Control and R. Please pick the new file Simple Sell Cross Over Stop Loss .ex5. Mark the option for the Visual mode, if you don’t see it, you need to drag the Strategy tester. Here is the Visual mode. Please enable it and click on START. Here we are. This is our first sell position. And as soon as we see a cross over here, the position should be closed.

 

It didn’t because we reached the Take Profit value. So let’s speed that up here. And this time it worked, so our little Expert Advisor has closed the position, and in this little video you have learned how to create an Expert Advisor that is able to open sell positions and close them as soon as we have a crossover and you have coded it yourself with a few lines of Angyal five code.