COund not Close the trade using IF else with certain conditions

 

hi to all,


Good day and God bless to everyone. i am a newbie trying to program my own algo using MQL4.

i have done creating my EA and it trades perfectly but i could not seem to let it exit upon trigger of indicator condition( the reverse direction of my trigger condition for buy and sell)


please see codes below and if anyone would be so generous to help out in correcting whats i did please do so.


really appreciate your help .


void PanchoTrailingTradewithindiexittrigger()

{

               

      static datetime candletime;

      if(Time[0]== candletime)

      return; 

    

      double kijunsen_period = 26;

      

      int magicNB1 = 1111;

      int magicNB2 = 2222;

      double riskPerTrade = 0.02;

      int openOrderID1;

      int openOrderID2;

      int Maximumorders = 1;

      int lotsDecimals = 2;

      

      // Average true Range

      double ATR = NormalizeDouble(iATR(NULL,PERIOD_CURRENT,14,1)/ GetPipValue(),0);

   

      double Stoplossinpips = NormalizeDouble((1.5* ATR),Digits);

      double lotSize = OptimalLotSize(riskPerTrade,Stoplossinpips);

      

      double halfLots = NormalizeDouble(lotSize/2, Digits);

      

   

      // Baseline - ichimoku kijun sen

      double kijunsen = iIchimoku(NULL,PERIOD_CURRENT,9,kijunsen_period,52,MODE_KIJUNSEN,1);


      // C2 - William Percentage Range

      double WPRvalue = iWPR(NULL,PERIOD_CURRENT,5,1);

      double WPRbelowlevel = -80;

      double WPRabovelevel = -20;


      

      // Volume indicator - CMF 

      double CMF= iCustom(NULL,0,"CMF_v1",7,0,0);

      

    if(OrdersCounter()<Maximumorders)

     {

      // if(!CheckIfOpenOrdersByMagicNB(magicNB));

         //  {

        if (Ask > kijunsen && WPRvalue >= WPRabovelevel && CMF > 0 ) 

            {          

            double Stoplossinpips = NormalizeDouble((1.5* ATR),Digits);

            double Stoplossprice_buy = Ask-(ATR * 1.5*GetPipValue());

            double takeprofitprice_buy =(ATR)* GetPipValue() + Ask;

            

            double Stoplossprice_sell =(Bid+(ATR * 1.5*GetPipValue()) );

            double takeprofitprice_sell = MathAbs(Bid- (ATR)* GetPipValue());

            double Stoplevel = MarketInfo(NULL,MODE_STOPLEVEL);  

    

            Alert("");

            Alert( "Ready to Buy");

            Alert( "Entry Price : " + Ask );

            Alert( "Stoplossprice_buy : " + Stoplossprice_buy );

            Alert( "takeprofitprice_buy : " + takeprofitprice_buy );

            Alert( "Stoplevel : " + Stoplevel );

            

         

      

            double halfLots = NormalizeDouble(lotSize/2, Digits);

      

            double lotSize = OptimalLotSize(riskPerTrade,Stoplossinpips);

            Alert("lotSize: " + lotSize);

            

            

             openOrderID1 = OrderSend(Symbol(), OP_BUY,lotSize,Ask,0,Stoplossprice_buy,takeprofitprice_buy,NULL,0,0,clrNONE);

            if(openOrderID1>0)

               {

               if(OrderSelect(openOrderID1, SELECT_BY_TICKET, MODE_TRADES))

                Alert("BUY order with TP opened : ",OrderOpenPrice());

               }

               else

               Alert("Error opening BUY order with TP: ",GetLastError());

               

       

               if( WPRvalue <= WPRbelowlevel )

                           {

                            OrderClose(openOrderID1,OrderLots(),Ask,3,clrNONE);

                           } 

             

      

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

               {

               if (OrderSelect(b,SELECT_BY_POS, MODE_TRADES))


                  if(OrderSymbol()==Symbol())


               if(OrderType()== OP_BUY)

                { 

                   

                

                if(takeprofitprice_buy== OrderOpenPrice())

                    {

                     OrderModify(openOrderID1,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE)  ;                 

                return;

                           }   

                           

                          }


                       }  

                             

                   }

     

            

 else if (Bid < kijunsen && WPRvalue <= WPRbelowlevel && CMF < 0 )

            {

            

            double Stoplossinpips = NormalizeDouble((1.5* ATR),Digits);

            double Stoplossprice_buy =Ask-(ATR * 1.5*GetPipValue());

            double takeprofitprice_buy = ATR* GetPipValue() + Ask;

            

            double Stoplossprice_sell =Bid+(ATR * 1.5*GetPipValue());

            double takeprofitprice_sell = MathAbs(Bid- (ATR* GetPipValue()));

            double Stoplevel = MarketInfo(NULL,MODE_STOPLEVEL);    

    

            Alert("");  

          

            Alert("");

            Alert( "Ready to Sell");

            Alert( "Entry Price : " + Bid );

            

            Alert( "Stoplossprice_sell : " + Stoplossprice_sell );   

            Alert( "takeprofitprice_sell : " + takeprofitprice_sell );

            Alert( "Stoplevel : " + Stoplevel );

            

            double halfLots = NormalizeDouble(lotSize/2, Digits);

      

            double lotSize = OptimalLotSize(riskPerTrade,Stoplossinpips);

            Alert("lotSize: " + lotSize);

            

         

            openOrderID1 = OrderSend(Symbol(), OP_SELL,lotSize, Bid, 0, Stoplossprice_sell, takeprofitprice_sell, NULL, 0, 0, clrNONE);

            if(openOrderID1>0)

             {

               if(OrderSelect(openOrderID1, SELECT_BY_TICKET, MODE_TRADES))

                Alert("SELL order with TP opened : ",OrderOpenPrice());

                }

               else

                Alert("Error opening SELL order with TP: ",GetLastError());

                

                  if( WPRvalue >= WPRbelowlevel )

                              {

                               OrderClose(openOrderID1,OrderLots(),Bid,3,clrNONE);

                              }

                           

             

             

                int b;

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

               {

               if(OrderSelect(b,SELECT_BY_POS, MODE_TRADES))


                 

                  if(OrderSymbol()==Symbol())


               if(OrderType()== OP_SELL)

                   {  

                  

                   if(takeprofitprice_sell== OrderOpenPrice())

                        {

                     OrderModify(openOrderID1,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE) ;  

                     return;           

                        }

                           

                           

                         }

                       }

                     }  

                   }


           candletime = Time[0];  

   }

          

Reason: