wemersonrv

MQL4 Excluir ordem pendente com sua contrária tocando no SL

Nov 16th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. void LimpaOrdens(){
  2.  
  3.    int i=0, count=0, total=0;
  4.    int ticketp=0; // ticketp = ticket da ordem pendente que vamos analizar
  5.    int typep=0;   // typep = Typo da ordem pendente que vamos analizar
  6.    bool sucesso=false; // Sucesso em encerrar a ordem pendente
  7.    
  8.    // 1 - Verifica se tem somente uma ordem pendente, sem mais ordens abertas
  9.    // percorre Histórico de ordens abertas ( MODE_TRADES )
  10.    for( i=OrdersTotal()-1; i>=0; i++){
  11.       if( !OrderSelect(i, SELECT_BY_POS, MODE_TRADES ) ) continue;
  12.       if( OrderMagicNumber() != MagicNumer ) continue;
  13.       if( OrderType() != OP_BUY && OrderType() != OP_SELL ){
  14.          count++;
  15.          total++;
  16.          ticketp=OrderTicket();
  17.          typep=OrderType();
  18.       }  
  19.    }
  20.    
  21.    // Total de ordens pendentes abertas deve ser 1
  22.    // Total de ordens abertas também deve ser somente 1
  23.    // Se um dos do contadores for diferente de 1, aborta
  24.    if( count!=1 || total!=1) return ;
  25.    
  26.    // Localiza a Ordem ATIVA Contrária mais recente ( se fechou no TP )
  27.    if( !OrderSelect(ticketp, SELECT_BY_TICKET) ) return;
  28.    
  29.    // Percorre histórico de ordens ( MODE_HISTORY ), da mais e recente para a mais antiga
  30.    // Comno iremos filtrar as ordens, a priemira ordem encontrada que satisfizer as condições
  31.    // Será a orden que precisamos
  32.    for( i=OrdersTotal()-1; i>=0; i++){
  33.       if( !OrderSelect(i, SELECT_BY_POS, MODE_HISTORY ) ) continue;
  34.       if( OrderMagicNumber() != MagicNumer ) continue;
  35.       // Ordem em questão não pode ser pendente, somente ativa OP_BUY ou OP_SELL
  36.       if( OrderType() != OP_BUY && OrderType() != OP_SELL ) continue;
  37.       // Ordem de compra OP_BUY NÃO fechou no Take Profit ? Então não serve
  38.       if( OrderType() == OP_BUY && OrderClosePrice() < OrderTakeProfit() ) continue;
  39.       // Ordem de venda OP_SELL NÃO fechou no Take Profit ? Então não serve
  40.       if( OrderType() == OP_SELL && OrderClosePrice() > OrderTakeProfit() ) continue;      
  41.       // Bem... ao chegar aqui a ordem deve ser :
  42.       // 1 - Do tipo ativa OP_BUY ou OP_SELL
  43.       // 2 - Deve ter fechado no Take Profit
  44.       // Agora basta verificar se é a ordem ativa contrária à ordem pendente
  45.       if( (typep==OP_BUYSTOP && OrderType()==OP_SELL) || (typep==OP_SELLSTOP && OrderType()==OP_BUY) ){
  46.          // Achamos nossa danadinha... vamos fechá-la
  47.          while(IsTradeContextBusy()) Sleep(100);
  48.          sucesso=OrderDelete(ticketp);
  49.          break;
  50.       }  
  51.    }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment