How to close a position without openning another one

 

 

Hello to all,

Regarding the code scheme below, could anybody help me? It closes a position only when the next signal happens. Then it, instantly, opens another position, the opposite to the first.

extern double TakeProfit=400.0;
extern double Lots=0.01;
int NumeroMagico=170225, Sinal_vencedor=0, Sinal_perdedor=0;

int init() {
  double StopMinimo=MarketInfo(Symbol(), MODE_STOPLEVEL);
  if (TakeProfit<=StopMinimo) TakeProfit=0;
  if (TrailingStop<=StopMinimo) TrailingStop=0;
  return(0);
}

int deinit() {
  return(0);
}
  
int Teste_condicao (...) {
  
  static int condicao_atual = 0;
  
  if(...) {
    condicao_atual=1;
    return(condicao_atual);
  }
      
  if(...) {
    condicao_atual=5; 
    return(condicao_atual);
  }    
    
  else return(condicao_atual); 
}

int start () {
  int cnt, ticket, total;
  double ... ;
  
  if(Bars<100) {
    Print("bars less than 100");
    return(0);
  }                                                                       
  
  Sinal_vencedor = Teste_condicao (...); 
                                      
  total = OrdersTotal();
  
  if(total<1) {          
    if(Sinal_vencedor==1) {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA v0.91",NumeroMagico,0,Green); 
      if(ticket>0) {
        if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
        Print("BUY order opened : ",OrderOpenPrice());
        Sinal_perdedor=5;
      }
      else Print("Error opening BUY order : ",GetLastError()); 
      return(0);
    }
    if(Sinal_vencedor==5) {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA v0.91",NumeroMagico,0,Red);
      if(ticket>0) {
        if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
        Print("SELL order opened : ",OrderOpenPrice());
        Sinal_perdedor=1;
      }
      else Print("Error opening SELL order : ",GetLastError()); 
      return(0);
    }
    return(0);
  }
  
  if((Sinal_vencedor==0) && (Sinal_perdedor==1)) Sinal_vencedor=1;
  if((Sinal_vencedor==0) && (Sinal_perdedor==5)) Sinal_vencedor=5;
    
  if(total>0) {
  for(cnt=0;cnt<total;cnt++) { 
    OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); 
    if(OrderSymbol()==Symbol()) {
      if(OrderType()==OP_BUY) {     
        if(Sinal_vencedor==5) {
          OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); 
          return(0);                                          
        }        
      }
      if(OrderType()==OP_SELL) {    
        if(Sinal_vencedor==1) {
          OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); 
          return(0);                                          
        }        
      }
    } 
  }  
  }  
  return(0);
}    

However, in the case of 'condicao=0', it was intended just to close the current position and 'do nothing more', only wait for a next signal and, finally, open a new position. 

The way it is, all the profit may return to the market.

Thanks a lot for any help.

JR

 
Jr_attain: It closes a position only when the next signal happens. Then it, instantly, opens another position, the opposite to the first.
  1. No it does not. It closes the order and then on some subsequent tick it opens another position.
    That can only happen if Teste_condicao(...) has returned a 1 or a 5
        if(Sinal_vencedor==1) {
          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA v0.91",NumeroMagico,0,Green); 
    Don't check levels. Check for a change in level.
        if(Sinal_vencedor==1 && Sinal_perdedor==0){
          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA v0.91",NumeroMagico,0,Green); 

  2.   if(total>0) {
      for(cnt=0;cnt<total;cnt++) { 
    The if statement is unnecessary; if the count is zero the for loop does nothing. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  3. Don't use constants when you mean an enumeration
      static int condicao_atual = 0;
        condicao_atual=1;
        condicao_atual=5; 
        if(Sinal_vencedor==1) {
            Sinal_perdedor=5;
        if(Sinal_vencedor==5) {
      if((Sinal_vencedor==0) && (Sinal_perdedor==1)) Sinal_vencedor=1;
      if((Sinal_vencedor==0) && (Sinal_perdedor==5)) Sinal_vencedor=5;
            if(Sinal_vencedor==5) {
            if(Sinal_vencedor==1) {
    Write self documenting code.
    enum Direction { NONE, SHORT, LONG=5 };
      static Direction condicao_atual = NONE;
        condicao_atual=SHORT;
        condicao_atual=LONG; 
        if(Sinal_vencedor==SHORT) {
            Sinal_perdedor=LONG;
        if(Sinal_vencedor==LONG) {
      if(Sinal_vencedor==NONE && Sinal_perdedor==SHORT) Sinal_vencedor=SHORT;
      if(Sinal_vencedor==NONE && Sinal_perdedor== LONG) Sinal_vencedor=LONG;
            if(Sinal_vencedor==LONG) {
            if(Sinal_vencedor==SHORT) {

 
WHRoeder:
  1. No it does not. It closes the order and then on some subsequent tick it opens another position. That can only happen if Teste_condicao(...) has returned a 1 or a 5
  2. The if statement is unnecessary; if the count is zero the for loop does nothing. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  3. Don't use constants when you mean an enumeration
    Write self documenting code.

WHRoeder,

Thank you so much for the valuable help.

JR