PSAR to closing trades

 
Hi guys, I want to ask how to close order using a Parabolic SAR. I have two logic to close the price:
1. comparing the previous bar with the current bar.
double sarPrev = iSAR(NULL, 0, 0.1, 0.2, 1);
double sarCurr = iSAR(NULL, 0, 0.1, 0.2, 0);
//then comparing with the price
if(sarPrev>sarCurr) //do close trade
2. comparing with the price
if(SARValue > Bid) //close buy order
if(SARValue < Ask) //close sell order
which one of this two logic that's correct to apply if I want to close the order using PSAR?
 
I would say that the second one is what you want to use, but chart prices are bid, so don't use ask , only bid.
 
Keith Watford:
I would say that the second one is what you want to use, but chart prices are bid, so don't use ask , only bid.

So for both open buy and sell order I should use the bid price?

 
Luandre Ezra:

So for both open buy and sell order I should use the bid price?

Compare the bid price with the SAR, the SAR is calculated with the bid price, not the ask.

 
Keith Watford:

Compare the bid price with the SAR, the SAR is calculated with the bid price, not the ask.

Thanks for the reply Keith!. I already did like what you suggest but I got somewhat odd result. This is my code and I attach an image and to pin point the odd result. The order would not be close when the parabolic SAR change direction and close really far from it suppose to be. Is there an error in my code?

//closing open order
if(OrderSelect(orderId,SELECT_BY_TICKET,MODE_TRADES))    //check the order by the ticket
        {
         if(OrderSymbol() == Symbol() && OrderType()==OP_BUY)
           {
            if(PSARValue > Bid)
              {
               bool res = OrderClose(orderId,OrderLots(),Bid,0);
               if(!res)
                  Print("Error in OrderModify. Error code=",GetLastError());
               else
                  Print("Order closed successfully.");

              }
           }
         if(OrderSymbol() == Symbol() && OrderType()==OP_SELL)
           {
            if(PSARValue < Bid)
              {
               bool res = OrderClose(orderId,OrderLots(),Ask,0);
               if(!res)
                  Print("Error in OrderModify. Error code=",GetLastError());
               else
                  Print("Order closed successfully.");

              }
           }
        }
Files:
Capture.JPG  129 kb
 
Luandre Ezra:

Thanks for the reply Keith!. I already did like what you suggest but I got somewhat odd result. This is my code and I attach an image and to pin point the odd result. The order would not be close when the parabolic SAR change direction and close really far from it suppose to be. Is there an error in my code?

Hello,

Try setting

PSARValue>ASK, and

PSARValue<BID respectively.


I am working on a similar bot and had a similar problem.

 
Aidan Stcyr:

Try setting

PSARValue>ASK, and

PSARValue<BID respectively.

Please do not give wrong information.
I have already explained why NOT to use ask.

 
Luandre Ezra:

Thanks for the reply Keith!. I already did like what you suggest but I got somewhat odd result. This is my code and I attach an image and to pin point the odd result. The order would not be close when the parabolic SAR change direction and close really far from it suppose to be. Is there an error in my code?

I can't see anything obviously wrong with the snippet that you have shown except you only have slippage set to 0 which is low.

if(OrderSelect(orderId,SELECT_BY_TICKET,MODE_TRADES))    //check the order by the ticket

Where does orderId come from? Are you sure that an open order is being selected?

Incidentally

if(OrderSelect(orderId,SELECT_BY_TICKET))    //check the order by the ticket

There is no need for the MODE_TRADES as this is ignored when selecting by ticket. If the order is closed already, it will still be selected.

 
Keith Watford:

I can't see anything obviously wrong with the snippet that you have shown except you only have slippage set to 0 which is low.

Where does orderId come from? Are you sure that an open order is being selected?

Incidentally

There is no need for the MODE_TRADES as this is ignored when selecting by ticket. If the order is closed already, it will still be selected.

orderId is variable to contain the ordersend function.

orderId = OrderSend(Symbol(),OP_SELL,lotSize,Bid,0,stopLossPrice,0,NULL,magicNumber1);

Already tried to give up to 1 pip to the slippage but the result still the same. Really doesn't know what cause the close order behave like that.

 
Luandre Ezra:

orderId is variable to contain the ordersend function.

Is it a globalscope variable?

 
Keith Watford:

Is it a globalscope variable?

Yes it is.