Closed bar and time

 
Hi,

New to MQL4 and I have two questions:

1. I want to perform an operation after a bar has closed, regardless of period. How do I test that a bar has closed so that I can perform my operation. I saw one example on the site that didn't work very well.

2. I travel quite a bit and I want to be able to trade different pairs in different markets. How can I determine when the Asian, UK or US market is open if I'm in New York, Los Angeles or Maui?

Thanks,

FG
 

1:

int oldBarTime;

init(){

     oldBarTime = Time[0];
}



start(){
     ...
     ...
     if(oldBarTime != Time[0]){
          oldBarTime = Time[0];
          ... a new bar has just started...
... set a flag or do what you need to do.....
} return(0); }

2. Chart time won't change when you travel.

ww.marketclocks.com is useful

 
All I did was have an alert fire whenever a bar closed and the results were not consistent. Any ideas why?

When I say inconsistent I mean I was getting an alert at 3 to 5 seconds after the minute, then some times 50 or 51 seconds after the minute.

Thanks,

FG
 

New bar occurs with the first tick of the new bar, not according to clock time. No tick, no bar.

New tick may come at any time, or never.

MT4 Charts can even have a hole where no tick was received during the clock period of that
bar (not uncommon on 1 minute charts), in fact that occurs on may timeframes every weekend.

 
Thanks, you are very knowledgeable. So when I hover over bars in a chart and they all show perfect timing 18:30, 18:45, 19:00 that's not accurate?

What other method is there to have a block of code execute every n minutes?

Again, much thanks in advance,

FG
 

The bars represent time periods, so the times are "correct"

But, a new bar will not appear on a chart until there is a tick in that time period

If no tick comes, there will be no bar for that period. how many bars do you have for last Saturday?

---

indicators and EA's depend on a new tick to cause the code to be run

A script does not wait for ticks, it starts when you attach it to a chart.

Create a loop in the script, and it will continue to execute indefinately.

You can use LocalTime() for timekeeping to the second.

If you do create a loop, put a Sleep() in it or you may find yourself trapped in the loop at 100% CPU

script:

int start(){
     Comment("Waiting");
     while(!IsStopped()){
         if(MathMod(TimeLocal(), 15) == 0){
              string time = TimeToStr(LocalTime(), TIME_DATE|TIME_SECONDS);
              Comment("Time was ", time);
         }
         Sleep(100);
     }   
     return(0);
}
 
Thanks a ton Phy.

FG
 
I tried it last night and it worked great. I also got a chance to see what you meant by "a new bar will not appear on a chart until there is a tick in that time period". This brings up another question. Does the switch of Bar[0] to Bar[1] and the creation of the new Bar[0] also wait on a tick in the new time period?

Thanks,

FG
 


Yes

bar0 is always the rightmost bar on the chart. Doesn't matter what time it is.
Chart doesn't move unless new bar gets created. Look at weekends for how it works.
No tick on weekend, no bar. The last bar of Friday is bar0 until the first tick of new week.

There is nothing in MT4 that says "weekend start" and "weekend End". The ticks (and therefore
the bars) suddenly stop, then later, ticks start again (and bars are created)

 
Again, thanks a lot.

FG
Reason: