cycle function to pick the most losing trade.
-
- Posts: 6
- Joined: Tue Nov 15, 2011 10:06 pm
cycle function to pick the most losing trade.
hello everyone. i would love and appreciate if someone can write a cycle code of a function which picks the most losing trades among the open trades (or the most profitable) for closure.
-
- Trader
- Posts: 50
- Joined: Wed Nov 09, 2016 7:50 pm
- Location: South Africa
cycle function to pick the most losing trade.
Code: Select all
#define MAXTRY 10 //--- Max number of Order Functions retries
Code: Select all
void CloseMaxLossTicket(string _message)
{
int _ticket=0;
double _max_loss=0;
for(int apos=0; apos<OrdersTotal(); apos++)
{
if(OrderSelect(apos,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderMagicNumber()==p_MagicNo && OrderType()<=OP_SELL)
{
if(OrderProfit()<_max_loss)
{
_max_loss=OrderProfit();
_ticket=OrderTicket();
}
}
}
}
if(_ticket>0)
{
if(OrderSelect(_ticket,SELECT_BY_TICKET)==true)
{
if(OrderMagicNumber()==p_MagicNo && OrderType()<=OP_SELL)
{
string _symbol=OrderSymbol();
for(int _try=1; _try<=MAXTRY; _try++)
{
double _close=0;
if(OrderType()==OP_BUY) _close=MarketInfo(_symbol,MODE_BID);
else if(OrderType()==OP_SELL) _close=MarketInfo(_symbol,MODE_ASK);
if(OrderClose(_ticket,OrderLots(),_close,50,clrNONE)==true)
{
Print("CloseMaxLossTicket SUCCESS: Magic # ",p_MagicNo," Ticket # ",_ticket," [",_symbol,"]");
Print(_message);
return;
}
else
{
if(_try==MAXTRY) Print("CloseMaxLossTicket ERROR: Magic # ",p_MagicNo," maximum attempts reached # ",MAXTRY," [",_symbol,"]");
else Print("CloseMaxLossTicket ERROR: Magic # ",p_MagicNo," Ticket # ",_ticket," Error # ",GetLastError()," [",_symbol,"]");
}
Sleep(3000); //--- Wait 3 seconds before retrying to close the open order
}
}
}
}
}
-
- Posts: 6
- Joined: Tue Nov 15, 2011 10:06 pm
cycle function to pick the most losing trade.
THANKS a lot Alecsa !!!
amazed by the speed of your reply, i'm grateful.
amazed by the speed of your reply, i'm grateful.