This is my profit protector code:
PHP Code:
extern bool ProtectProfit= true;
extern double ProfitToProtect = 150;
int start()
{
...
if(ProtectProfit)
ProfitProtect(ProfitToProtect);
....
}
void ProfitProtect(double profit)
{
int total = OrdersTotal();
double MyCurrentProfit=0;
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == MagicNumber)
MyCurrentProfit += OrderProfit();
}
Print("My Current Profit is : " + DoubleToStr(MyCurrentProfit,2) + " While My Profit Target is " + DoubleToStr(profit,2));
if(MyCurrentProfit>=profit)
CloseAll();
}
void CloseAll()
{
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == MagicNumber)
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet);
}
}