Opening One trade at the same bar...

To add comments, please log in or register
Murat Yazici
1522
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.

rod178
276
rod178  
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

Murat Yazici
1522
Murat Yazici  
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?

nicholi shen
2392
nicholi shen  
if(mostRecentTradeTime() < Time[0])
   //ok to trade.
Shalem Loritsch
1536
Shalem Loritsch  
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.

Murat Yazici
1522
Murat Yazici  
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.

nicholi shen
2392
nicholi shen  
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.
To add comments, please log in or register