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

 

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?

 
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.
     

    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);
    
    }
    
    
     

    Hi,


    Thank you for the replies. I tried the first method with this:


    bool NewBar() {
       static datetime previous_time = 0;
       datetime current_time = iTime(Symbol(), Period(), 0); 
       if (previous_time!=current_time){
       previous_time = current_time;
       return(true);
       }
       return(false);
       }
    
    If (NewBar()){//open trades}

    It worked, but now some trades that were supposed to open did not. I can't figure out why.


    Regarding the second method, how should currentBarTime be defined?

     
    Denislav Stoychev #:

    .. It worked, but now some trades that were supposed to open did not. I can't figure out why.

    Hi,

    If you use that method then you should write 

     previous_time  = current_time;

    after your EA successfully open new trade.

    Regards.

     
    Fernando Carreiro #:

    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.

    Simpler approach.

    Create a static or global datetime variable. In OnTick, if there is a position open, update the variable with bar time. Do not open if variable equals bar time.

     
    William Roeder #: Simpler approach. Create a static or global datetime variable. In OnTick, if there is a position open, update the variable with bar time. Do not open if variable equals bar time.
    Thank you!
     
    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:

    It is cool bro. Thanks.

     

    Hi,


    Thank you all for the suggestions. The easiest way I found was this:


    void OnTick() {
    
    // Open trades on a New Bar Only
    static datetime Current  = WRONG_VALUE;
                    datetime Previous = Current;
                                  Current  = iTime( _Symbol, _Period, 0 );
                    bool     NewBar  = ( Current != Previous );
    
    if(NewBar) { //open trades }
     
    Denislav Stoychev #: The easiest way I found was this:

    That isn't the answer to your original question. That only allows a trade at the start of the bar.

    Not when a condition triggers mid-bar.

     
    William Roeder #:

    That isn't the answer to your original question. That only allows a trade at the start of the bar.

    Not when a condition triggers mid-bar.

    True, this is exactly the point - the strategy condition in my case is only entries at the start of the bar. 
    Reason: