I tried this code and got the same result you had. 1970.01.01 00.00.00
I tried ETime=TimeCurrent(); and output was correct. I tried ETime=HistoryDealGetInteger(HTicket,DEAL_TIME); and ETime=PositionGetInteger(POSITION_TIME);. Both returned 1970.01.01 00.00.00. This may indicate a problem within MT-5.
datetime LastDealElapsedTime() // return elapsed time from current time to last deal open time { uint pos_total=0; uint total=0; long pos_id=0; ulong HTicket; datetime ETime; pos_total=PositionsTotal(); if(pos_total > 0) { // continue if current open position for(uint i=0;i<pos_total;i++) { if(PositionSelect(Symbol())) // continue if open position is for chart symbol { pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER); HistorySelectByPosition(pos_id); total=HistoryDealsTotal(); if(HTicket==HistoryDealGetTicket(total-1)) // get ticket number for last deal in position ETime=TimeCurrent()-HistoryDealGetInteger(HTicket,DEAL_TIME); return(ETime); } } } return(0); }
- www.mql5.com
The code above is not correct. Try this one
//+------------------------------------------------------------------+ //| return elapsed time from current time to last deal open time | //+------------------------------------------------------------------+ datetime LastDealElapsedTime() { uint pos_total=0; uint total=0; long pos_id=0; ulong HTicket; // where is initialization ? datetime ETime=LONG_MAX; // I added initial default value pos_total=PositionsTotal(); if(pos_total>0) { // continue if current open position if(PositionSelect(Symbol())) // continue if open position is for chart symbol { pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER); HistorySelectByPosition(pos_id); total=HistoryDealsTotal(); ulong ticket=HistoryDealGetTicket(total-1); // get ticket number for last deal in position // Have you checked HTicket and HistoryDealGetTicket(total-1)? Print("HTicket=",HTicket," ticket=",ticket); if(HTicket==ticket) ETime=TimeCurrent()-HistoryDealGetInteger(HTicket,DEAL_TIME); return(ETime); } } return(0); }
The code above is not correct. Try this one
Rosh,
Your code did not produce the desired result. HTicket value is 0.
ETime=TimeCurrent()-HistoryDealGetInteger(HTicket,DEAL_TIME);
I made a few changes and now it appears to work OK.
//+------------------------------------------------------------------+ //| return elapsed time for last deal in current open position | //+------------------------------------------------------------------+ datetime LastDealElapsedTime() { uint pos_total=0; uint total=0; long pos_id=0; ulong HTicket=0; datetime ETime=LONG_MAX; // I added initial default value pos_total=PositionsTotal(); if(pos_total>0) { // continue if current open position if(PositionSelect(Symbol())) // continue if open position is for chart symbol { pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER); HistorySelectByPosition(pos_id); total=HistoryDealsTotal(); HTicket=HistoryDealGetTicket(total-1); // get ticket number for last deal in position if(HTicket>0) ETime=TimeCurrent()-HistoryDealGetInteger(HTicket,DEAL_TIME); return(ETime); } } return(0); }
If you want to monitor deal elapse time as a Comment() output, try this
Comment("Last Deal Elapsed Time ", TimeToString(LastDealElapsedTime(),TIME_MINUTES));
Rosh,
Your code did not produce the desired result. HTicket value is 0
And which value of ticket?
ulong ticket=HistoryDealGetTicket(total-1); // get ticket number for last deal in position
Are you sure that
(HTicket==ticket)
is true?
And which value of ticket?
Are you sure that
is true?
As I tested your code. HTicket had not been assigned a value.
The result for the Print() call was;
2010.09.10 09:48:55 Bogie-Test EA (EURUSD,H1) HTicket=0 ticket=1401967
So this code would not product correct result.
// Have you checked HTicket and HistoryDealGetTicket(total-1)? Print("HTicket=",HTicket," ticket=",ticket); if(HTicket==ticket) // because HTicket=0, ETime is not assign a value ETime=TimeCurrent()-HistoryDealGetInteger(HTicket,DEAL_TIME);
So code needed a few changes similar to this.
// Have you checked HTicket and HistoryDealGetTicket(total-1)? Print("ticket=",ticket); if(ticket>0) ETime=TimeCurrent()-HistoryDealGetInteger(ticket,DEAL_TIME);
As I tested your code. HTicket had not been assigned a value.
The result for the Print() call was;
2010.09.10 09:48:55 Bogie-Test EA (EURUSD,H1) HTicket=0 ticket=1401967
So this code would not product correct result.
So code needed a few changes similar to this.
Can someone help me figure out how to code the elapsed time (in minutes or seconds) between now and the last deal time?
I have written this function for getting the open time.
datetime PositionOpenTime() // open time of first Deal in open position.
{
uint total=0;
long ticket;
datetime OpenTime;
long pos_id;
if(PositionSelect(_Symbol)) // continue if open position for symbol
{
pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER);
HistorySelectByPosition(pos_id);
ticket=HistoryDealGetTicket(1); // get ticket number for 1st trade in open position
OpenTime=HistoryDealGetInteger(ticket,DEAL_TIME);
}
return(OpenTime);
}
I try to use this expression
ElapsedTime = TimeCurrent()-PositionOpenTime()
but it returns dates from June 1970.
Wackena, Are you there to help on this.
Thanks