-
Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
Forum rules and recommendations - General - MQL5 programming forum (2023)
Messages Editor - GHH_tug77: I want close all position except the most old one.
Isn't the oldest one, position zero?
-
Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
PositionClose is not working - MQL5 programming forum (2020)
MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)You need one Magic Number for each symbol/timeframe/strategy.
Trade current timeframe, one strategy, and filter by symbol requires one MN.
If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)
-
Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
Forum rules and recommendations - General - MQL5 programming forum (2023)
Messages Editor -
Isn't the oldest one, position zero?
-
Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
PositionClose is not working - MQL5 programming forum (2020)
MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)You need one Magic Number for each symbol/timeframe/strategy.
Trade current timeframe, one strategy, and filter by symbol requires one MN.
If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)
Imagine I have 10 open positions. I want to close 9 positions and leave 1 position which opened firstly.
I edited your code because it was short. However, in future, please insert your code properly.
Usee the CODE button (Alt-S) when inserting code.
- www.mql5.com
I edited your code because it was short. However, in future, please insert your code properly.
Usee the CODE button (Alt-S) when inserting code.
thank you for that. Sorry for not properly code format.
- MT5: Begin learning to code it.
If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.
Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
Hiring to write script - General - MQL5 programming forum (2019)We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
No free help (2017) -
The first position is position index zero. If you don't want to close that, test for the index being zero and don't close or change your loop to not process position zero.
I can only explain it to you; I can't understand it for you.
-
Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
No free help (2017)Or pay someone. Top of every page is the link Freelance.
Hiring to write script - General - MQL5 programming forum (2018)We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
No free help (2017)
-
The first position is position index zero. If you don't want to close that, test for index zero or change your loop to not process position zero.
I can only explain it to you; I can't understand it for you.
-
Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
No free help (2017)Or pay someone. Top of every page is the link Freelance.
Hiring to write script - General - MQL5 programming forum (2018)We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
No free help (2017)
okey okey I changed the code and inserted it. Please check it? Is it correct? Now it leaves the most old position?
void CloseAllPositionsPart() { m_trade.SetAsyncMode(true); for(int i=PositionsTotal()-1; i>=1; i--) //I changed 0 to 1 if(m_position.SelectByIndex(i)) { if(!m_trade.PositionClosePartial(m_position.Ticket(),0.04,-1)); } }
Your code may work, I am unsure. I did not check it.
However, for my own coding, I would not assume that position 0 is the "oldest", but that is just my own thoughts.
Instead, I would first loop over all the positions, verifying the "opening time" of each position, to decide which is the oldest, and keep track of that ticket.
After that, I would then loop over the positions once more to close all the positions, except for the one with the ticket number I tracked in the previous loop.
This may add an extra loop, but it would be safer, as it does not assume any predefined sequence of the positions.
As an extra note, I would suggest reading the following article if you have not done so already ...
Orders, Positions and Deals in MetaTrader 5
MetaQuotes, 2011.02.01 16:13
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
By the way, if you intend to close a position completely, then use the PositionClose method instead.
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
First of all thank you for helping. I am newbie in mql5 coding. How to close all position except most old one? I want close all position except the most old one. I have a code of closing all position by lot. I attached it.