function to return "LAST CLOSED LOSS TRADE's Volume (Lot size) "

 

Hi

I am building an EA.

Please someone help to select or write a function to return  "LAST CLOSED ORDER's Volume (Lot size) " using EA's Magic number and currency pair. It would be much better if someone can show me to get "lot Size" of the "closed LOSS TRADE" only (dont need Profitable trade's lot size) . I appriciate any help. Thank you

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Sudha365:

Hi

I am building an EA.

Please someone help to select or write a function to return  "LAST CLOSED ORDER's Volume (Lot size) " using EA's Magic number and currency pair. It would be much better if someone can show me to get "lot Size" of the "closed LOSS TRADE" only (dont need Profitable trade's lot size) . I appriciate any help. Thank you

void OnTick() {
   CDealInfo info;
   datetime recent_time = 0; 
   ulong recent_ticket = NULL;
   HistorySelect(0, TimeCurrent());
   for (int i=HistoryDealsTotal()-1; i>=0; i--) {
      if (info.SelectByIndex(i) && info.Entry() == DEAL_ENTRY_OUT && info.Profit() < 0.0 && info.Time() > recent_time) {
         recent_time = info.Time();
         recent_ticket = info.Ticket();
      }
   }
   if (recent_ticket != NULL) {
      info.Ticket(recent_ticket);
      double profit = info.Profit();
      printf("Last loss-trade profit = %.2f", profit);
   }
}
 

Here you are


double last_deal_volume(const string symbol,const int magicnumber)
  {
// --- determine the time intervals of the required trading history
   datetime end=TimeCurrent();                 // current server time
   datetime start=end-PeriodSeconds(PERIOD_H1) *Hours_Order_lookback;// set the beginning time to 24 hours ago

//--- request in the cache of the program the needed interval of the trading history
   HistorySelect(start,end);
//--- obtain the number of deals in the history
   int deals=HistoryDealsTotal();

   int returns=0;
   double profit=0;
   double loss=0;
   double deal_volume=0;
//--- scan through all of the deals in the history
   if(deals>0)
     {
      for(int i=deals-1;i<deals;i++)
         //+------------------------------------------------------------------+
         //|                                                                  |
         //+------------------------------------------------------------------+
        {
         //--- obtain the ticket of the deals by its index in the list
         ulong deal_ticket=HistoryDealGetTicket(i);
         if(deal_ticket>0) // obtain into the cache the deal, and work with it
           {
            string deal_symbol=HistoryDealGetString(deal_ticket,DEAL_SYMBOL);
            ulong time=HistoryDealGetInteger(deal_ticket,DEAL_TIME);
            ulong order               =HistoryDealGetInteger(deal_ticket,DEAL_ORDER);
            long order_magic          =HistoryDealGetInteger(deal_ticket,DEAL_MAGIC);
            long pos_ID               =HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID);


            //--- process the deals with the indicated DEAL_MAGIC
            if(order_magic==magicnumber && deal_symbol==symbol)
              {
               deal_volume=HistoryDealGetDouble(deal_ticket,DEAL_VOLUME);
              }

            //--- calculate the losses and profits with a fixed results

           }
         else // unsuccessful attempt to obtain a deal
           {
            PrintFormat("We couldn't select a deal, with the index %d. Error %d",
                        i,GetLastError());
           }
        }
     }
//--- output the results of the calculations
   return(deal_volume);
  }
 
      for(int i=deals-1;i<deals;i++)
That loop will only process one deal.
Reason: