Close Buy AND Open Sell Before New Tick

 

Hi Guys, 

 

First time poster, hope you might be able to help me with this one...

 

I have been working on a simple EA that will perform trading actions based on an indicator crossing point. My starting point was the first simple EA example in the MQL4 online book, found here https://book.mql4.com/samples/expert.

 Essentially I have noticed that when my indicator crossover happens, as per the screenshot below, my 'Close' action completes at the end of that tick (in this case between 9:30 and 10:00). But I must wait for a new tick to open the opposite action, (in this case open sell. at 10:30). I would like to be able to close buy and open sell at 10:00. Should this be possible? I've been wondering whether my 'start' iteration might take too long... 

I hope that I've used appropriate language to describe my problem! I am coming from back-testing my thesis in Matlab,  now looking at what might happen in the real world. 

Any help would be greatly appreciated, code is below (Copied from the book as a learning exercise). Thanks!


//--------------------------------------------------------------- 5 --
   // Trading criteria
   MA_1_t=iMA(NULL,0,Period_MA_1,0,MODE_LWMA,PRICE_TYPICAL,0); // ??_1
   MA_2_t=iMA(NULL,0,Period_MA_2,0,MODE_LWMA,PRICE_TYPICAL,0); // ??_2
 
   if (MA_1_t > MA_2_t) //+ Rastvor*Point)         // If difference between
     {                                          // ..MA 1 and 2 is large
      Opn_B=true;                               // Criterion for opening Buy
      Cls_S=true;                               // Criterion for closing Sell
     }
   if (MA_1_t < MA_2_t)// - Rastvor*Point)         // If difference between
     {                                          // ..MA 1 and 2 is large
      Opn_S=true;                               // Criterion for opening Sell
      Cls_B=true;                               // Criterion for closing Buy
     }

//--------------------------------------------------------------- 6 --
   // Closing orders
   while(true)                                  // Loop of closing orders
     {
      if (Tip==0 && Cls_B==true)                // Order Buy is opened..
        {                                       // and there is criterion to close
         Alert("Attempt to close Buy ",Ticket,". Waiting for response..");
         RefreshRates();                        // Refresh rates
         Ans=OrderClose(Ticket,Lot,Bid,2);      // Closing Buy
         if (Ans==true)                         // Success :)
           {
            Alert ("Closed order Buy ",Ticket);
            break;                              // Exit closing loop
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
            continue;                           // Retrying
         return;                                // Exit start()
        }
 
      if (Tip==1 && Cls_S==true)                // Order Sell is opened..
        {                                       // and there is criterion to close
         Alert("Attempt to close Sell ",Ticket,". Waiting for response..");
         RefreshRates();                        // Refresh rates
         Ans=OrderClose(Ticket,Lot,Ask,2);      // Closing Sell
         if (Ans==true)                         // Success :)
           {
            Alert ("Closed order Sell ",Ticket);
            break;                              // Exit closing loop
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
            continue;                           // Retrying
         return;                                // Exit start()
        }
      break;                                    // Exit while
     }
//--------------------------------------------------------------- 7 --
   // Order value
   RefreshRates();                              // Refresh rates
   Min_Lot=MarketInfo(Symb,MODE_MINLOT);        // Minimal number of lots 
   Free   =AccountFreeMargin();                 // Free margin
   One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);// Price of 1 lot
   Step   =MarketInfo(Symb,MODE_LOTSTEP);       // Step is changed
 
   if (Lots > 0)                                // If lots are set,
      Lts =Lots;                                // work with them
   else                                         // % of free margin
      Lts=MathFloor(Free*Prots/One_Lot/Step)*Step;// For opening
 
   if(Lts < Min_Lot) Lts=Min_Lot;               // Not less than minimal
   if (Lts*One_Lot > Free)                      // Lot larger than free margin
     {
      Alert(" Not enough money for ", Lts," lots");
      return;                                   // Exit start()
     }
//--------------------------------------------------------------- 8 --
   // Opening orders
   while(true)                                  // Orders closing loop
     {
      if (Total==0 && Opn_B==true)              // No new orders +
        {                                       // criterion for opening Buy
         RefreshRates();                        // Refresh rates
         SL=Bid - (StopLoss)*Point;//New_Stop(StopLoss)*Point;     // Calculating SL of opened
         TP=Bid + (TakeProfit)*Point;//New_Stop(TakeProfit)*Point;   // Calculating TP of opened //PW 10/05/15 New_stop removed to understand how LS works. To be put back in 
         Alert("Attempt to open Buy. Waiting for response..");
         Ticket=OrderSend(Symb,OP_BUY,Lts,Ask,2,SL,TP);//Opening Buy
         if (Ticket > 0)                        // Success :)
           {
            Alert ("Opened order Buy ",Ticket);
            return;                             // Exit start()
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
            continue;                           // Retrying
         return;                                // Exit start()
        }
      if (Total==0 && Opn_S==true)              // No opened orders +
        {                                       // criterion for opening Sell
         RefreshRates();                        // Refresh rates
         SL=Ask + StopLoss*Point;//New_Stop(StopLoss)*Point;     // Calculating SL of opened
         TP=Ask - TakeProfit*Point;//New_Stop(TakeProfit)*Point;   // Calculating TP of opened
         Alert("Attempt to open Sell. Waiting for response..");
         Ticket=OrderSend(Symb,OP_SELL,Lts,Bid,2,SL,TP);//Opening Sell
         if (Ticket < 0)                        // Success :)
           {
            Alert ("Opened order Sell ",Ticket);
            return;                             // Exit start()
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
            continue;                           // Retrying
         return;                                // Exit start()
        }
      break;                                    // Exit while
     }
//--------------------------------------------------------------- 9 --
   return;                                      // Exit start()
  }
 

You are probably running this in the strategy tester using open prices only.

I had a look at the sample EA and it is quite a poor example

for(int i=1; i>=OrdersTotal(); i++)          // Loop through orders
     
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If there is the next one

This loop is only effective of there is only 1 open order

 

if (Total==0 && Opn_B==true) 

//

if (Total==0 && Opn_S==true)  

//

 Total wasn't changed in the code after closing the opposite order, so a new order cannot be opened until the next tick when Total will equal 0

 Try

if (OrdersTotal()==0 && Opn_B==true) 

//

if (OrdersTotal()==0 && Opn_S==true)  

//

 and it should open the new order in the same tick

 

GumRai, 

 

Thank you very much for taking the time to give me a hand - Yes I had dismissed role of 'Total'.  A newbie error...

What you are saying makes sense, I'll go and try it now.

 

Thanks again for your help, saved me a lot of figuring out!

Paddy.  

Reason: