Detecting start of new candle?

 

Programming my first EA, I face the problem of how best to detect the start of a new hourly candle. There appears to be only one method, and that is to wait for the first tick to come along which falls into the next time period. That is because there appears to be only one way the start() function is called in my EA, and that is when the server pumps out a new tick. So, if a tick occurs at 01:59, and the next tick comes at 02:02, the EA detects the end of the 01:00 candle and the start of the 02:00 candle at 02:02, two minutes after the top of the hour. My EA is designed to run on the H1 chart, and it makes trading decisions based upon the close prices of hourly candles. I would like my EA to make those decisions at the top of the hour, rather than wait for the first tick that occurs in the new hour. Is there any method by which the start() function in my EA can be called as soon as a new hour begins on the clock?

 
SteveBrown:
Programming my first EA, I face the problem of how best to detect the start of a new hourly candle. There appears to be only one method, and that is to wait for the first tick to come along which falls into the next time period. That is because there appears to be only one way the start() function is called in my EA, and that is when the server pumps out a new tick. So, if a tick occurs at 01:59, and the next tick comes at 02:02, the EA detects the end of the 01:00 candle and the start of the 02:00 candle at 02:02, two minutes after the top of the hour. My EA is designed to run on the H1 chart, and it makes trading decisions based upon the close prices of hourly candles. I would like my EA to make those decisions at the top of the hour, rather than wait for the first tick that occurs in the new hour. Is there any method by which the start() function in my EA can be called as soon as a new hour begins on the clock?

No, you do not have any way to do that, but maybe you can write an endless script ...

But what should be the avantage to run the EA before the first tick ?

 
Michel:
No, you do not have any way to do that, but maybe you can write an endless script ... But what should be the avantage to run the EA before the first tick ?

Thanks for your reply. Any advantage I could hope to gain would be minimal, but since my EA makes the decision to open a trade based on the closing price of the hourly candle, I would prefer it to place the order at the close of the candle, instead of waiting for the next candle to begin. Even if the new candle is not immediately displayed at the top of the hour, the close price has already been established by the end of the time period.

Having programmed applications for Windows, I am accustomed to the ability of a program to receive a message from the OS or another program and to act upon it immediately, so I find it restrictive that my EA can only act when my start() function is called when a new tick occurs.

 
SteveBrown:
Programming my first EA, I face the problem of how best to detect the start of a new hourly candle. There appears to be only one method, and that is to wait for the first tick to come along which falls into the next time period. That is because there appears to be only one way the start() function is called in my EA, and that is when the server pumps out a new tick. So, if a tick occurs at 01:59, and the next tick comes at 02:02, the EA detects the end of the 01:00 candle and the start of the 02:00 candle at 02:02, two minutes after the top of the hour. My EA is designed to run on the H1 chart, and it makes trading decisions based upon the close prices of hourly candles. I would like my EA to make those decisions at the top of the hour, rather than wait for the first tick that occurs in the new hour. Is there any method by which the start() function in my EA can be called as soon as a new hour begins on the clock?

do you mean to detect the price of the new cande, or the time it occurs? as both of theese methods ARE do-able, and fairly simple.

 
Eaglehawk:
do you mean to detect the price of the new cande, or the time it occurs? as both of theese methods ARE do-able, and fairly simple.

Eaglehawk,

What I mean is, I want my start() function to be called at the exact top of the hour, even before the first tick comes in on the new candle.

 
SteveBrown:
Eaglehawk, What I mean is, I want my start() function to be called at the exact top of the hour, even before the first tick comes in on the new candle.

ok, here's what i would do, i would use the Minute() variable as you are trading on the hourly chart, this will allow you to make your ea's desicions at the exact top of the hour.

example

if (Minute() == 0 && /*rest of how you determine your action*/);
 
Eaglehawk:
ok, here's what i would do, i would use the Minute() variable as you are trading on the hourly chart, this will allow you to make your ea's desicions at the exact top of the hour.

example

if (Minute() == 0 && /*rest of how you determine your action*/);

What Minute() does is tell me the minute of the most recent tick. Are you saying that instead of returning after evaluating every tick, my start() function should go into a loop waiting for Minute()==0? If so, in that same loop it would have to continually check for a new price, but I don't think the price is actually updated until MT calls the start() function again with a new tick. As I understand from the documentation, the start() function cannot be called again until the previous call to the start() function returns.

I think what I really need is information as to what Windows message I can have a program send to MT4, to tell it to call the start() function in my EA. I can create a Windows program to monitor the time and send the message to MT4 at the top of the hour. In other words, I need to be able to simulate a tick at the top of the hour.

 
SteveBrown:
What Minute() does is tell me the minute of the most recent tick. Are you saying that instead of returning after evaluating every tick, my start() function should go into a loop waiting for Minute()==0? If so, in that same loop I would have to continually check for a new price, but I don't think the price is actually updated until MT calls the start() function again with a new tick. I think what I really need is information as to what Windows message I can have a program send to MT4, to tell it to call the start() function in my EA. I can create a Windows program to monitor the time and send the message to MT4 at the top of the hour. In other words, I need to be able to simulate a tick at the top of the hour.

no, no, no, not that confusing, minute of the hour, so if minute is equal to 0, it's not 10:01 or 9:59, it is 10:00, 0 minutes after the hour.

now if you want to recall the price of the market right as it opened, or opens, i would recommend using the variable "iOpen"

example

double openperiod iOpen(NULL, 0, 1);

the "openperiod" is just a quick example name. you may want to change the last "1" to "0" considering you mention you want to look at the "now" top of the hour, 0 meaning current bar, 1 meaning last complete bar, and so forth.

 
Eaglehawk:
no, no, no, not that confusing, minute of the hour, so if minute is equal to 0, it's not 10:01 or 9:59, it is 10:00, 0 minutes after the hour.

now if you want to recall the price of the market right as it opened, or opens, i would recommend using the variable "iOpen"

example

double openperiod iOpen(NULL, 0, 1);
the "openperiod" is just a quick example name. you may want to change the last "1" to "0" considering you mention you want to look at the "now" top of the hour, 0 meaning current bar, 1 meaning last complete bar, and so forth.

Eaglehawk, thanks for trying to help, but I don't think you understand that the start() function is normally called for every new tick. It processes the tick and then returns. It can't wait for Minute() to equal zero without going into a loop, and if it does that, it can't process additional ticks.

I do understand that Minute() reports the minute of the hour of the most recent tick.

 

And even if I could have start() wait for Minute() to indicate the start of a new candle, that does not remove the delay if the first tick occurs a minute or two after the top of the hour.

I think I'm going to have to resign myself to detecting the end of the hourly candle in the conventional way, by waiting for the first tick of the next hourly candle, even though that may occur a minute or two after the top of the hour.

 

would it mess up your strategy to start the new candle at minute = 59?

Reason: