MACD trade closing not happening at the right spots

 

Need to code a simple MACD Cross over and U turn of MACD.......however EA is not picking the right spots for closing the trades.

Sell trade opens, when MACD cross below Signal line while MACD is above zero line

Close must happen when MACD Cross zero line from below, or when MACD turns down signal line when below zero


while sell entries open correctly the closing is happening at the wrong spots


pls suggest the solution


double MACDC   = iMACD(NULL, 0, 34, 89, 9, PRICE_CLOSE, MODE_MAIN,0);

double MACDP1  = iMACD(NULL, 0, 34, 89, 9, PRICE_CLOSE, MODE_MAIN,1);

double MACDP2  = iMACD(NULL, 0, 34, 89, 9, PRICE_CLOSE, MODE_MAIN,2);

double MACDS   = iMACD(NULL, 0, 34, 89, 9, PRICE_CLOSE, MODE_SIGNAL,0);

double MACDSP1 = iMACD(NULL, 0, 34, 89, 9, PRICE_CLOSE, MODE_SIGNAL,1);

double MACDSP2 = iMACD(NULL, 0, 34, 89, 9, PRICE_CLOSE, MODE_SIGNAL,2);


//----------------------------+

//Close Short Position        /

//----------------------------+                

if (OrdersTotal() > 0)

{


         bool close_sell_condition_1 =  MACDC > 0;

         bool close_sell_condition_2 =  MACDP1 <= 0;

         bool close_sell_condition_3 =  MACDP2 < 0;

         

         bool close_sell_condition_4 =  MACDC < 0;

         bool close_sell_condition_5 =  MACDC < MACDS;

         bool close_sell_condition_6 =  MACDP1 > MACDSP1;


 

if((close_sell_condition_1  &&  close_sell_condition_2  &&  close_sell_condition_3)|| (close_sell_condition_4  &&  close_sell_condition_5  &&  close_sell_condition_6))

{

OrderSelect(ShortTicket,SELECT_BY_TICKET);

if(OrderCloseTime() == 0 && ShortTicket > 0)

{

Closed = OrderClose(ShortTicket,OrderLots(),Ask,0,Red);


ShortTicket = 0;

}

         }


         

}

Files:
LONGt_macd.png  59 kb
short_macd.png  51 kb
 
  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. double MACDC   = iMACD(NULL, 0, 34, 89, 9, PRICE_CLOSE, MODE_MAIN,0);
    double MACDP1  = iMACD(NULL, 0, 34, 89, 9, PRICE_CLOSE, MODE_MAIN,1);
    Where is this code? Global or in OnTick?
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem
  3. ckm Forex: Close must happen when MACD Cross zero line from below, or when MACD turns down signal line when below zero

    Your if((close… is not testing that.

  4. bool close_sell_condition_1 =  MACDC > 0;
    bool close_sell_condition_2 =  MACDP1 <= 0;
    Use meaningful variable names. 1,2,3 are not.
Reason: