Close trades at EA deinit

To add comments, please log in or register
Damien Olanier
102
Damien Olanier  

This simply does'nt work. My EA opens lot of trades, and i just want when i close EA that EA closes all his trades. I think it doesn't work because deinit is fast, faster that time required to close all trades.


In case of bugs can someone see code below? I insert CloseFunction in deinit function. Only one or two orders are close, but the others always opened.....


Thanks for advance,

Dam.


void CloseFunction()
    {
     ordtot=OrdersTotal();
     for ( i=0; i<ordtot; i++)
        {
         OrderSelect(i,SELECT_BY_POS);
         if ( OrderMagicNumber()==Expert_Id && OrderSymbol()==Symbol() )
           {
            // check whether trade context is free
            TradeAllow = _IsTradeAllowed();
            if ( TradeAllow < 0 ) 
              return(-1); 
            if ( TradeAllow == 0 )
              RefreshRates();
            Print(" Currency", Symbol()," is already traded while EA (de)initing- Closing Trades - BEWARE.");
            if ( OrderType()==OP_BUY )
              close=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Green);
            if ( OrderType()==OP_SELL )
              close=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Green);
            if ( !close )
              {
               Print("Ticket ",OrderTicket()," has close error ",GetLastError(),". Retry in 2 seconds.");
               Sleep (2000);
               // check whether trade context is free
               TradeAllow = _IsTradeAllowed();
               if ( TradeAllow < 0 ) 
                 return(-1); 
               if ( TradeAllow == 0 )
                 RefreshRates();
               if ( OrderType()==OP_BUY )
                 close=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Green);
               if ( OrderType()==OP_SELL )
                 close=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Green);
               if ( close )
                 Print("Ticket ",OrderTicket()," has closed successfully.");
               else 
                 Print("Problem with Close at (de)init - See error code");  
              }
           }
        }
    } 
blogzr3
506
blogzr3  

You are just attempting to close a second time if the first one fails. The second close could also fail, so you are left with open trades.


You need to retry a few more times in a loop with a Sleep in between, and exit if retried for more than say 5 times without succeeding.


What is your slippage? You might need to increase it if too small.

Damien Olanier
102
Damien Olanier  
blogzr3:

You are just attempting to close a second time if the first one fails. The second close could also fail, so you are left with open trades.


You need to retry a few more times in a loop with a Sleep in between, and exit if retried for more than say 5 times without succeeding.


What is your slippage? You might need to increase it if too small.

Thanks Blog for answers.


I will look for the "big closin loop" trail.


slippage is 2+spread (so it is for currencies traded between 4 and 20). I will try that too, but i don't have errors related to slippage in journal.


But i don't have any error in my expert journal, so i hardly believe this is this cases. See the "close at deinit" print at the second retry. I don't see that in journal. Some trades are closed, others aren't.


Very strange.... I have seen in mql manual that deinit should not loop long. Is there a limit (2.5s?).

fbj
1047
fbj  

i use attached script whenever hit panic button or want close everything - is shotgun approach. (i gave name so is top of navigators script name list - so always 'know' where to get too fast! :)

maybe look at code and give ideas...

Best

fbj
1047
fbj  

4got mention: must ALWAYS loop high --> low when using orderstotal + orderclose

look on forum - much info as why must do. i got comment in attachated file of last post - explain why...

To add comments, please log in or register