Code EA to close half lot of position!

 

My intention is to make an EA Mql 4,  which in my example at least closes half of the position at 4 pips and let the rest of the initial lot run, this is what I did,

it works well but sometimes not...

For example, sometimes the half of position is closed  , a second after the position is opened! .

I need you to help me understand what is wrong, because I have no idea what the problem could be.

Thank you so much!


void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   check_TP_SL();
  
  }
//+------------------------------------------------------------------+


void check_TP_SL()
{
     double Lote=0.5;
  

   int total=OrdersTotal();
   double T1 = OrderTicket();
   double CS=(OrderOpenPrice()- _Point*10*4);
   double CB=(OrderOpenPrice()+ _Point*10*4);
   
 
   for(int i=total;i>0;i--)
      { 
       if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES))
         
         {
           if(OrderType()==OP_SELL )

             {
               if(Ask <= CS)
                {
                 if(OrderLots()== Lote)
             
                   {
                     bool res1=OrderClose(OrderTicket(),OrderLots()/2,Ask,3,clrYellow);
                   }
                }
               }
              
        if(OrderType()==OP_BUY )
        
           {
            if(Bid >= CB)
             {
              if(OrderLots()== Lote)
               
              {
               bool res2=OrderClose(OrderTicket(),OrderLots()/2,Bid,3,clrBeige); 
              }
            }
          }
        }
      } 
   return;
 }   


 

Check the function PositionClosePartial() in ...\MQL5\Include\Trade\Trade.mqh

on your local PC. It shows how you can close a part of a position.

 

Thanks , Carl Schreiber 

I forgot comment that is a Mql4 script !

Thank you very much !

 

Google for: site:mql5.com partial close mq4

and you'll find several examples....

 
Sanbach21:

My intention is to make an EA Mql 4,  which in my example at least closes half of the position  it works well but sometimes not...

For example, sometimes the half of position is closed  , a second after the position is opened! .

I need you to help me understand what is wrong, because I have no idea what the problem could be.

  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. You state a "second position is opened" but didn't supply the code for that, we can't help you there.

  3.  bool res1=OrderClose(OrderTicket(),OrderLots()/2,Ask,3,clrYellow);

    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.

    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.)

  4.    double CS=(OrderOpenPrice()- _Point*10*4);
       double CB=(OrderOpenPrice()+ _Point*10*4);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

 
Carl Schreiber #:

Google for: site:mql5.com partial close mq4

and you'll find several examples....

Thanks again Carl ...

 
Carl Schreiber #:

Google for: site:mql5.com partial close mq4

and you'll find several examples....

Thanks again !!!

 
William Roeder #:
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. You state a "second position is opened" but didn't supply the code for that, we can't help you there.

  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.

    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.)

  4. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

Thank you William, many errors , i will check it !

Reason: