Please Help me add % Partial Close Lots to this EA

 

Please can some add close % of lots when profit hits a certain target in pips to this EA!

Struggling to find out how to do it.

Code Below


//+------------------------------------------------------------------+
//|                                           EM.mq4                                |
//|                                                                                       |
//+------------------------------------------------------------------+
#define RUNS 20
#define SLEEP 1000
enum ENUM_TIMEFRAMES_MY
  {
   //None    = -1,
   Current = 0,
   M1      = 1,
   M5      = 5,
   M15     = 15,
   M30     = 30,
   H1      = 60,
   H4      = 240,
   D1      = 1440,
   W1      = 10080,
   MN1     = 43200
  };
//--- input parameters
extern ENUM_TIMEFRAMES_MY TF=0; //Time Frame
extern double    PartialTP=20.0; //Take Profit Level
extern double    BreakEvenAdd=5.0; //Pips Above Break Even
extern double    TrailingStop=0.0; //Trailing Stop
extern double    LotPartClose=0.03; //Lots to Close
extern ENUM_TIMEFRAMES_MY TimeFrameFractal=0; //Time Frame Fractals
extern bool      FractalTrail=false; //Fractals Trailing Stop
extern double    PipGap=5.0; //Pip Gap
extern int       Slippage=3;
/*extern*/ int       MagicNumber=0; //manua orders only
bool InitRun;
double mlt=1;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   mlt=1;
   
   if(Digits==5 || Digits==3 || Digits==1) //5-digit broker
     {
      mlt *= 10;
      PartialTP *= 10;
      TrailingStop *= 10;
      BreakEvenAdd *= 10;
      PipGap *= 10;
     }
   
   InitRun=true;
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//----
   Comment("");
//----
   return;
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   if(PartialTP>0.0)
    {
     BreakEven(MagicNumber,PartialTP,BreakEvenAdd);
     if(LotPartClose>0.0) PartialClose(MagicNumber,PartialTP,LotPartClose/*,Comm*/);
     if(TrailingStop>0.0) TrailingStopRegular(MagicNumber,TrailingStop,0.0,PartialTP);
     if(FractalTrail) TrailingStopFractal(MagicNumber,TimeFrameFractal,PipGap,PartialTP);
    }
   
   InitRun=false;
//----
   return(0);
  }
//+------------------------------------------------------------------+
double ND(double val)
  {
   return(NormalizeDouble(val, Digits));
  }
//+------------------------------------------------------------------+
string ErrorToString(int Error)
  {
   switch(Error)
     {
      case 1: return("No error returned, but the result is unknown.");
      case 2: return("Common error.");
      case 3: return("Invalid trade parameters.");
      case 4: return("Trade server is busy.");
      case 5: return("Old version of the client terminal.");
      case 6: return("No connection with trade server.");
      case 7: return("Not enough rights.");
      case 8: return("Too frequent requests.");
      case 9: return("Malfunctional trade operation.");
      case 64: return("Account disabled.");
      case 128: return("Trade timeout.");
      case 129: return("Invalid price.");
      case 130: return("Invalid stops.");
      case 131: return("Invalid trade volume.");
      case 132: return("Market is closed.");
      case 133: return("Trade is disabled.");
      case 134: return("Not enough money.");
      case 135: return("Price changed.");
      case 136: return("Off quotes.");
      case 137: return("Broker is busy.");
      case 138: return("Requote.");
      case 139: return("Order is locked.");
      case 140: return("Long positions only allowed.");
      case 141: return("Too many requests.");
      case 145: return("Modification denied because order too close to market.");
      case 146: return("Trade context is busy.");
      case 147: return("Expirations are denied by broker.");
      case 148: return("The amount of open and pending orders has reached the limit set by the broker.");
      case 149: return("An attempt to open a position opposite to the existing one when hedging is disabled.");
      case 150: return("An attempt to close a position contravening the FIFO rule.");
      default:  return(StringConcatenate("Error #",Error));
     }
  }
//+------------------------------------------------------------------+
void BreakEven(int MagNum, double BE_Level, double BE_add=0.0)
  {
   if(BE_Level == 0) return;
   int error;
   int total = OrdersTotal();
   for(int i=total-1;i>=0;i--)
     {
     if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
     int Type=OrderType();
     if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagNum && Type<=OP_SELL)
       {
       double OrderSL=ND(OrderStopLoss());
       double OpenPrice=ND(OrderOpenPrice());
       double NewSL=0;
       if(Type==OP_BUY)
         {
          if(ND((Bid-OpenPrice)/Point)>=BE_Level && (OpenPrice>OrderSL || OrderSL==0))
            {
             NewSL=ND(OpenPrice+BE_add*Point);
             if(NewSL>OrderSL || OrderSL==0)
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),ND(NewSL),OrderTakeProfit(),0,Yellow))
                 {error=GetLastError(); Print(Symbol()," Break Even error: ",ErrorToString(error));}
             continue;
            }
         }
       if(Type==OP_SELL)
         {
          if(ND((OpenPrice-Ask)/Point)>=BE_Level && (OpenPrice<OrderSL || OrderSL==0))
            {
             NewSL=ND(OpenPrice-BE_add*Point);
             if(NewSL<OrderSL || OrderSL==0)
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),ND(NewSL),OrderTakeProfit(),0,Yellow))
                 {error=GetLastError(); Print(Symbol()," Break Even error: ",ErrorToString(error));}
             continue;
            }
         }
       }
     }
  }
//+------------------------------------------------------------------+
void PartialClose(int MgcNmb, double _TP, double _Lot/*, string _Comm*/)
{
 int total=OrdersTotal();
 for (int i = total-1; i >= 0; i--)
   if(OrderSelect(i, SELECT_BY_POS))
     {
      int type=OrderType();
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MgcNmb && type<=OP_SELL && StringFind(OrderComment(),"#")<0)
         {
          if(ND((type==OP_BUY?1:-1)*(OrderClosePrice()-OrderOpenPrice()))>=ND(_TP*Point))
           {
            if (!OrderClose(OrderTicket(), _Lot, OrderClosePrice(), Slippage, clrWhite))
              {int error=GetLastError(); Print(Symbol()," Exit Trade error: ",ErrorToString(error));}
           }
         }
     }
 return;
}
//+------------------------------------------------------------------+
int TrailingStopRegular(int MagNum, double _TrailingStopPip, double _TrailingStopStep, double _TrailingStopTrigger)
  {
   if(_TrailingStopPip == 0) return (0);
   
   int total=OrdersTotal();
   
   for(int i=total-1;i>=0;i--)
     {
     if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
     int Type=OrderType();
     if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagNum && Type<=OP_SELL)
       {
        double CurStopLoss=OrderStopLoss();
        double OpenPrice=OrderOpenPrice();
        double NewStoploss = 0.0;
        if(Type==OP_BUY) //long
          {
           NewStoploss = Bid - _TrailingStopPip*Point;
           if( (CurStopLoss >= NewStoploss && CurStopLoss != 0.0) || Bid-OpenPrice < _TrailingStopTrigger*Point ) NewStoploss = 0.0;
          }
   
        if(Type==OP_SELL) //short
          {
           NewStoploss = Ask + _TrailingStopPip*Point;
           if( (CurStopLoss <= NewStoploss && CurStopLoss != 0.0) || OpenPrice-Ask < _TrailingStopTrigger*Point ) NewStoploss = 0.0;
          }
   
        if( NewStoploss != 0.0 ) 
          {
           if( MathAbs( CurStopLoss - NewStoploss ) <= Point*_TrailingStopStep ) continue;
           else NewStoploss = NormalizeDouble( NewStoploss, Digits);
           if(!OrderModify(OrderTicket(),OrderOpenPrice(), NewStoploss, OrderTakeProfit(),0/*,Yellow*/))
            {int error=GetLastError(); Print(Symbol()," Modify Order error: ",ErrorToString(error)); continue;}
          }
       }
     }
   return (0);
  }
//+------------------------------------------------------------------+
int TrailingStopFractal(int MagNum, int _TF, double _TrailingStopPip, double _TrailingStopTrigger)
  {
   if(_TrailingStopPip == 0) return (0);
   
   int total=OrdersTotal();
   
   for(int i=total-1;i>=0;i--)
     {
     if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
     int Type=OrderType();
     if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagNum && Type<=OP_SELL)
       {
        double CurStopLoss=OrderStopLoss();
        double OpenPrice=OrderOpenPrice();
        double NewStoploss = 0.0;
        if(Type==OP_BUY) //long
          {
           NewStoploss = ND(FractalStopLoss(_TF,OP_BUY) - _TrailingStopPip*Point);//Bid - _TrailingStopPip*Point;
           if( (CurStopLoss >= NewStoploss && CurStopLoss != 0.0) || Bid-OpenPrice < _TrailingStopTrigger*Point ) NewStoploss = 0.0;
          }
   
        if(Type==OP_SELL) //short
          {
           NewStoploss = ND(FractalStopLoss(_TF,OP_SELL) + _TrailingStopPip*Point);//Ask + _TrailingStopPip*Point;
           if( (CurStopLoss <= NewStoploss && CurStopLoss != 0.0) || OpenPrice-Ask < _TrailingStopTrigger*Point ) NewStoploss = 0.0;
          }
   
        if( NewStoploss != 0.0 ) 
          {
           if(!OrderModify(OrderTicket(),OrderOpenPrice(), NewStoploss, OrderTakeProfit(),0,clrRed))
            {int error=GetLastError(); Print(Symbol()," Modify Order error: ",ErrorToString(error)); continue;}
          }
       }
     }
   return (0);
  }
//+------------------------------------------------------------------+
double FractalStopLoss(int _tf, int _type)
 {
  int mode=0;
  switch(_type)
   {
    case OP_BUY:  mode=MODE_LOWER; break;
    case OP_SELL: mode=MODE_UPPER; break;
   }
  
  int i=3;
  while(i<iBars(NULL,_tf))
   {
    double fr=iFractals(NULL,_tf,mode,i);
    if(fr>0) return(fr);
    i++;
   }
  
  return(0.0);
 }
 
Can anyone help?
 
i will try 
 
rafat darwish:
i will try 

Thanks can you see what I have done wrong?

 
rafat darwish:
i will try 

Tried also adding this code, however can't seem to get it work, I'm not a programmer so might be something simple!

if(OrdersTotal())
   {
         if(!OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
         Print("unable to select order");
         double op =OrderOpenPrice();
         int type = OrderType();
         double lots = OrderLots();
         int ticket = OrderTicket();
         if(lots == LotSize)
         {
         if(type == OP_BUY && Bid-op > (PartialClosePips*pips))
            {
               if(!OrderClose(ticket,lots/2,Bid,30,clrGreen))
               Print("failure to close order");
            }
      else if(type == OP_SELL && op-Ask > (PartialClosePips*pips))
            if(OrderClose(ticket,lots/2,Ask,30,clrRed))
            Print("failure to close order");
         }
   
   }