Improving Simple Expert Advisor (Mql4)New comment

 

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:  

 

#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);

  }

 

Hi


There are various ways to limit your buy and sell orders

Like running a seperate function to total up all the buys & sells


Another way is in your code declare a global variable called 

int count_Buys=0;

int count_Sells=0;

...

Then when you send a new order and it has been sent succesfully you can add this to these variables

if (openbuy > 0) {

 count_Buys++;

}

and remember to subtract when you send the OrderClose function.


So effectively you can have add checks to your code like:

if (count_Buys > 0) {  //Dont open new orders } or

if ( (count_Buys+count_Sells) > 0) { //Dont open new orders }


Hope this helps.

Reason: