Could You check My EA

 

Hi!
I don't know why OrderClose() is not working. Could You help me please.

extern double TakeProfit = 500;
extern double Lots = 0.1;
extern double TrailingStop = 100;
extern double StopLoss = 200;

double sar,imacurrent;
int cnt, ticket, total;


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
void OpBuySell()
{
imacurrent=iMA(NULL, 0, 5, 0, MODE_SMMA, PRICE_MEDIAN, 0);
sar=iSAR(NULL, 0, 0.02, 0.2, 0);


if(sar<imacurrent)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-(StopLoss*Point),Ask+(TakeProfit*Point),"sar sample",16384,0,Green);
return(0);
}
if(sar>imacurrent)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+(StopLoss*Point),Bid-(TakeProfit*Point),"sar sample",16384,0,Red);
return(0);
}
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
void CloseOpBuySell()
{
if(OrderType()==OP_BUY) // long position is opened
{
if(sar>imacurrent)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,Red);

}
}
if(OrderType()==OP_SELL) // short position is opened
{
if(sar<imacurrent)
{
OrderClose(OrderTicket(),OrderLots(),Ask,5,Red);

}
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
RefreshRates();

int total=OrdersTotal();
total=OrdersTotal();
if(total<1)
{

OpBuySell();
return(0);
}
for(int cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol()) // check for symbol
{

CloseOpBuySell();
return(0);
}
}

}
//+------------------------------------------------------------------+

Files:
 

hi nottin,
you should put all the functions to be performed in the start() part.
init() is used only when incorporating the expert into the chart, you may put some pre-set calculation which is only going to be done ONCE before your codes run.
deinit() is used while removing the expert from the chart

the link below should give you a better understanding
https://book.mql4.com/programm/structure

 
First, don't paste code, use the SRC button, or if very long attach it.
Second, once you open an order, imacurrent and sar are NEVER updated.
 

Thanks Guys,
I put all code to the start() part, and EA works excellent.