https://mql5tutorial.com/?s=position
Introduction to Closing Positions in MQL5 (00:00 – 00:11)
Introduction to the topic of closing positions in MQL5, addressing a request from YouTube subscribers.
Creating a Video on How to Close Positions on Certain Conditions (00:11 – 00:20)
Mentioning the creation of a video tutorial on how to close all open positions in MQL5 under specific conditions.
Starting with the Basic Setup in MetaEditor (00:20 – 00:29)
Beginning the tutorial with the basic setup in MetaEditor for the expert advisor.
Explanation of the Expert Advisor's Functionality (00:29 – 00:43)
Explaining how the expert advisor will close positions and immediately open new ones.
Creating a New Expert Advisor in MetaEditor (00:43 – 00:51)
Demonstrating how to create a new expert advisor in MetaEditor.
Including Necessary Functions and Instances (00:51 – 01:16)
Including necessary functions and creating an instance of the 'CTrade' class.
Calculating Ask and Bid Prices (01:16 – 02:01)
Instructions on how to calculate the ask and bid prices using 'SymbolInfoDouble'.
Setting Up Position Count and Account Information (02:01 – 02:27)
Setting up variables for position count and retrieving account information like balance and equity.
Implementing the Position Closing Logic (02:27 – 02:39)
Coding the logic to close positions based on certain conditions.
Testing the Expert Advisor in MetaTrader (02:39 – 03:31)
Instructions on how to test the expert advisor in MetaTrader using the Strategy Tester.
Closing Positions Using the Expert Advisor (03:31 – 04:13)
Demonstrating how to use the expert advisor to close positions.
Addressing Potential Errors and Compiling (04:13 – 04:30)
Addressing potential errors and compiling the expert advisor.
Final Steps and Strategy Tester in MetaTrader (04:30 – 04:50)
Final steps to prepare for testing the expert advisor in MetaTrader's Strategy Tester.
Conclusion: How to Close All Open Positions (04:50 – End)
Concluding the tutorial on how to close all open positions using the expert advisor in MQL5.
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” .
Watch video MQL5 TUTORIAL - SIMPLE POSITION CLOSE online without registration, duration hours minute second in high quality. This video was added by user MQL5 Tutorial 11 September 2017, don't forget to share it with your friends and acquaintances, it has been viewed on our site 31,913 once and liked it 295 people.