Problem with opening orders

[B][U]hi Traders,

[/U]im learning expert programming these days and made small experts depends on the common indicators in MT4.[/B]

I made an EA to open 1 buy order when Parabolic SAR is under the current price and the opposite for sell.
and closes buy orders when SAR goes over the price and the opposite for sell.
it worked fine in the first order in the back-test and closed the order fine too, then the EA began to open (many many many …) buy orders in the very same bar (not 1 as I coded it)
(as shown in the attached photo)

N.B: I zoomed the bars as possible. Hope that helps

can anyone help me solve this code problem?

im attached a photo and the code for more details

thnx in advance for helping

//-----------------------------------------------------------------

void OnTick()
  {
//---
double SAR=iSAR(Symbol(),0,0.02,0.2,1); // ----------- opening orders
if (SAR<Ask && OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"",0,0);
else if (SAR>Bid && OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",0,0);
   
   for(int i=0;i<OrdersTotal();i++)   //------------------ closing orders
   {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
     {Comment("Order Selected");
      if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && SAR>Bid)
       OrderClose(OrderTicket(),0.1,Bid,3);
      if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && SAR<Ask)
       OrderClose(OrderTicket(),0.1,Ask,3);
     }
   }
 }

//-----------------------------------------------------------------


hany,

Once again good work at learning coding…well done.

Your bug lies in the fact that you are opening an order when the ask falls below the sar…the ea then goes on to check if it should close the order, and it then finds the price above the bid which it always will be, so it closes the order…it then simple repeats the process.

Thnx kenny
I’ve been learning alot in the last few weeks and I’m improving my skills day after day.

I got my mistake and dealt with ask in both opening and closing conditions and the bug is fixed now.
Now i got the same bug but with Bollinger bands. It doesn’t appear much as the latest bug, but it affects my profit.
I couldn’t apply what I did in the recent bug, because as u can see in the code below, it opens buy order when bar(2) closes outside the bottom line && bar(1) closes inside the bottom line and it exits the order when bid (touches) the upper line

here’s the code

if(OrdersTotal()==0)
{
 if (iClose(Symbol(),0,2) < bandLower2 && iClose(Symbol(),0,1) > bandLower1 && Stoch1<20)
  {
 Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0,NULL,Magic,0);
 
 
  }
if (iClose(Symbol(),0,2) > bandUpper2 && iClose(Symbol(),0,1) < bandUpper1 && Stoch1>80)
 {
 Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,0,0,NULL,Magic,0);
 }
}
for(int i=0;i<OrdersTotal();i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
          { 
           if(OrderType()==OP_BUY)
           if(OrderSymbol()==Symbol())
            {
             if(Ask >= bandUpper)
             OrderClose(OrderTicket(),LotSize,Bid,3);
            }
           if(OrderType()==OP_SELL)
           if(OrderSymbol()==Symbol())
            {
             if(Bid <= bandLower)
             OrderClose(OrderTicket(),LotSize,Ask,3);
          
            }
          } 
        } 

Any advice ?


hany,

I’m not sure what you want to do…in the picture, bar 2 closes inside the bands and bar 1 closes outside the upper band. Of course because this is where the last bar closed, this is where the ask price is when your trade is open, so it closes it.

just to be clear…the bollinger band consists of an upper band, lower band and a moving average. I am not sure which lines you are referring to in your post.

TIP: instead of using bid and ask when closing and order, just use OrderClosePrice()…the computer will automatically substitute the correct price when it is time to close the order.

Guided by your tips i found a solution to the bug
I added a condition to the OrderSend function telling EA not to open buy orders if Ask goes over the middle line and same for sell ofc (as indicated in the photo).
That solved the bug permanently.

Thnx for ur tips kenny :slight_smile:

//==================================
if(OrdersTotal()==0)
{Comment((bandLower+bandUpper)/2);
 if (iClose(Symbol(),0,2) < bandLower2 && iClose(Symbol(),0,1) > bandLower1 && Stoch1<20 && Ask <(bandLower+bandUpper)/2) // --------------- open buy order when conditions met
  {
 Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0,NULL,Magic,0);
 ;
  }
if (iClose(Symbol(),0,2) > bandUpper2 && iClose(Symbol(),0,1) < bandUpper1 && Stoch1>80 && Bid > (bandLower+bandUpper)/2) // --------------- open sell order when conditions met
 {
 Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,0,0,NULL,Magic,0);
 }
}
for(int i=0;i<OrdersTotal();i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
          { 
           if(OrderType()==OP_BUY)
           if(OrderSymbol()==Symbol())
            {
             if(Ask > bandUpper)
             OrderClose(OrderTicket(),LotSize,Bid,3);
            }
           if(OrderType()==OP_SELL)
           if(OrderSymbol()==Symbol())
            {
             if(Bid < bandLower)
             OrderClose(OrderTicket(),LotSize,Ask,3);
          
            }
          } 
        }
//==================================