Improving Simple Expert Advisor (Mql4)

 

 Hi Guys, 

 Firstly I'm so so newbie about MQL 4 and MetaTrader and try to learn how can I develop algorithmic trading as well as I have developed this simple expert advisor at below;

 But When I have made test with strategy tester, I saw that opened two orders (buy & sell) at the same time. Otherwise I would like to open one order at once. How can I handle this  issue? 

 Thanks your response,  

Strategy Testing Result:  

 

Expert Advisor:  

#property copyright "Aytac Ozkan"

#property link      "aytacozkan.com"


//---- input parameters

extern double    Lots=0.1;

extern int       Slip=5;

extern double    TakeProfit=500;

extern double    StopLoss=50;

extern bool      Continuation=true;

extern bool      ReverseClose=true;

int MagicNumber1=2001,MagicNumber2=2002,i,closesell=0,closebuy=0;

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

//| expert start function                                            |

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

int start()

  {

//----

   int digits=MarketInfo("EURUSD",MODE_DIGITS);

   if(digits==5){int StopMultd=10;} else{StopMultd=1;}

   int Slippage=Slip*StopMultd;

   double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);

   double SL=NormalizeDouble(StopLoss*StopMultd,Digits);

   

   double slb = NormalizeDouble(Ask-SL * Point, Digits);

   double sls = NormalizeDouble(Bid+SL*Point, Digits);

   

   double tpb = NormalizeDouble(Ask +TP *Point , Digits); 

   double tps = NormalizeDouble(Bid -TP *Point , Digits);

   

   //Check open orders 

   if(OrdersTotal() >0 ){

   for(i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders

     {

      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available

        {

          if(OrderMagicNumber()==MagicNumber1) {int halt1=1;}

          if(OrderMagicNumber()==MagicNumber2) {int halt2=1;}


        }

     }

  }

    

    //Bar checks 

    if(iOpen(NULL,0,1)<iClose(NULL,0,1)) int BarOneUp=1;

    if(iOpen(NULL,0,1)>iClose(NULL,0,1)) int BarOneDown=1;

    if(iOpen(NULL,0,2)<iClose(NULL,0,2)) int BarTwoUp=1;

    if(iOpen(NULL,0,2)>iClose(NULL,0,2)) int BarTwoDown=1;

    if(iOpen(NULL,0,3)<iClose(NULL,0,3)) int BarThreeUp=1;

    if(iOpen(NULL,0,3)>iClose(NULL,0,3)) int BarThreeDown=1;

    if(iOpen(NULL,0,4)<iClose(NULL,0,4)) int BarFourUp=1;

    if(iOpen(NULL,0,4)>iClose(NULL,0,4)) int BarFourDown=1;

    

   if(TotalOpenOrders()==0 && IsNewBar()==true)

     {

   // Open buy by continuation

   if(BarOneUp==1&&BarTwoUp==1&&BarThreeUp==1&&BarFourUp==1&&halt1!=1&&Continuation==true){

      int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,slb,tpb,"Candle bug buy continuation order",MagicNumber1,0,Blue);

      if(ReverseClose==true)closesell=1;

   }


   // Open sell by continuation

   if(BarOneDown==1&&BarTwoDown==1&&BarThreeDown==1&&BarFourDown == 1&&halt2!=1&&Continuation==true){

      int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sls,tps,"Candle bug sell continuation order",MagicNumber2,0,Green);

      if(ReverseClose==true)closebuy=1;

   }

 }

     //Closing criteria 

     if(OrdersTotal() > 0 )

       {

         for(i=0;i<= OrdersTotal();i++)

           {

             if(OrderSelect(i -1 , SELECT_BY_POS) == true)

               {

                  if(OrderMagicNumber() == MagicNumber1 && closebuy== 1)

                    {

                        OrderClose(OrderTicket(), OrderLots(), Bid,Slippage,CLR_NONE);

                    }

                   if(OrderMagicNumber() == MagicNumber2 && closesell == 1)

                     {

                        OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, CLR_NONE);

                     }

               }

           }

       }

   if(openbuy < 1 || opensell < 1)

     {

         Sleep(1000*60*60*4);

     }

   return(0);

  }

// Check if there is a new bar

bool IsNewBar()

  {

   static datetime RegBarTime=0;

   datetime ThisBarTime=Time[0];


   if(ThisBarTime==RegBarTime)

     {

      return(false);

     }

   else

     {

      RegBarTime=ThisBarTime;

      return(true);

     }

  }

// Returns the number of total open orders for this Symbol and MagicNumber

int TotalOpenOrders()

  {

   int total_orders=0;


   for(int order=0; order<OrdersTotal(); order++)

     {

      if(OrderSelect(order,SELECT_BY_POS,MODE_TRADES)==false) break;


      if(OrderMagicNumber()==MagicNumber1 && OrderSymbol()==_Symbol)

        {

         total_orders++;

        }

     }

   return(total_orders);

  }

Tureng - at the same time - Türkçe İngilizce Sözlük
  • tureng.com
İngilizce Türkçe online sözlük Tureng. Kelime ve terimleri çevir ve farklı aksanlarda sesli dinleme. at the same time aynı zamanda do two jobs at the same time ne demek.
 

Rather use bool than int.

//Bar checks 

    bool BarOneUp=(iOpen(NULL,0,1)<iClose(NULL,0,1))?true:false;

    bool BarOneDown=(iOpen(NULL,0,1)>iClose(NULL,0,1))?true:false;

    bool BarTwoUp=(iOpen(NULL,0,2)<iClose(NULL,0,2))?true:false;

    bool BarTwoDown=(iOpen(NULL,0,2)>iClose(NULL,0,2))?true:false;

    bool BarThreeUp=(iOpen(NULL,0,3)<iClose(NULL,0,3))?true:false;

    bool BarThreeDown=(iOpen(NULL,0,3)>iClose(NULL,0,3))?true:false;

    bool BarFourUp=(iOpen(NULL,0,4)<iClose(NULL,0,4))?true:false;

    bool BarFourDown=(iOpen(NULL,0,4)>iClose(NULL,0,4))?true:false;

    

   if(TotalOpenOrders()==0 && IsNewBar())

     {

   // Open buy by continuation

   if(BarOneUp && BarTwoUp && BarThreeUp && BarFourUp && halt1!=1 && Continuation){

      int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,slb,tpb,"Candle bug buy continuation order",MagicNumber1,0,Blue);

      if(ReverseClose) closesell=1;

   }




   // Open sell by continuation

   else if(BarOneDown && BarTwoDown && BarThreeDown && BarFourDown && halt2!=1 && Continuation){

      int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sls,tps,"Candle bug sell continuation order",MagicNumber2,0,Green);

      if(ReverseClose) closebuy=1;

   }

 }

It's only checking for open buy orders.

if((OrderMagicNumber()==MagicNumber1||OrderMagicNumber()==MagicNumber2) && OrderSymbol()==_Symbol)
 
Thank you so much Sir for your helpful answer and your time, your improvement works quite well. 
Reason: