I have this code which should close all trades when a certain condition is met. Normally 3 open trades of the same symbol are open when the condition is met. However, the code closes one trade and leaves 2 others instead of closing all the three trades how do I amend the code to close all the three trades?
- EA related issue - Urgent
- [ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4.
- Sleep code
#include <Trade\Trade.mqh> //Instatiate Trades Execution Library #include <Trade\OrderInfo.mqh> //Instatiate Library for Orders Information #include <Trade\PositionInfo.mqh> //Instatiate Library for Positions Information void Close_all() { CTrade m_trade; // Trades Info and Executions library COrderInfo m_order; //Library for Orders information CPositionInfo m_position; // Library for all position features and information //--Đóng Positions for(int i = PositionsTotal() - 1; i >= 0; i--) // loop all Open Positions if(m_position.SelectByIndex(i)) // select a position { m_trade.PositionClose(m_position.Ticket()); // then close it --period Sleep(100); // Relax for 100 ms } //--End Positions //-- Orders for(int i = OrdersTotal() - 1; i >= 0; i--) // loop all Orders if(m_order.SelectByIndex(i)) // select an order { m_trade.OrderDelete(m_order.Ticket()); // then delete it --period Sleep(100); // Relax for 100 ms } //--End //-- Positions for(int i = PositionsTotal() - 1; i >= 0; i--) // loop all Open Positions if(m_position.SelectByIndex(i)) // select a position { m_trade.PositionClose(m_position.Ticket()); // then close it --period Sleep(100); // Relax for 100 ms } //--End }// End func Close_all //+------------------------------------------------------------------+
"I'm sharing with you a function that I often use, which can close all open trades. Note that I perform it twice to ensure certainty."
Mistake, the account is a hedging type so don't need to amend.I have 3 trades of the same symbol opened, code semss to close only one and leaves the other 2.Why?
Peter Kaiza #: Mistake, the account is a hedging type so don't need to amend.I have 3 trades of the same symbol opened, code semss to close only one and leaves the other 2.Why?
Because the PositionSelect() function is mostly for "netting" accounts. Search the CodeBase for publications for closing all trades and study how they work.
Peter Kaiza:
I have this code which should close all trades when a certain condition is met. Normally 3 open trades of the same symbol are open when the condition is met. However, the code closes one trade and leaves 2 others instead of closing all the three trades how do I amend the code to close all the three trades?
I have this code which should close all trades when a certain condition is met. Normally 3 open trades of the same symbol are open when the condition is met. However, the code closes one trade and leaves 2 others instead of closing all the three trades how do I amend the code to close all the three trades?
Take your time to study this code: https://www.mql5.com/en/code/25650
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register