Place order on Open Bar Only

 

I've seen a few threads about this and tried one of them, but it doesn't seem to be working.  The specific one I've referred to is:  https://www.mql5.com/en/forum/133366.


The code that I'm using is:

static bool IsNewBar;
static datetime LastBarOpenAt;

int start()
{


 if(LastBarOpenAt == Time[0]) // This tick is not in new bar
    {
     IsNewBar=false;
    }
  else
    {
      LastBarOpenAt = Time[0];
      IsNewBar = true;    
    }

// Rest of code



In real trading, I'm on M5.  The open positions were at 8:05:50 and 08:06:36.  I thought of using something like below.  Any validity to that?  When I print the times in backtest for open position only, every output is a multiple of 5 for minutes and zero for seconds.  Should I give seconds < 5 to allow a few seconds to purchase?


double TimMinute = TimeMinute(TimeCurrent());
Print("TimeMinute = ",TimMinute);


double TimSeconds = TimeSeconds(TimeCurrent());
Print("TimeSeconds = ",TimSeconds);

int TimeTest = 1;
if(TimMinute != 0 || TimMinute != 5 || TimMinute != 10 || TimMinute != 15 || TimMinute != 20 || TimMinute != 25 || TimMinute != 30 || TimMinute != 35 || TimMinute != 40 || TimMinute != 45 || TimMinute != 50 || TimMinute != 55) {TimeTest = 0; return(0);}
Print("TimeTest = ",TimeTest);

int TimeTestSec = 1;
if(TimSeconds != 0) {TimeTestSec = 0; return(0);}
Print("TimeTestSec = ",TimeTestSec);

if(TimeTest == 1 && TimeTestSec == 1){Print("SUCCESS");
 
stockengineer12: In real trading, I'm on M5.  The open positions were at 8:05:50 and 08:06:36.
  1. When you first add the script your global has no value, so you get an immediate new bar.
  2. New bar changes when you get the first tick, there may be no tick in the first minute.
  3. Simplify
    if(TimMinute != 0 || TimMinute != 5 || TimMinute != 10 || TimMinute != 15 || TimMinute != 20 || TimMinute != 25 || TimMinute != 30 || TimMinute != 35 || TimMinute != 40 || TimMinute != 45 || TimMinute != 50 || TimMinute != 55) {TimeTest = 0; return(0);}

    if(TimMinute % 5 != 0) {TimeTest = 0; return(0);}
 
WHRoeder:
stockengineer12: In real trading, I'm on M5.  The open positions were at 8:05:50 and 08:06:36.
  1. When you first add the script your global has no value, so you get an immediate new bar.
  2. New bar changes when you get the first tick, there may be no tick in the first minute.
  3. Simplify
    if(TimMinute != 0 || TimMinute != 5 || TimMinute != 10 || TimMinute != 15 || TimMinute != 20 || TimMinute != 25 || TimMinute != 30 || TimMinute != 35 || TimMinute != 40 || TimMinute != 45 || TimMinute != 50 || TimMinute != 55) {TimeTest = 0; return(0);}

    if(TimMinute % 5 != 0) {TimeTest = 0; return(0);}

So depending on when I start the EA, the bars may begin on those off times? That is, the bars for EURUSD started at 8:05:50 would be 8:05:50, 8:10:50, 8:15:50, etc.?  So would my 5 min and zero seconds method work then?  Probably not, it seems. 


From the linked topic, I tried your code too.  I'm guessing since you wrote it, you'd be an advocate of this for open bar only.  I just want to see if I understand it.  It starts the script and uses that as Time0.  It says if the current time is the time of the bar start (Time[0]), return and set time Time0=Time[0].  Shouldn't it be something like if Time0 == Time[0] continue, else return?

int     start(){     static datetime Time0;
    if (Time0 == Time[0]) return(0); Time0 = Time[0];
   ...
 
stockengineer12: So depending on when I start the EA, the bars may begin on those off times? That is, the bars for EURUSD started at 8:05:50 would be 8:05:50, 8:10:50, 8:15:50, etc.? 
int     start(){     static datetime Time0;
    if (Time0 == Time[0]) return(0); Time0 = Time[0];
You misunderstood me. If you started the EA at 8:09:50, your new Bar code would indicate new bars on the first tick received (8:09:50) and then first tick received on or after 8:10:00.0, etc.
Reason: