MQL5 TUTORIAL – SIMPLE POSITION CLOSE

video
play-sharp-fill

This time we are talking about closing of positions, that happened right now.
I have gotten a request to do this from one of the YouTube subscribers, the actual request was: “can you make a simple close all position order?”.
So I guess he is asking if I can create a video how to close all opened positions once a certain condition is true. We have already closed all positions here, so the first thing we do is opening a few positions and as soon the equity here rises above the balance all the open positions will be closed.
That happened right now and because this is just a test the Expert Advisor immediately opened a few new positions, but as soon as the equity is higher than the balance they will be closed again.
Here are our closed positions and now we want to check out, how we can create an Expert Advisor that is going to do this.
To start, please click on the little button here or press F4 in your Metatrader and now you should see the Metaeditor window. Here you want to click on “File, New, Expert Advisor (template)” from template. Continue. I will call this one: “Simple Position Close” (SimplePositionClose). Continue, continue and finish.
Now you want to delete everything above the “OnTick” function here and the two comment lines. The first thing we need to do is to import the Trade Library. We can do that by using the “include” function and the file we want to import is the “Trade.mqh” file.
After that we need to create an instance of “Ctrade” that will be called: trade.
Inside the “OnTick” function we need to get the Ask price. That is done by using “SymbolInfoDouble” for the current symbol on the chart, and we use SYMBOL_ASK to calculate the current Ask price and with NormalizeDouble and _Digits we make sure that we have either three digits or five digits, depending on the currency pair that might be different.
Let’s get the account balance here and it’s done with AccountInfoDouble and here we use ACCOUNT_BALANCE. We do the same for the account equity, but this time we use ACCOUNT_EQUITY.
Now we want to open a few positions to have something we can close, so, if PositionsTotal is below 5 we use trade.Buy, let’s actually use 10 micro lot here for the current symbol and the current Ask price.
We haven’t defined a Stop Loss here and the Take Profit should be 100 points above the current price, and we don’t need any comments.
Please remember we just do this to test it and once the equity is greater than the balance – that would mean that we made a profit – we want to close all positions.
So, let’s create a function that can do that.
We will actually call it CloseAllPositions and the first thing we need to do is to count from the number of positions down to zero that is done with this “for” loop here and the result will be assigned to counter variable called: “i” .
We use PositionGetTicket and that counter variable. To get the ticket number we need to close the position and as soon as we have done that we can close the current position by using trade.PositionClose(i) for the current ticket number.
Let’s close the “for” loop and the CloseAllPositions function. If you are ready you can click on the “Compile” button here or press F7 and you shouldn’t get any errors here and if that is the case, please click on the little button here to go back to Metatrader.
In Metatrader please click on: “View/ Strategy Tester” or press CTRL and R on your keyboard and here you want to select the file: SimplePositionClose.ex5.
Mark the visualization option and start your test. And here we go! The Expert Advisor is opening and closing test positions and now you know how to close all your open positions at once and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 Tutorial - Simple Position Close EA”

SimplePositionClose.txt – Downloaded 643 times – 1.13 KB