Hi ...
I use this code in the most of my EA's to protect profit is the account profit reach a specific $ but not pips.
The code is not recomended to add to EA's which are attached to plateform with other EA's because it has no MAGIC No.
You can add MAGIC No. easily if you like.
extern int MaxProfitPerPair = 7; //in Dollars
...
...
...
if(AccountProfit()>= MaxProfitPerPair * OrdersTotal())
CloseAllPositions();
...
...
...
void CloseAllPositions(){
int total = OrdersTotal();
for(int i=total-1;i>=0;i--){
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
|