Parabolic SAR indicator for closing order error

 
Hi!

I've created an EA that will close the open order if there's a changing in parabolic SAR indicator. For this EA to work I want to check the Indicator for every new candle since the change of the PSAR is in every new candle.
Here's the code for every new candle function:
datetime newCandleTime = TimeCurrent();
bool isNewCandle()
  {
   if(newCandleTime == iTime(Symbol(),0,0))
      return false;
   else
     {
      newCandleTime = iTime(Symbol(),0,0);
      return true;
     }
  }
And for the order close code:
if(OrderSymbol() == Symbol() && OrderType()==OP_BUY)
   {
   if(isNewCandle())
      {
      if(PSARValue(1) < Close[1] && PSARValue(0) > Close[0])
         {
         bool res = OrderClose(orderId,OrderLots(),Bid,0);
         if(!res)
            Print("Error in closing order. Error code=",GetLastError());
         else
            Print("Order closed successfully for ticket: ", OrderTicket());
            }
         }
      }
if(OrderSymbol() == Symbol() && OrderType()==OP_SELL)
   {
   if(isNewCandle())
      {
      if(PSARValue(1) > Close[1] && PSARValue(0) < Close[0])
         {
         bool res = OrderClose(orderId,OrderLots(),Ask - spread,0);
         if(!res)
            Print("Error in closing order. Error code=",GetLastError());
         else
            Print("Order closed successfully for ticket: ", OrderTicket());
            }
         }
      }
The issue is that sometimes for the short position the EA does not close when the PSAR indicator changing direction. I've add the screen capture for the issue. In the backtest this issue is happened twice and I don't know why this is happen. 
Files:
Capture.JPG  90 kb
 
  1. if(OrderSymbol() == Symbol() && OrderType()==OP_BUY)
       {
       if(isNewCandle())
          {
          if(PSARValue(1) < Close[1] && PSARValue(0) > Close[0])
    There are no mind readers here and our crystal balls are cracked. Your post is without context. You can not use any Trade Functions until you first select an order. And it matters what direction you loop if you potentially open multiple orders.

  2. Move all non-changing items outside your loop.

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

  3. Why are you testing whether it was on the correct side? If it is current on the wrong side, you want to close the order. Simplify your code.

  4. bool res = OrderClose(orderId,OrderLots(),Ask - spread,0);

    You buy at the Ask and sell at the Bid.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

Reason: