Orders management and EA init

 

Rehi, ;)


Got another problem while forward testing my EA.


EA opened a short on EURCHF.

I initialized EA on chart (short always in trade list).

Indicator says to EA to close the losing short but EA dont want to close it, and i don't see what's going wrong in my close loop. (i use global variable to communicate with my indicator. -2 is signal for closing a trade, profit or loss. signal displayed by EA is ok at -2. So short should be closed but is not).

See the simple code under.


Thanks for advance,

Dam.

----


//---- Close Short
if ( signal==-2 ) //
{
ordtot=OrdersTotal();
for ( i=0; i<ordtot; i++)
{
OrderSelect(i,SELECT_BY_POS);
if ( OrderMagicNumber()==Expert_Id && OrderType()==OP_SELL )
{
// check whether trade context is free
TradeAllow = _IsTradeAllowed();
if ( TradeAllow < 0 )
return(-1);
if ( TradeAllow == 0 )
RefreshRates();
close=OrderClose(OrderTicket(),LotNum,Ask,slippage,Green);
if ( !close )
{
Print("Short Ticket ",OrderTicket()," has close error ",GetLastError(),". Retry in 5 seconds.");
Sleep (5000);
// check whether trade context is free
TradeAllow = _IsTradeAllowed();
if ( TradeAllow < 0 )
return(-1);
if ( TradeAllow == 0 )
RefreshRates();
close=OrderClose(OrderTicket(),LotNum,Ask,slippage,Green);
if ( close )
Print("Short Ticket ",OrderTicket()," has closed successfully.");
else
{
Print("Problem with Short Close - See error code");
return(0);
}
x=0;
ArrayInitialize(tn,0); ==> my famous init error, see my another topic!!!!
}
}
}

 

Ok found my answer while sleeping.... The EA after init doesn't closed order because of different MN.... Resolved. :P

Reason: