Opening One trade at the same bar...

 

Hi all,

I want to open only one trade at the same bar. I use M5 timefrime. My EA opens trades according to some rules.

However, I want to open only one trade in a bar. Do you have any idea how I can do that?

Best wishes.

 
Murat Yazici:

Hi all,

I want to open only one trade at the same bar. I use M5 timefrime. My EA opens trades according to some rules.

However, I want to open only one trade in a bar. Do you have any idea how I can do that?

Best wishes.

 use a conditional  ie only new trade when iTimeLastM5 < iTimeNewM5

 
rod178:

 use a conditional  ie only new trade when iTimeLastM5 < iTimeNewM5

Hi rod178, Thank you for information. However, I don't understand exactly. 

iTimeLastM5 < iTimeNewM5

What are the mean Last and New? About last trade and new trade?

 
if(mostRecentTradeTime() < Time[0])
   //ok to trade.
 
Murat Yazici:

Hi all,

I want to open only one trade at the same bar. I use M5 timefrime. My EA opens trades according to some rules.

However, I want to open only one trade in a bar. Do you have any idea how I can do that?

Best wishes.


Yes, try using this function:

bool CheckForNewBar() {
   static datetime PTime;
   datetime CTime = SeriesInfoInteger (Symbol(), Period(), SERIES_LASTBAR_DATE);

   if (PTime == 0) {
      PTime = CTime;
      return (false);
   } else if (PTime != CTime) {
      PTime = CTime;
      return (true);
   } else {
      return (false);
   }
}

The first time it is called, it will always return False until the next bar.  If this is not the desired behavior, you can remove the "PTime == 0" If-statement so that the function will return True the first time it is called.  You could then place this function before your order opening code, and only one order will be placed per bar.

 
Shalem:


Yes, try using this function:

The first time it is called, it will always return False until the next bar.  If this is not the desired behavior, you can remove the "PTime == 0" If-statement so that the function will return True the first time it is called.  You could then place this function before your order opening code, and only one order will be placed per bar.

Thank you Shalem. Problem solved. Thnak you all!

Best.

 
Murat Yazici:

Thank you Shalem. Problem solved. Thnak you all!

Best.

That code is only appropriate for when you need to detect a new bar. What happens when your EA reinitializes for some reason? It won't recover properly.