You reset and re-initialize your 'DateTimeOpenLastOp' variable each time you loop. You need to compare it with another variable.
Note: not an "order", but a "deal". It is important.
Example:
//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ datetime GetLastTradeOpenTime() { datetime time=0; //--- uint TotalNumberOfDeals = HistoryDealsTotal(); ulong TicketNumber = 0 ; long DealType, DealEntry; double DealProfit = 0 ; string MySymbol = "" ; string PositionDirection = "" ; string MyProfit = "" ; double Volume = 0 ; datetime DateTimeOpenLastOp = "" ; HistorySelect(0, TimeCurrent()); for(uint i = 0 ; i < TotalNumberOfDeals; i++) { if((TicketNumber = HistoryDealGetTicket(i)) > 0) { DealProfit = HistoryDealGetDouble(TicketNumber, DEAL_PROFIT); DealType = HistoryDealGetInteger(TicketNumber, DEAL_TYPE); MySymbol = HistoryDealGetString(TicketNumber, DEAL_SYMBOL); DealEntry = HistoryDealGetInteger(TicketNumber, DEAL_ENTRY); Volume = HistoryDealGetDouble(TicketNumber, DEAL_VOLUME); DateTimeOpenLastOp = HistoryDealGetInteger(TicketNumber, DEAL_TIME); if(MySymbol == _Symbol) { if(DealType == DEAL_TYPE_BUY || DealType == DEAL_TYPE_SELL) { if(DealEntry == DEAL_ENTRY_OUT) { if(DealType == DEAL_TYPE_BUY) { PositionDirection = "SELL-TRADE" ; } if(DealType == DEAL_TYPE_SELL) { PositionDirection = "BUY-TRADE" ; } if(DateTimeOpenLastOp>time) { time=DateTimeOpenLastOp; } } } } } } //--- return(time); } //+------------------------------------------------------------------+
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi everybody,
I'am are very new in mql5, i try to develop a EA but i need get the datetime for last open trade. I have the next code but i get forever "2020-07-22 01:00:00" without work.
Thank you,
Regards