Help! Need to take half profit and move stoploss to breakeven

 

Please help me with this

Hope someone can give me a clear and helpful answer


Here is the code I wrote to check if the price is between open price and take profit, then sell half of the position and move the stoploss to breakeven. It works good, when reaching to the half profit point, it will close half of the position and move the stoploss to the open price of the original position (breakeven). My question is that how can I do this only once for each trade. How to make this code happen just once to any single trade. Now it does the same thing over and over on a trade, for example, first time 500 profit (half of 1000 take profit), immediately 250 profit, then 125 profit, then 62.5 profit, etc. until it's close to zero. I want to take the profit when we're half way through, and let the rest half of the profit run for much more profits.

 for(int i=0; i<OrdersTotal(); i++) // For market and pending orders
     {
      if((OrderSelect(i,SELECT_BY_POS)==true)     //If there is the next one
         && (OrderSymbol()==Symbol()))               //.. and our currency pair
        {

            if(OrderType() == 0)                               // if it's a buy

               if(Bid == (OrderTakeProfit() + OrderOpenPrice())/2)            // if the price is half-way to take profit
                 {
                  double MN1 = OrderMagicNumber();
                  double OP1 = OrderOpenPrice();
                  OrderClose(OrderTicket(), OrderLots()/2, Bid, 2, clrDarkOrange);                                         // close half of the position
                  if((OrderSelect(i,SELECT_BY_POS)==true)                                                  
                     && (OrderSymbol()==Symbol()) && OrderMagicNumber() == MN1)                                            // find the new position (after partially closing the original position)
                     OrderModify(OrderTicket(), OrderOpenPrice(), OP1, OrderTakeProfit(), OrderExpiration(), clrNONE);     // move the stoploss to breakeven
                 }

            if(OrderType()== 1)                                                               // if it's a sell
               if(Ask == (OrderTakeProfit() + OrderOpenPrice())/2)            // if the price is half-way to take profit
                 {
                  double MN2 = OrderMagicNumber();
                  double OP2 = OrderOpenPrice();
                  OrderClose(OrderTicket(), OrderLots()/2, Ask, 2, clrDarkOrange);                                       // close half of the position
                  if((OrderSelect(i,SELECT_BY_POS)==true)                                                  
                     && (OrderSymbol()==Symbol()) && OrderMagicNumber() == MN2)                                          // find the new position (after partially closing the original position)
                     OrderModify(OrderTicket(), OrderOpenPrice(), OP2, OrderTakeProfit(), OrderExpiration(), clrNONE);   // move the stoploss to breakeven
                 }
           }
       
     }
forex news - Trading blogs and financial markets analysis
forex news - Trading blogs and financial markets analysis
  • www.mql5.com
Forex – foreign exchange market, or currency market – is the market where one currency is traded for another. Open 24-hours a day from Sunday evening through to Friday night, it is the world's most
 
Please edit your post and
use the code button (Alt+S) when pasting code
 
  1. John2010: if the price is between open price and take profit, then sell half of the position and move the stoploss to breakeven. I

    The problem is to remember that you have already sold half. Move to breakeven first and then close.

  2. OrderClose(OrderTicket(), OrderLots()/2, 

    Will not work. The close amount and the remaining amount must both be between min and max and be a multiple of lot step.

  3.  if(Bid == (OrderTakeProfit() + OrderOpenPrice())/2)

    Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 2013.06.07

 
if(OrderType() == 0)                               // if it's a buy
   if((Bid > ((OrderTakeProfit() + OrderOpenPrice())/2)) && (OrderOpenPrice()!= OrderStopLoss())&& (OrderTakeProfit()!= 0))      // if the price is half-way to take profi
   {
      double MN1 = OrderMagicNumber();
      double OP1 = OrderOpenPrice();
      OrderClose(OrderTicket(), OrderLots()/2, Bid, 2, clrDarkOrange);                                         // close half of the position
      if((OrderSelect(i, SELECT_BY_POS)==true) && (OrderSymbol()==Symbol()) )
         OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration(), clrNONE);     // move the stoploss to breakeven
   }
if(OrderType()== 1)                                                               // if it's a sell
   if((Ask < ((OrderTakeProfit() + OrderOpenPrice())/2)) && (OrderOpenPrice()!= OrderStopLoss()) && (OrderTakeProfit()!= 0))       // if the price is half-way to take profit
   {
      double MN2 = OrderMagicNumber();
      double OP2 = OrderOpenPrice();
      OrderClose(OrderTicket(), OrderLots()/2, Ask, 2, clrDarkOrange);                                       // close half of the position
      if((OrderSelect(i, SELECT_BY_POS)==true) && (OrderSymbol()==Symbol()))
         OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), OrderExpiration(), clrNONE);   // move the stoploss to breakeven
   }
 
SunSoil Crypto #:
Not working … if close half of the order and then try to select, the remaining order will have different index .
Also, OrderLots()/2 is wrong … order lots should be normalized to lotstep and checked to be between LotMax and LotMin
Also, as said above, doubles are almost never equal