Need help regarding OrderClose and OrderModify

 

Hello guys, 

I am new here and have just recently started learning how to code mql4 few days ago. I am trying to write a code so that the EA will automatically close half of my position and modify my order so that it will create a new TP and SL.

However, whenever I run the code, it seems like it partial closes but does not modify my order so that my new tp point is created

I would really appreciate all the helps. (Please be nice, as my code seems very ignorant)

Here is the code:

for(int b = OrdersTotal()-1; b>=0; b--)

   {

      if(OrderSelect(b, SELECT_BY_POS, MODE_TRADES))

      if(OrderSymbol() == Symbol())

      if(OrderType() == OP_BUY)

      {   

         if(Bid == OrderTakeProfit()-1*Point()) 

            {

               bool CloseHalfLongPosition;

               CloseHalfLongPosition = OrderClose(OrderTicket(), (Lots/2), Bid, Slippage);

               if(CloseHalfLongPosition == false)

               {

                  Alert("Unable to Close Half of the Long Position");

               }

               else

               {

                  Alert("Closing Half of the Long Position");

                  bool ModifyHalfLongPosition;

                  ModifyHalfLongPosition = OrderModify(OrderTicket(), 0, (OrderOpenPrice()-2*Point()), Bid + ((OrderTakeProfit()-OrderOpenPrice())*2), 0);

                  if(ModifyHalfLongPosition == false)

                  {

                     Alert("Unable to Modify the Half of the Long Position");

                  }

                  else

                  {

                     Alert("Changing Long's TP & SL");

                  }

               }          

            }

      }

   }
 


In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
 
Keith Watford:


In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
Oh, i did not know. Sorry. Thank you so much too!
 
  1. hyunjeshin: it partial closes but does not modify my order so that my new tp point is created

    When you partial close, the remaining order gets a new OrderTicket. You are trying to modify the closed order. Modify first, so you can tell that you've already done the partial close, then close.

  2. CloseHalfLongPosition = OrderClose(OrderTicket(), (Lots/2), Bid, Slippage);
    
    1. 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. See my code.
    2. You also must check if you have already done it, to avoid repeated closing. Alternatives:
      • Move SL to Break Even+1 before the partial close. That way you know that you already did it.
      • Set a flag in persistent storage (files, global variables w/flush)
      • Open two orders initially, and close one (manually or by TP.)

  3. for(int b = OrdersTotal()-1; b>=0; b--){
          if(OrderSelect(b, SELECT_BY_POS, MODE_TRADES))
          if(OrderSymbol() == Symbol())

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles

  4. if(Bid == OrderTakeProfit()-1*Point()) 

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

 
William Roeder:
  1. When you partial close, the remaining order gets a new OrderTicket. You are trying to modify the closed order. Modify first, so you can tell that you've already done the partial close, then close.

    1. 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. See my code.
    2. You also must check if you have already done it, to avoid repeated closing. Alternatives:
      • Move SL to Break Even+1 before the partial close. That way you know that you already did it.
      • Set a flag in persistent storage (files, global variables w/flush)
      • Open two orders initially, and close one (manually or by TP.)

  2. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles

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

I believe I understand every point except the 3rd number.

I do not really know what or how magic number works, thus i have just searched codes online to incoporate in order to limit open orders to only 1 order at a time. Would it be possible for you to explain abit more about this magic number thing?
 
hyunjeshin:

Do not double post!

I have deleted your other topic!

 
Keith Watford:

Do not double post!

I have deleted your other topic!

Oh... It was not double post, as second question was about error 130 on Short Positions... Not long positions....

Should I then post the code here?
 
hyunjeshin:
Oh... It was not double post, as second question was about error 130 on Short Positions... Not long positions....

Should I then post the code here?

Once you have the correct code for working with long positions, it is a simple step to work with short positions. There is no need for a new topic.

 
Keith Watford:

Once you have the correct code for working with long positions, it is a simple step to work with short positions. There is no need for a new topic.

Yes, I agree, however, for short position it has error 130, while the long position does not.

It is very likely to be error in my part, but since i am not able to spot the problem, I opened another post to ask fro help, as i believed that it was whole another issue.

I apologize for the confusion and would it be possible for you to take a look and help?

 
Show the updated code for the long position and the short.
 
//1/2 TP for Long
   
   for(int b = OrdersTotal()-1; b>=0; b--)
   {
      if(OrderSelect(b, SELECT_BY_POS, MODE_TRADES))  
      if(OrderSymbol() == Symbol())                   
      if(OrderType() == OP_BUY)                       
      {   
         if(Bid == OrderTakeProfit()-1*Point()) 
            {
               bool CloseHalfLongPosition;
               CloseHalfLongPosition = OrderClose(OrderTicket(), (NormalizeDouble(((OrderLots())/2),2)), Bid, Slippage);
               if(CloseHalfLongPosition == true)
               {
                  Alert("Closing Half of the Long Position");
                  bool SelectLongPositionToModify;
                  SelectLongPositionToModify = OrderSelect(b, SELECT_BY_POS, MODE_TRADES);
                  if(SelectLongPositionToModify == true)
                  {
                     bool ModifyHalfLongPosition;
                     ModifyHalfLongPosition = OrderModify(OrderTicket(), 0, (OrderOpenPrice()-1*Point()), Bid + (OrderTakeProfit()- OrderOpenPrice()), 0);
                     if(ModifyHalfLongPosition == true)
                     {
                        Alert("Changing Long's TP & SL");   
                     }
                     else
                     {
                        Alert("Unable to Modify the Half of the Long Position");
                     } 
                  }
                  else
                  {
                     Alert("Unable to Select Long Position to Modify");
                  }
                  
               }
               else
               {
                  Alert("Unable to Close Half of the Long Position");   
               }
            }
      }
//1/2 TP for Short
         if(OrderSelect(b, SELECT_BY_POS, MODE_TRADES))
         if(OrderSymbol() == Symbol())
         if(OrderType() == OP_SELL)
         {   
            if(Ask == OrderTakeProfit()+1*Point()) 
               {
                  bool CloseHalfShortPosition;
                  CloseHalfShortPosition = OrderClose(OrderTicket(), (NormalizeDouble(((OrderLots())/2),2)), Ask, Slippage);
                  if(CloseHalfShortPosition == true)
                  {
                     Alert("Closing Half of the Short Position");
                     bool SelectShortPositionToModify;
                     SelectShortPositionToModify = OrderSelect(b, SELECT_BY_POS, MODE_TRADES);
                     if(SelectShortPositionToModify == true)
                     {
                        bool ModifyHalfShortPosition;
                        ModifyHalfShortPosition = OrderModify(OrderTicket(), 0, (OrderOpenPrice()+1*Point()), Ask + (OrderOpenPrice() - OrderTakeProfit()), 0);
                        if(ModifyHalfShortPosition == true)
                        {
                           Alert("Changing Short's TP & SL");   
                        }
                        else
                        {
                           Alert("Unable to Modify the Half of the Short Position");
                        } 
                     }
                     else
                     {
                        Alert("Unable to Select Short Position to Modify");
                     }
                     
                  }
                  else
                  {
                     Alert("Unable to Close Half of the Short Position");   
                  }
               }
         }   
   
Keith Watford:
Show the updated code for the long position and the short.
Its this. For Long Position it works, but it does not work for Short Position. Please Help.
Reason: