How to get my last order profit?

 
I want to get the last order profit,but HistoryDealGetDouble gets the last order profit is still zero.
input long order_magic=55555;
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
      
//---
   return(0);
  }
void OnTimer()
  {
//---
     static ulong recdd=0,tmprecdd=0,ticket,record=0,tmprecord=0,tmprecord2=0;
     int i,j,ih,hstTotal,cnt;
     uint total;
     double prof;
     MqlDateTime str1,str2;
    static double profit=0,stoplose=0; 
    static double nowkj;  
    datetime date1,date2;
    MqlTradeRequest request;     
   MqlTradeResult result;
    CTrade  trade;
     COrderInfo  order; 
    CPositionInfo position;
    bool success;
    
    HistorySelect(0,TimeCurrent());
    hstTotal=HistoryOrdersTotal();  //get history orders
    total = PositionsTotal();       //get pengding order
      if(hstTotal>0&&total==0)
   {
        for(ih=hstTotal-1;ih<hstTotal;ih++)
         {
             tmprecord=HistoryOrderGetTicket(ih);
           
             if(record!=tmprecord)
             {               
                record=tmprecord;               
                prof=HistoryDealGetDouble(record,DEAL_PROFIT);
                Print("record:",record," closeprice:",HistoryOrderGetDouble(record,ORDER_PRICE_OPEN)," prof:",prof);
             }
             
            
         
           }
     }
     
     
     if(total<1)
     {
     //TimeCurrent(str2);

    date1=D'2012.01.02 02:00';
    date2=D'2012.01.02 02:10';
   // Print(" total:",total);
               if(TimeCurrent()>=date1&&TimeCurrent()<=date2)
               {
                    profit=0;
                    stoplose=0;      
                    nowkj=SymbolInfoDouble(Symbol(),SYMBOL_ASK);             
                    ZeroMemory(request);
                    ZeroMemory(result);
                    request.action=TRADE_ACTION_DEAL;         
                    request.magic=order_magic;                 
                    request.symbol=_Symbol;                      
                    request.volume= 0.1  ;                       
                    request.sl=profit;                              
                    request.tp=stoplose;                             
                    request.type=ORDER_TYPE_BUY;
                    request.price=nowkj;
                    request.deviation =3;
                    success= OrderSend(request,result);
                    return;
               }
         
     } 
     
      for(cnt=0; cnt < total; cnt++) 
          {
              ticket=HistoryOrderGetTicket(cnt);
              order.Select(ticket);
           date1=D'2012.01.04 16:00';
           date2=D'2012.01.04 16:01';
            if(TimeCurrent()>=date1&&TimeCurrent()<=date2)
               {
               if(position.Select(_Symbol))
               {
                      trade.PositionClose(_Symbol,3);
                      return;
               }
               }
           }
        
  }

run the above code from 2012.1.1 to 2012.1.10  in "AUDUSD"  symbol, The log is: 

18:45:30        2012.01.02 02:00:00   instant buy 0.10 AUDUSD at 1.02258 (1.02208 / 1.02258 / 1.02208)                    
18:45:30        2012.01.02 02:00:00   deal #2 buy 0.10 AUDUSD at 1.02258 done (based on order #2)                         
18:45:30        2012.01.02 02:00:00   deal performed [#2 buy 0.10 AUDUSD at 1.02258]                                      
18:45:30        2012.01.02 02:00:00   order performed buy 0.10 at 1.02258 [#2 buy 0.10 AUDUSD at 1.02258]                 
18:45:30        2012.01.04 16:00:00   instant sell 0.10 AUDUSD at 1.03402 (1.03402 / 1.03420 / 1.03402)                   
18:45:30        2012.01.04 16:00:00   deal #3 sell 0.10 AUDUSD at 1.03402 done (based on order #3)                        
18:45:30        2012.01.04 16:00:00   deal performed [#3 sell 0.10 AUDUSD at 1.03402]                                     
18:45:30        2012.01.04 16:00:00   order performed sell 0.10 at 1.03402 [#3 sell 0.10 AUDUSD at 1.03402]               
18:45:30        2012.01.04 16:00:00   CTrade::OrderSend: instant sell 0.10 AUDUSD at 1.03402 [done at 1.03402]            
18:45:30        2012.01.04 16:01:00   record:3 closeprice:1.03402 prof:0.0                                                
18:45:30        final balance 10115.66                                                                                    
18:45:30        OnTester result 0                                                                                         
18:45:30        AUDUSD,H1: 39980 ticks (168 bars) generated within 125 ms (total bars in history 6379, total time 188 ms) 

Thank a lot in advance for any help.

 

 

 
Same problem for me. I created my own variable that I instanciate at each new order and delete when position is closed.
 

Hi,

 

I am not sure if this helps. I wrote a function that returns %-profit for my current (open!) position.
CMySignal is derived from CExpertSignal Class.

//+------------------------------------------------------------------+
//|   return current position %-profit for last deal in current      |
//|   open position                                                  |
//+------------------------------------------------------------------+
double CMySignal::GetPositionProfitPercent()
  {
   uint pos_total=0; 
   double curProfit=0.0; // 
   pos_total=PositionsTotal();
   if(pos_total>0)
     { // continue if current open position

      if(PositionSelect(Symbol())) // continue if open position is for chart symbol
        {
         curProfit=(PositionGetDouble(POSITION_PRICE_CURRENT)/PositionGetDouble(POSITION_PRICE_OPEN)-1.0)*100.0;
        }

     }
   return(curProfit);
  }
Reason: