Good morning friends,
I am building a function to know if the last closed deal by an expert advisor was profitable. So I am iterating deals until finding one that matches the magic number, type and deal comment desired. However, I am having trouble using the HistorySelect() function, as I don’t want to use any start/end date (I don’t know if the last trade was recent or not). This is what I have so far.
bool LastPositionWon(ENUM_DEAL_TYPE Type, string COMMENT)
{
HistorySelect(0, 0); // Problem here!
int l_deals = HistoryDealsTotal();
for (int i = 0; i < l_deals; i++)
{
// Ticket
ulong l_ticket = HistoryDealGetTicket(i);
// Magic number
int l_magic = (int) HistoryDealGetInteger(l_ticket, DEAL_MAGIC);
// Type
ENUM_DEAL_TYPE l_type = (ENUM_DEAL_TYPE) HistoryDealGetInteger(l_ticket, DEAL_TYPE);
// Comment
string l_comment = HistoryDealGetString(l_ticket, DEAL_COMMENT);
// Entry type
ENUM_DEAL_ENTRY entry_type = (ENUM_DEAL_ENTRY) HistoryDealGetInteger(l_ticket, DEAL_ENTRY);
// Evaluate and return
if(entry_type == DEAL_ENTRY_OUT && l_type == Type && l_comment == COMMENT && l_magic == MagicNumber)
{
double profit = HistoryDealGetDouble(l_ticket, DEAL_PROFIT);
if(profit > 0)
{
return(true);
} else {
return(false);
}
}
}
return(true);
}
I am pretty sure there is an easier way to do this. Could you please point me in the right direction?
Thanks in advance!