I am trying to create an Expert Adviser (EA) in MQL4 language.
How to code a function that returns the largest losing trade, (and not the total losing trades)?
The following function will return the ticket of the largest loss trade.
With a default of loss = DBL_MAX
, this can still return profitable trades with the lowest profit.
With a loss = 0
, it will only return a trade with the most negative profit or zero.
Will return a ticket of such trade and EMPTY, if no trades found.
int LargestLoss( int magic, double loss = DBL_MAX )
{
int ticket=EMPTY;
for(int i=0; i < Orderstotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderProfit()+OrderSwap()+OrderCommision()<loss)
{
loss=OrderProfit()+OrderSwap()+OrderCommision();
ticket=OrderTicket();
}
}
}
return ticket;
}