how can i get order profit from mulitple closed orders

 

using this code:

but not working for some reason...help... each time returns last profit, but not multiple closed orders, which closed at the same time.

/

/------------

 double LastProfit(int tip)

     {

      double cnt=0;

      for(int i=OrdersHistoryTotal();i>=0;i--)

        {

         if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

         if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber&&tip==OrderType())

           {

            cnt+=(OrderProfit()+OrderCommission()+OrderSwap());

            Print("profit history ",cnt);

            return(cnt);

            

           }

        }

      return(cnt);

     }
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. I have moved this thread.

  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 

Remove this line:

/------------

 double LastProfit(int tip)

     {

      double cnt=0;

      for(int i=OrdersHistoryTotal();i>=0;i--)

        {

         if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

         if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber&&tip==OrderType())

           {

            cnt+=(OrderProfit()+OrderCommission()+OrderSwap());

            Print("profit history ",cnt);

            return(cnt);

            

           }

        }

      return(cnt);

     }