Getting how long the last closed position took

 

Hello coders. I am try to get how long the last closed position took in milliseconds,  from entry to exit. The code below doesn't seem to produce what I want. Please advise. I am beginner in MQL5, so bear with me.

struct DEALTIME{
   long dealentryIn;
   long dealentryOut;
};

DEALTIME time;

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  { }
//+------------------------------------------------------------------+
void OnTick()
  {
//--- TimeDealEntryIn for last closed deal
  if(OpenPositions()<=0){
  HistorySelect(0, TimeCurrent());
  if(HistoryDealsTotal()>0){
  for(int i=HistoryDealsTotal()-1; i>=0; i--){
      ulong dnumber= HistoryDealGetTicket(i);
      if(HistoryDealSelect(dnumber)){
      if(HistoryDealGetInteger(dnumber,DEAL_MAGIC)==magiciID){
      if(HistoryDealGetString(dnumber,DEAL_SYMBOL)==_Symbol){
      if(HistoryDealGetInteger(dnumber,DEAL_ENTRY)==DEAL_ENTRY_IN){
       time.dealentryIn = HistoryDealGetInteger(dnumber,DEAL_TIME_MSC);
             break;
      }
      }
      }
      }
  }
  }
  }
//--- TimeDealEntryOut for last closed deal
  if(OpenPositions()<=0){
  HistorySelect(0, TimeCurrent());
  if(HistoryDealsTotal()>0){
  for(int i=HistoryDealsTotal()-1; i>=0; i--){
      ulong dnumber= HistoryDealGetTicket(i);
      
      if(HistoryDealSelect(dnumber)){
      if(HistoryDealGetInteger(dnumber,DEAL_MAGIC)==magiciID){
      if(HistoryDealGetString(dnumber,DEAL_SYMBOL)==_Symbol){
      if(HistoryDealGetInteger(dnumber,DEAL_ENTRY)==DEAL_ENTRY_OUT){
       time.dealentryOut = HistoryDealGetInteger(dnumber,DEAL_TIME_MSC);
             break;
      }
      }
      }
      }
  }
  }
  }
   long timetakenMSC = TimeTakenClosedPos(time.dealentryOut,time.dealentryIn);
      
      Comment("TimeIn: ", time.dealentryIn, "\nTimeOut: ",
       time.dealentryOut, "\nTimeTaken: ", timetakenMSC);
  }
//+------------------------------------------------------------------+

//CountOpenPositions
 int OpenPositions(){
 
   int poscount=0;
   
 if(PositionsTotal()>0){
    int positionCount= PositionsTotal();
    
   for(int i=positionCount-1; i>=0; i--){
      ulong pNumber = PositionGetTicket(i);
      
      if(PositionSelectByTicket(pNumber)){
      if(PositionGetInteger(POSITION_MAGIC)==magiciID){
      if(PositionGetString(POSITION_SYMBOL)==_Symbol){
         poscount++;
      }
      }
      }
   
   }
 
 }
  return poscount;
 }
 
//TimeItTookToClosePosition 
long TimeTakenClosedPos(long x, long y){
    long timetaken = x-y;
   return timetaken;
 }
 

Learn to search before learning to code as there's virtually nothing that hasn't already been programmed and is ready for you!

    => Search in the articles: https://www.mql5.com/en/articles
    => Search in the codebase: https://www.mql5.com/en/code
    => Search in general: https://www.mql5.com/en/search or via Google with: "site:mql5.com .." (forgives typos and variants)

E.g. here you find the code to get everything from the history: https://www.mql5.com/de/code/27635

Just copy and paste what you want from that. This way you start with a working code...

MQL5 Articles
MQL5 Articles
  • www.mql5.com
MQL5 Programming Articles
 
Carl Schreiber #:

Learn to search before learning to code as there's virtually nothing that hasn't already been programmed and is ready for you!

    => Search in the articles: https://www.mql5.com/en/articles
    => Search in the codebase: https://www.mql5.com/en/code
    => Search in general: https://www.mql5.com/en/search or via Google with: "site:mql5.com .." (forgives typos and variants)

E.g. here you find the code to get everything from the history: https://www.mql5.com/de/code/27635

Just copy and paste what you want from that. This way you start with a working code...

Thanks for the help!