In this video, we are going to create an Expert Advisor that is able to close open sell positions when the trend changes and we see a crossover. 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 here you want to click on file, new file, Expert Advisor from template continue. I will call this file simple sell 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. This variable is also called signal and we don’t assign a value here because we are going to calculate it later.

The next step is to calculate the small moving average for candle one. That is done by using the IMA function. For the current symbol on the chart and for the currently selected period on that chart. We want to calculate the IMA for 20 candles, we don’t use a shift value here. And because we want to calculate a simple moving average, we are going to use Mode underscore SMA for an exponential moving average you could use Mode underscore EMA.

The result should be calculated based on the close price for candle one. The calculation for the big moving average for candle one is pretty similar. The only difference is the name for the variable and we are going to calculate it for 50 candles in this case. Now that we know those values, we can find out if the big moving average one for candle one is bigger than the small moving average for candle one. And if that is the case, that would be a Sell signal.

So we assign the word sell to our signal. In the other case, if the value for big moving average one is below the value for small moving average one, we would consider that to be a buy signal and then we would assign the word buy to our signal. And when our signal equals sell and the return value for OrdersTotal equals zero, that would mean we have a sell signal and no open orders. And now we use Ordersend to open a test position for 10 Microlot.

Please keep in mind this is only a test position, we need to open a position so we can check if the moving average sell cross over Stop Loss is working. But you wouldn’t do that on a real account. In the other case, when we have a buy signal and we have open orders, that would mean that OrdersTotal is bigger than zero. We want to call a function that is called close sell position this pair and close all the open sell position.

Let’s add a comment statement here that will output the text the current signal is followed by the calculated signal. And that’s it for the Ontick function, but to use this function, we need to create it now because it doesn’t exist. Our function uses void because we don’t need a return value. The name is close sell positions this pair and we are going to use For loop to go through all the open positions. We use orderselect to select the position for the current counter value.

Now we want to find out the current currency pair that is done by using order symbol and that makes it possible to check if the current symbol on the chart matches the currency symbol. We also want to find out if the order type equals OP_SELL and if those conditions are true, we use orderclose for the current order ticket and the current order lot size to close the current position. Finally, we need to close the For loop and 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 or maybe even the Premium Course on our website might be interesting for you. Now we have a link on that website where you can find the source codes for these basic videos. But for now, please click on the Compile button or press F7 on your keyboard. You shouldn’t get any 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 EX4.

Mark, the option for the Visual mode here and start a test. The first time you start the test, you will not see any moving averages. So let’s click on Stop and the moving averages appear on the chart. Now right click into the Chart. Select template, save template and save it as tester.tpl because this is the template that we are going to see in the next strategy test. You can override the current value. Now, restart the test.

Here we are. You see the moving averages are calculated and once we have a crossover we should see that this position will be closed. Because that is when the trend changes and this changes to buy. That should happen in a few seconds. And right now, the position was closed, so our little Expert Advisor is working as expected, and in this little video, you have learned how to create a crossover sell stop and you have coded it yourself with a few lines of MQL4 code.