how to close all position except the most old one?

 

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.

void CloseAllPositionsPart()
   {      
      m_trade.SetAsyncMode(true);
      for(int i=PositionsTotal()-1; i>=0; i--) 
         if(m_position.SelectByIndex(i))     
           {
               if(!m_trade.PositionClosePartial(m_position.Ticket(),0.04,-1)); // close a position by the specified m_symbol                    
           }   
   }

Improperly formatted code edited by moderator.

 
  1. 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

  2. GHH_tug77: I want close all position except the most old one. 

    Isn't the oldest one, position zero?

  3. 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)

 
William Roeder #:
  1. 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

  2. Isn't the oldest one, position zero?

  3. 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.

 
GHH_tug77 #: Imagine I have 10 open positions. I want to close 9 positions and leave 1 position which opened firstly.

Asked and previously answered. #1.2 Did you miss it, was it not clear, or are you ignoring it?

 
GHH_tug77: 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.

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.

Code button in editor

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
William Roeder #:

Asked and previously answered. #1.2 Did you miss it, was it not clear, or are you ignoring it?

Honestly i am newbie in mql5. I do not fully understand answer of #1.2. Please help? 

 
Fernando Carreiro #:

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.

 
GHH_tug77 #: Honestly i am newbie in mql5. I do not fully understand answer of #1.2. Please help? 
  1. 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)

  2. 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.

  3. 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)


 
William Roeder #:
  1. 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.

  2. 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));   
           }   
   }
 
GHH_tug77 #: okey okey I changed the code and inserted it. Please check it? Is it correct? Now it leaves the most old position?

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 ...

Articles

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.

 
GHH_tug77: 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.

By the way, if you intend to close a position completely, then use the PositionClose method instead.

Documentation on MQL5: Standard Library / Trade Classes / CTrade / PositionClose
Documentation on MQL5: Standard Library / Trade Classes / CTrade / PositionClose
  • www.mql5.com
PositionClose(const string,ulong) - CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: