EA to Trade only for FIRST TICK of New Candle

 
Hello Coders, Please I need your help. I recently started learning how to program EAs but I'm currently having a problem with my EA opening multiple orders per candle tick. 

I could have used the OrdersTotal function to stop this but that will ultimately limit the EA to only one order but I want to have the option of opening additional orders to those already opened for other candles that meet the entry criteria. So what I want essentially is for the EA to open only one order per candle and not for every tick of candle. I don't know what function I will need to add?
I have studied several EAs to find the solution but don't seem to find my way around it. Any help will be appreciated. Thanks.
 
Gabriel Nsor:
Hello Coders, Please I need your help. I recently started learning how to program EAs but I'm currently having a problem with my EA opening multiple orders per candle tick. 

I could have used the OrdersTotal function to stop this but that will ultimately limit the EA to only one order but I want to have the option of opening additional orders to those already opened for other candles that meet the entry criteria. So what I want essentially is for the EA to open only one order per candle and not for every tick of candle. I don't know what function I will need to add?
I have studied several EAs to find the solution but don't seem to find my way around it. Any help will be appreciated. Thanks.

1. check opened time inside orderstotal

2. check current candle time

3. ibarshft <= 1 . don't trade

 

My solution for determining first and only the first tick of a new candle ...


datetime new_candle = 0; //in global space or in OnInit()

if (new_candle != iTime(Symbol(), Period(), 0))
   {
    ...
    ...

    new_candle = iTime(Symbol(), Period(), 0);
   }
 

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:46

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.
 

Please don't post randomly in any section. MT4/mql4 has it's own section on the forum.I have moved your topic to the correct section.

 
Thanks a zillion for your response and assistance!! 
 
Fernando Carreiro #:

Please don't post randomly in any section. MT4/mql4 has it's own section on the forum.I have moved your topic to the correct section.

Ok noted sir! Thanks so much for your help
 
Fernando Carreiro #:

This worked so perfectly!! Thanks so very much for the code.

void OnTick()
  {
  
  // Check for New Bar (Compatible with both MQL4 and MQL5)
  static datetime dtBarCurrent=WRONG_VALUE;
  datetime dtBarPrevious=dtBarCurrent;
  dtBarCurrent=(datetime) SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
  bool NewBarFlag=(dtBarCurrent!=dtBarPrevious);
  
  if(NewBarFlag)
 {
  
  }
 
Gabriel Nsor #: This worked so perfectly!! Thanks so very much for the code.

That is not the same code that I directed you to from the CodeBase. It seems to be from an older version.

Also, I have edited your post, but in the future please use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Fernando Carreiro #:

That is not the same code that I directed you to from the CodeBase. It seems to be from an older version.

Also, I have edited your post, but in the future please use the CODE button (Alt-S) when inserting code.

Yes, not exactly the same code but very similar to the one you directed me to. Both codes working perfectly!

Thanks so much! I did not know about the code button. I'm learning so many new things here