exit on psar

 

Hi,



I just can;t exit on psar in my EA. It does this weird thing where it exits and enters numerous times. Also it exits on will, usually not on first, but on a second change in psar



I attached the file for reference



int start()
{
int cnt=0, total;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0);
}
double fastQQE1 = iCustom(NULL,0,"QQEA",fastQQEparm,14,4.236,0,1);
double fastQQE2 = iCustom(NULL,0,"QQEA",fastQQEparm,14,4.236,1,1);
double slowQQE1 = iCustom(NULL,0,"QQEA",slowQQEparm,14,4.236,0,1);
double slowQQE2 = iCustom(NULL,0,"QQEA",slowQQEparm,14,4.236,1,1);
if(OrdersTotal()<1)
{
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money");
return(0);
}
if(slowQQE1 > slowQQE2)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Points,"macd sample",16384,0,Red);
if(GetLastError()==0)Print("Order opened : ",OrderOpenPrice());
return(0);
}
if(slowQQE1 < slowQQE2)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Points,"macd sample",16384,0,Red);
if(GetLastError()==0)Print("Order opened : ",OrderOpenPrice());
return(0);
}
return(0);
}
total=OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(iSAR(NULL,0,0.02,0.2,2)<Bid && iSAR(NULL,0,0.02,0.2,1)>Bid)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Points*TrailingStop)
{
if(OrderStopLoss()<Bid-Points*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Points*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
else
{
if(iSAR(NULL,0,0.02,0.2,2)>Ask && iSAR(NULL,0,0.02,0.2,1)<Ask)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Points*TrailingStop))
{
if(OrderStopLoss()==0.0 ||
OrderStopLoss()>(Ask+Points*TrailingStop))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Points*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}

Files:
qqe_ea.mq4  5 kb
 

J


1) As you are executing this every tick, the SAR can & will quite often flip-flop above/below the price during the formation of a bar.

It only 'settles' on completion of the bar, so is better checked on first tick of the next bar only

2) The code above does OrderSend by MagicNumber (16384) but does not check this on OrderClose, so... the EA is vulnerable to interfence from other orders placed by other EA's or manually - makes me wonder if other EA's are closing the orders of this EA?!


IMHO - SAR is a bit laggy to do this sort of thing, yes, its easy to code but its original purpose was only to set stop loss values...


My 2c worth

-BB-