HOW TO WRITE A TAKE PARTIAL PROFIT FUNCTION IN MQL4

 
  1. Im currently trying to write a function for taking partials but it seems not to be working.Aby help in correcting my mistakes will be appreciated amd better still,if you could come up with a better fun


void Takepartials()

  {

   bool status;

   int cnt;

   RefreshRates();


   if(OrdersTotal()>0)

     {

      for(cnt=OrdersTotal()-1; cnt>=0; cnt--)

        {

         OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

         if(OrderMagicNumber()==Magicnumber)

           {

            if(OrderType()==OP_BUY)

              {

               if(Bid-OrderOpenPrice()>=Takepartialspips1*Usepoint)

                 {

                  if(OrderLots()==Lotsize)

                    {

                     status= OrderClose(OrderTicket(),Partialslots,Bid,UseSlippage);

                     if(status==true)

                       {

                        Print("Partials Taken");

                       }

                     else

                       {

                        int err=GetLastError();

                        Print("Error is : ",err);

                       }


                     return;

                    }

                 }

              }

            if(OrderType()==OP_SELL)

              {

               if(OrderOpenPrice()-Ask>=Takepartialspips1*Usepoint)

                 {

                  if(OrderLots()==Lotsize)

                    {

                     status= OrderClose(OrderTicket(),Partialslots,Ask,UseSlippage);

                     if(status==true)

                       {

                        Print("Partials Taken");

                       }

                     else

                       {

                        int err=GetLastError();

                        Print("Error is : ",err);

                       }


                     return;

                    }

                 }

              }

           }

        }

     }

  }

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2.             if(OrderType()==OP_BUY){
                   ⋮
                         status= OrderClose(OrderTicket(),Partialslots,Bid,UseSlippage);
    You don't have to test the type and use Bid/Ask. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price.

  3. You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.