Hello All,
This is my first post in this forum. I’m very new with MQL4 and my c language skill is almost nothing
I try to make this simple code, but the result is confusing for me.
Here is my code :
int order_count = 0;
int start()
{
//----
int zero_hour, zero_minute;
zero_hour=Hour();
zero_minute=Minute();
if(zero_hour==0 && zero_minute>0) {order_count=0;}
if(zero_hour==0 && zero_minute==0 && order_count==0)
{
OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-20*Point,Ask+60*Point,"My Order",12345,0,Green);
order_count++;
}
//----
return(0);
}
what I want to do is when the time reach 00.00 then send order to buy.
I run it for 1 month ( 2007.01.01 to 2007.01.31) in M1 period. But why the code make 3 order send at 2007.01.04 00:00 ?
2007.04.11 07:54:24 2007.01.04 00:00 GMT00 GBPUSD,M1: open #4 buy 1.00 GBPUSD at 1.9513 sl: 1.9493 tp: 1.9573 ok
2007.04.11 07:54:24 2007.01.04 00:00 GMT00 GBPUSD,M1: open #3 buy 1.00 GBPUSD at 1.9509 sl: 1.9489 tp: 1.9569 ok
2007.04.11 07:54:24 2007.01.04 00:00 GMT00 GBPUSD,M1: open #2 buy 1.00 GBPUSD at 1.9510 sl: 1.9490 tp: 1.9570 ok
2007.04.11 07:54:23 2007.01.03 08:05 Tester: stop loss #1 at 1.9716 (1.9714 / 1.9718)
2007.04.11 07:54:23 2007.01.03 00:00 GMT00 GBPUSD,M1: open #1 buy 1.00 GBPUSD at 1.9736 sl: 1.9716 tp: 1.9796 ok
what’s wrong with my code?
ByeThe problem is that int start() is called with each tick, and there can be multiple calls during that period where minute == 0 so each call will fire an OrderSend until the time increments by one minute. Try using a simple counter as a limiter. Declare the counter as an EA scope variable above int start() so it will retain it's value and then get reset after the trigger minute. I edited your code above to show this.
Hello Hatardi,
I have had the same problem. This is the code I use to prevent this from happening.
string time=TimeToStr(TimeCurrent(),TIME_SECONDS:);
Print("Current time seconds is ",time);
Then you can decide exactly what time to execute an order.
if (time=="00:00:00")
OrderSend(Symbol(),OP_BUY,lotsize,Ask,slippage,Ask-intStopLoss*Point,Ask+intTakeProfit*Point,"Order",12345,0,Green);
You can use this to close orders as well at specific times. This works for a 24 hour period, you would have to access date functions for specific days. Hope this helps.
Hello Hatardi,
I have had the same problem. This is the code I use to prevent this from happening.
string time=TimeToStr(TimeCurrent(),TIME_SECONDS:);
Print("Current time seconds is ",time);
Then you can decide exactly what time to execute an order.
if (time=="00:00:00")
OrderSend(Symbol(),OP_BUY,lotsize,Ask,slippage,Ask-intStopLoss*Point,Ask+intTakeProfit*Point,"Order",12345,0,Green);
You can use this to close orders as well at specific times. This works for a 24 hour period, you would have to access date functions for specific days. Hope this helps.That will work as long as you get at least one tick at precisely "00:00:00", otherwise, int start() won't be called and your order won't be placed.
Hello Bluto,
I was wondering about that. I was originally using that with daily bars, I thought there was always a tick at the open of a bar. Is this the case? If so couldn't you set the time for the start of any bar? Such as "01:00:00" for hourly bars, or "01:45:00" for 15 min bars?
Hello Bluto, I was wondering about that. I was originally using that with daily bars, I thought there was always a tick at the open of a bar. Is this the case? If so couldn't you set the time for the start of any bar? Such as "01:00:00" for hourly bars, or "01:45:00" for 15 min bars?
I'm not sure about that. If you take a glance at the history data, you'll see cases where an entire minute's worth of data is missing causing a gap...assumedly due to inactivity. I wouldn't trust or depend on broker data feeds to be reliable if I were programming such a precise time driven event as you are. There's lots of ways to skin the cat and do what you're trying to do, but to depend on a tick occuring at a precise time down to the second could be unreliable.
This kind of coding has worked for me in backtesting, however I know backtesting is highly unreliable. I've been working on EA's that rely only on price movements rather than indicators. ALL indicators seem to lag too much to catch
good trades. A lot of the strategies I am testing involve holding a position for an entire bar. I was using this time method of programming to enter and exit trades.
I have no experience with a live account yet, so maybe I do need to find another way to skin the cat. Thanks for your input.
This kind of coding has worked for me in backtesting, however I know backtesting is highly unreliable. I've been working on EA's that rely only on price movements rather than indicators. ALL indicators seem to lag too much to catch
good trades. A lot of the strategies I am testing involve holding a position for an entire bar. I was using this time method of programming to enter and exit trades.
I have no experience with a live account yet, so maybe I do need to find another way to skin the cat. Thanks for your input.Keep at it wolfe. You'll be coding up Super-EA's before you know it.
Woow ... thank you very much bluto and wolfe
i have tried the code and it works
Hi bluto,
what you said is right. We can not depend on a tick data for my code..
for example there is no order for 2007.01.05 because there is no zero_minute, only zero_hour.
is there any other way to fix my code?
Hii Hartady,
Trying to download another people EA's, and learn it from there.
Or u can search ea from www.mql4.com
Johan Kancil
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use