- Deal profits of the last 3 closed orders
- Why does this code does not work?
- MQL5 - History Deals not updating
Forum on trading, automated trading systems and testing trading strategies
OrderCloseTime Expert Advisor MQL5
fxsaber, 2018.07.06 00:49
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 void LastTimeMQL4( datetime &OpenTime, datetime &CloseTime ) { for (int i = OrdersHistoryTotal() - 1; i >= 0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderType() <= OP_SELL)) { OpenTime = OrderOpenTime(); CloseTime = OrderCloseTime(); break; } } void LastTimeMQL5( datetime &OpenTime, datetime &CloseTime ) { if (HistorySelect(0, INT_MAX)) { for (int i = HistoryDealsTotal() - 1; i >= 0; i--) { const ulong Ticket = HistoryDealGetTicket(i); if (HistoryDealGetInteger(Ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT) { CloseTime = (datetime)HistoryDealGetInteger(Ticket, DEAL_TIME); if (HistorySelectByPosition(HistoryDealGetInteger(Ticket, DEAL_POSITION_ID))) OpenTime = (datetime)HistoryDealGetInteger(HistoryDealGetTicket(0), DEAL_TIME); break; } } } }
Thanks,
So how do I access the deal profit figure outside the function?
Sorry not familiar with the void type function. Just below, on the on tick section of my EA I want to retrieve order profit and close time but I'm getting error 'undeclared identifier' even if i declare it, the programme doesn't compute the profit.
I just want to do this( if lastdeal loss) ...then buy.. etc
Please help
You need to change the function from fxsaber to return the ticket like this:
bool LastClosedDeal(ulong &ticket, datetime &closeTime) { bool found=false; if (HistorySelect(0,TimeCurrent())) { for (int i=HistoryDealsTotal()-1; i>=0; i--) { ticket=HistoryDealGetTicket(i); if (HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT) { found=true; closeTime=(datetime)HistoryDealGetInteger(Ticket,DEAL_TIME); break; } } } return found; }
and then invoke it to get the ticket of it:
ulong ticket; datetime closeTime; if(LastClosedDeal(ticket, closeTime)) { // check if it's been a loss ... }
To check for a loss you need to take the ticket and find out if it had profit with DEAL_PROFIT.
Please help
Forum on trading, automated trading systems and testing trading strategies
OrderCloseTime Expert Advisor MQL5
fxsaber, 2018.07.05 22:39
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 void OnStart() { if (OrderSelect(OrdersHistoryTotal() - 1, SELECT_BY_POS, MODE_HISTORY)) { Print(OrderCloseTime()); Print(OrderProfit()); OrderPrint(); } }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use