Coding: Last order open and last order closed

 

Hello 

Iam very new to coding  and trying to make an ea.

i need to get info about last open trade and also last closed

like Open time .Close time . Type of trade etc


and i have made this code it's not working  is there a better way to get these info

double lastOrderInfo(string N, int type)

      {

            for(int i=OrdersTotal()-1;i>=0;i--)

            {

                  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

                  if(OrderSymbol()==Symbol()  && OrderType()==type)

                  {

                            if(N=="Type")               return(OrderType());

                       else if(N=="SL")                 return(OrderStopLoss());

                       else if(N=="TP")                 return(OrderTakeProfit());

                       else if(N=="Lots")               return(OrderLots());

                       else if(N=="Price")              return(OrderOpenPrice());

                       else if(N=="Time")                return(OrderOpenTime());

                  

                  

                  }

            }return(0);

            }
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the...
 
Please edit your post and
use the code button (Alt+S) when pasting code
 
Keith Watford:
Please edit your post and
use the code button (Alt+S) when pasting code
Done
 
            for(int i=OrdersTotal()-1;i>=0;i--) if(
               OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            && OrderSymbol()==Symbol()  && OrderType()==type){

               if(N=="Type")               return(OrderType());
The above code only selects the last proper type in the list. See № 2.
  1. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum 2017.11.30

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum 2012.04.21
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 2020.06.08

  3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum 2017.11.01

    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

Reason: