EA doesnt close positions properly

 

Hello,

 

I have problem with my EA which Im trying to create.

Look like that this ea's open position well but unfortunately it closes position in way which I cant understand.

I would like to close long position when +DI is below the ADX and I want to close short when -DI is smaller than ADX.

Below is my code for that EA. Can anybody tell me whats wrong with it and how can i improve it to work fine?

 



//---- input parameters

extern double    Lots=0.1;
extern int ADX = 14;
extern int NumeroMagico = 100;
extern int Period_MA1 = 5;
extern int Period_MA2 = 144;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   double  p=Point;
   int     OrdersPerSymbol=0;
   int     cnt=0;
   double tp = Ask + 50*Point;
   double sl = Bid - 50*Point;
   
   if(Bars<150)                               {Print("-----NO BARS "); return(0);}
   OrdersPerSymbol=0;
   
   
     for(cnt=OrdersTotal();cnt>=0;cnt--)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if( OrderSymbol()==Symbol() )
        {
         OrdersPerSymbol++;
        }
     }
     
      
     
     for(cnt=OrdersTotal();cnt>=0;cnt--)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if( OrderSymbol()==Symbol() )
        {
         if(OrderType()==OP_BUY)
           {
            
            if( exit() == 3)
              {
               OrderClose(OrderTicket(),Lots,Bid,0,White);
               
              }
           } // if BUY


         if(OrderType()==OP_SELL)
           {
            // did we make our desired SELL profit?
            if ( exit() == 4 )
              {
               OrderClose(OrderTicket(),Lots,Ask,0,Red);
               //Fechado = 1;
              }
           } //if SELL
           
        } // if(OrderSymbol)
        
     } // for
   //Abre as ordens apenas se năo possuir nenhuma ordem aberta por Simbolo
   if(OrdersPerSymbol<1)
     {
      if(Sinal() == 1)
                  {
         OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"Buy  "+CurTime(),NumeroMagico,0,White);
         return(0);
        }
        
      if(Sinal() == 2)
        {
         OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"Sell "+CurTime(),NumeroMagico,0,Red);
         return(0);
        }
     }



 
//----
   return(0);
  }
  
  //+------------------------------------------------------------------+
//| Gerador de sinais                                          |
//+------------------------------------------------------------------+  
int Sinal()
{
 //set ADX Trend
    int Direcao = 0; // 1 indica compra , 2 indica venda
   double MA_1, MA_2;
   double MACD0, MACD1, MACD2, MACDsignal0;
   double rsi0, rsi1; 
   double adx0, adx1, pDI,mDI;

     
   MA_1 = iMA(NULL,0,Period_MA1,0,MODE_EMA,PRICE_CLOSE,0); 
   MA_2 = iMA(NULL,0,Period_MA2,0,MODE_EMA,PRICE_CLOSE,0);
   
   MACD0 = iMACD(NULL, 0, 12, 26, 9,PRICE_CLOSE, MODE_MAIN,0);
   MACD1 = iMACD(NULL, 0, 12, 26, 9,PRICE_CLOSE, MODE_MAIN,1);
   MACD2 = iMACD(NULL, 0, 12, 26, 9,PRICE_CLOSE, MODE_MAIN,2);
   MACDsignal0 = iMACD(NULL, 0, 12, 26, 9,PRICE_CLOSE, MODE_SIGNAL,0);
   
   rsi0 = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);
   rsi1 = iRSI(NULL, 0, 14, PRICE_CLOSE, 1);
   
   adx0 = iADX(Symbol(),0,ADX,PRICE_LOW,MODE_MAIN,0);
   adx1 = iADX(Symbol(),0,ADX,PRICE_LOW,MODE_MAIN,1);
   pDI  = iADX(Symbol(),0,ADX,PRICE_LOW,MODE_PLUSDI,0);
   mDI  = iADX(Symbol(),0,ADX,PRICE_LOW,MODE_MINUSDI,0);
   
   

   
   

 
   
   if ( MA_1>MA_2 && MA_1-MA_2>=100*Point && MACD0>MACD1>MACD2 && MACD0>0 && MACD0>MACDsignal0 && rsi0>70 && rsi0>=rsi1 && adx0>adx1 && pDI>mDI && pDI>adx0 )
   {
      Direcao = 1; //buy
   }
   else if ( MA_1<MA_2 && MA_2-MA_1>=100*Point && MACD0<MACD1<MACD2 && MACD0<0 && MACD0<MACDsignal0 && rsi0<30 && rsi0<=rsi1 && adx0>adx1 && pDI<mDI && mDI>adx0 ) 
   {
      Direcao = 2; //sell
   }
   //end ADX
   


   return (Direcao);

}

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

int exit()
{
int Direcao = 0;
double MACD0 =0 , MACDsignal0 =0;
double adx0=0,adx1=0,adx2=0, pDI=0, mDI=0; 

   MACD0 = iMACD(NULL, 0, 12, 26, 9,PRICE_CLOSE, MODE_MAIN,1);
   MACDsignal0 = iMACD(NULL, 0, 12, 26, 9,PRICE_CLOSE, MODE_SIGNAL,1);
   adx0 = iADX(NULL, 0, 14, PRICE_CLOSE,MODE_MAIN, 0);
   adx1 = iADX(NULL, 0, 14, PRICE_CLOSE,MODE_MAIN, 1);
   adx2 = iADX(NULL, 0, 14, PRICE_CLOSE,MODE_MAIN, 2);
   pDI  = iADX(Symbol(),0,ADX,PRICE_LOW,MODE_PLUSDI,0);
   mDI  = iADX(Symbol(),0,ADX,PRICE_LOW,MODE_MINUSDI,0);
   


if(pDI < adx0 )
{
Direcao = 3; //buy
}

if(mDI < adx0  )
{
Direcao = 4; 
}


return (Direcao);

}

 I also put screenshot from tester where this situation is visable.

 

Best Regards 

 
 
abx89: unfortunately it closes position in way which I cant understand.
  1. Print your variable values in exit, and find out why.
  2.    adx0 = iADX(NULL, 0, 14, PRICE_CLOSE,MODE_MAIN, 0);
       pDI  = iADX(Symbol(),0,ADX,PRICE_LOW,MODE_PLUSDI,0);
    If you "want to close when +Di < ADX" then you should look at those values. Either your indicator is looking at the Close price or the Low price; it doesn't look at both. Your code does.
 

Thank you for answer. I didnt notice that I check close and low so i have changed it but there is still the same problem.

 

 

exit() code:

 

int exit()
{
int Direcao = 0;
double MACD0 =0 , MACDsignal0 =0;
double adx0,adx1,adx2, pDI, mDI; 

   MACD0 = iMACD(NULL, 0, 12, 26, 9,PRICE_CLOSE, MODE_MAIN,1);
   MACDsignal0 = iMACD(NULL, 0, 12, 26, 9,PRICE_CLOSE, MODE_SIGNAL,1);
   adx0 = iADX(NULL, 0, 14, PRICE_CLOSE,MODE_MAIN, 0);
   adx1 = iADX(NULL, 0, 14, PRICE_CLOSE,MODE_MAIN, 1);
   adx2 = iADX(NULL, 0, 14, PRICE_CLOSE,MODE_MAIN, 2);
   pDI  = iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,0);
   mDI  = iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,0);
   


if(pDI <= adx0 )
{
Print("+DI[0] = ", pDI, "ADX[0] = ", adx0);
Direcao = 3; //buy
}

if(mDI <= adx0  )
{
Print("-DI[0] = ", mDI, "ADX[0] = ", adx0);
Direcao = 4; 
}


return (Direcao);

}
Reason: