Thread: Protect Profit
View Single Post
  #5 (permalink)  
Old 12-05-2006, 03:07 PM
oshaban oshaban is offline
Junior Member
 
Join Date: Nov 2005
Posts: 19
oshaban is on a distinguished road
Lightbulb

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);
}
Reply With Quote