OrderHistroySelect MQL5

 

Hello,

 

i try to change my mode from the netting mode to the Hedge mode, so i want to selct the histroy order with MagicNumber and Symbol,

i use follofing code

 

double last_deal_volume(const string symbol, const int magic)
  {
  double deal_price;
  double deal_volume;
  double deal_profit;
    
// --- time interval of the trade history needed
   datetime end=TimeCurrent();                 // current server time
   datetime start=end-PeriodSeconds(PERIOD_D1)*Days_Order_loockback;// decrease 1 day
//--- request of trade history needed into the cache of MQL5 program
   HistorySelect(start,end);
//--- get total number of deals in the history
   int deals=HistoryDealsTotal();
//--- get ticket of the deal with the last index in the list
   ulong deal_ticket=HistoryDealGetTicket(deals-1);
   if(deal_ticket>0) // deal has been selected, let's proceed ot
     {

      //--- ticket of the order, opened the deal
      ulong  order=HistoryDealGetInteger(deal_ticket,DEAL_ORDER);
      string deal_symbol=PositionGetString(POSITION_SYMBOL);
      long   order_magic=HistoryDealGetInteger(deal_ticket,DEAL_MAGIC);
      long   pos_ID=HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID);
      
             deal_price=HistoryDealGetDouble(deal_ticket,DEAL_PRICE);
             deal_volume=HistoryDealGetDouble(deal_ticket,DEAL_VOLUME);
             deal_profit=HistoryDealGetDouble(deal_ticket,DEAL_PROFIT);
    //  PrintFormat("Deal: #%d opened by order: #%d with ORDER_MAGIC: %d was in position: #%d price: #%f volume: #%f",
    //              deals-1,order,order_magic,pos_ID,deal_price,deal_volume);
      
     }
   else              // error in selecting of the deal
     {
    
      PrintFormat("Total number of deals %d, error in selection of the deal"+
                  " with index %d. Error %d",deals,deals-1,GetLastError());
     }
   return(deal_volume);
  }

does anyone have an idea, that i get only the volume for the last trade in with my MagicNumer in the Symbol back?

 

amando 

 
You can't change the Hedging or netting mode on your account yourself.
Contact the Service-Desk of your Broker, he will do it for you, if he offers this mode.

Otto
 
Otto Pauser:
You can't change the Hedging or netting mode on your account yourself.
Contact the Service-Desk of your Broker, he will do it for you, if he offers this mode.

Otto

i can with my broker, and i want a solution for this problem fist

 
MQL5-code
// https://www.mql5.com/de/code/16006
#include <MT4Orders.mqh> // Last version - https://www.mql5.com/ru/code/16006

double last_deal_volume( const string symbol, const int magic = 0 )
{
  double Res = 0;
  
  for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderType() <= OP_SELL) &&
         (OrderSymbol() == symbol) && (OrderMagicNumber() == magic))
    {
      Res = OrderLots();
      
      break;
    }
  
  return(Res);
}

More examples.