How to tell the EA to not open a position if another was closed on the same candle?

Denislav Stoychev  

Hi,

I'm not a programmer, but want to learn and made a very basic EA using some EA creator. The problem is that each time the TP/SL is hit, while the Entry condition is still met on that same candle, it opens a new trade, which most of the time is not the optimal entry - see attached.

How to tell the EA to not open a new position if another was closed on the same candle?

Fernando Carreiro  
Denislav Stoychev: Hi, I'm not a programmer, but want to learn and made a very basic EA using some EA creator. The problem is that each time the TP/SL is hit, while the Entry condition is still met on that same candle, it opens a new trade, which most of the time is not the optimal entry - see attached. How to tell the EA to not open a new position if another was closed on the same candle?

You have to track the condition with a variable.

  1. You start by declaring a boolean variable and initialise it to "false".
    It should be either a globally scoped variable or a static local variable.
  2. At the start of every new bar, you reset that variable to "false".
  3. When you close an order (or detect that it was closed), you set the variable to "true".
  4. Every time you need to place an order, you first check it:
    If it is "true" you abort the placing of an order,
    if not, proceed with placing the order.
EDIT: Please don't use code generators. Learn to do it all on your own, even if it takes longer to learn to do it. By using a generator, you don't learn the basics and learn bad coding habits because most generators produce horrible code.
    Marzena Maria Szmit  

    Or you can check if there was any closed trade at the same bar already before it opens a new trade.

    So before your function which opens new trades add

    if(!isClosedTradeOnBar()) {//open trades}

    And this function should be like that:

    bool isClosedTradeOnBar(){
    
    HistorySelect(currentBarTime,TimeCurrent()); //look for trades only from current bar
    
       for (int i = HistoryDealsTotal()-1; i >= 0; i--)
    
       {
    
          ulong ticket=HistoryDealGetTicket(i);
    
          //you can check magic and symbol here to find only trades from the EA
    
          if(HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT){
    
                return(true);//closed trade on this bar
    
          }  
    
       }
    
    return(true);
    
    }