Hi,
I am using a very similar subroutine for one of my multipair EAs and here there is the code:
Code: Select all //declarations
extern bool ProfitAsPercentageOfBalance = true;
extern double PercentageProfit = 1;
//Start()
if(ProfitAsPercentageOfBalance)
{
ProfitPercentage=AccountBalance() *(PercentageProfit/100);
if( OpenProfitPartial()>ProfitPercentage) {Print(OpenProfitPartial());closeOrdersF(OP_SELL);closeOrdersF(OP_BUY);Sleep(60000);}
}
double OpenProfitPartial()
{
double Profit=0.0;
for(int i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(/*OrderSymbol() != Symbol() ||*/ OrderMagicNumber()!=magic) continue;
// if(OrderType() != OrdType) continue;
if((OrderProfit()+OrderSwap()+OrderCommission())<0)continue;
Profit+=(OrderProfit()+OrderSwap()+OrderCommission());
}
return(Profit);
}
int closeOrdersF(int type)
{
int ticket,ordertype,total1=OrdersTotal();
double priceClose;
bool oc,od;
if(total1==0) {Print("Nothing to close"); return(0);}
total1--;
for(int x=total1; x>=0; x--)
{
if(OrderSelect(x,SELECT_BY_POS))
{
ordertype= OrderType();
if(type != ordertype /*|| OrderSymbol() != Symbol()*/||OrderMagicNumber()!=magic) continue;
ticket=OrderTicket();
if ((OrderProfit()+OrderCommission()+OrderCommission())<0) continue;
switch(ordertype)
{
case 0:
priceClose=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),Digits);
Print("Closing order ¹ ",x," ",ticket);
oc=OrderClose(ticket,OrderLots(),priceClose,3,Yellow);
break;
case 1:
priceClose=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),Digits);
Print("Closing order ¹ ",x," ",ticket);
oc=OrderClose(ticket,OrderLots(),priceClose,3,Yellow);
break;
default:
Print("Delete order ¹ ",x," ",ticket);
od=OrderDelete(ticket);
break;
}
}
}
return(0);
}
I am sure that this code can be simplified, but it works for me.
Regards
José Luis |