Does anyone have any example code for how to generate a time of day based entry signal in Metatrader 4? e.g. at a particular hour and minute of each day
-
Is there a certain part of this you are looking for (e.g. logic to tell what time it is, logic to initiate a trade, etc)? Or are you looking for all of it? – malonso Jan 29 '10 at 23:04
-
Just looking for the methodology. I am looking to use it to open trades at a certain time if meeting additional criteria. – acheo Jan 30 '10 at 16:11
2 Answers
TimeLocal()
gives you the number of seconds since midnight Jan 1st 1970 of your local computer (Terminal Client).
TimeCurrent()
gives you the number of seconds since midnight Jan 1st 1970 of your Broker's computer (Server).
You can convert either of these into a string like so:
string ct = TimeToStr(TimeLocal(),TIME_DATE|TIME_SECONDS);
Print("Client Time: ", ct);
string st = TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);
Print("Server Time: ", st);
You can also get individual elements of the date number like so:
Print("Year:",TimeYear(TimeCurrent())," Month:",TimeMonth(TimeCurrent()));
The MQL4 online programmers guide can be found here.
Hope this helps.
Cheers,
Mark
I've been working on just such a function. It has to be customized a lot—I'm working on making a more general version that;s going to be so big and tedious it'll need its own include
file. But it'll be worth it to be able to stick it on different charts without changing the code. It will basically be one big case statement with what I have here customized already to each individual timeframe.
What have you been working on? Thanks for your interest in time and forex!
bool existordertime( datetime time, int otype = -37 ) {
// +---------------------------------------------------------------+
// | this function is intended for use inside of if() and other conditionals
// | usually called with TimeCurrent() example:
// |
// | if ( !existordertime( TimeCurrent() ) )
// |
// | it accepts a datetime. A datetime is:
// | a number of seconds elapsed from 00:00 January 1, 1970
// | they can be treated as integers as such or accessed with other functions
// | so that if statements can be commented in and out easily based on what
// | timeframe we plan on looking at.
// | there is an optional parameter for an order type if you need it.
// |
// | KEEP IN MIND if you want to use this to trade something like a 5min 15min
// | or 4hr your gonna need a lot of if statements like:
// |
// | if ( MathMod( Minute() + 5, 5 ) == 0 )
// |
// +------------------------------------------------------------------+
for (int cnt = 0; cnt < OrdersTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == otype || -37 == otype)
// COMMENT OUT THE if()S YOU DON'T NEED HERE:
// also add new lines as needed
// if ( MathMod( TimeMinute( time ) + 5, 5 ) == 0 ) // 5min chart
// if ( MathMod( TimeMinute( time ) + 15, 15 ) == 0 ) // 15min chart
// if ( MathMod( TimeMinute( time ) + 30, 30 ) == 0 ) // 30min chart
// if ( MathMod( TimeHour( time ) + 4, 4 ) == 0 ) // 4hour chart
int dbOrderOpenTime = OrderOpenTime(); // re-use SAVEs dbPOOL-access time ...
if ( TimeSeconds( time ) == TimeSeconds( dbOrderOpenTime ) )
if ( TimeMinute( time ) == TimeMinute( dbOrderOpenTime ) )
if ( TimeHour( time ) == TimeHour( dbOrderOpenTime ) )
if ( TimeDay( time ) == TimeDay( dbOrderOpenTime ) )
if ( TimeMonth( time ) == TimeMonth( dbOrderOpenTime ) )
if ( TimeYear( time ) == TimeYear( dbOrderOpenTime ) )
return (TRUE);
}
for (cnt = 0; cnt < OrdersHistoryTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
if (OrderType() == otype || -37 == otype)
// COMMENT OUT THE if()S YOU DON'T NEED HERE:
// also add new lines as needed
// if ( MathMod( TimeMinute( time ) + 5, 5 ) == 0 ) // 5min chart
// if ( MathMod( TimeMinute( time ) + 15, 15 ) == 0 ) // 15min chart
// if ( MathMod( TimeMinute( time ) + 30, 30 ) == 0 ) // 30min chart
// if ( MathMod( TimeHour( time ) + 4, 4 ) == 0 ) // 4hour chart
int dbOrderOpenTime = OrderOpenTime(); // re-use SAVEs dbPOOL-access time ...
if ( TimeSeconds( time ) == TimeSeconds( dbOrderOpenTime ) )
if ( TimeMinute( time ) == TimeMinute( dbOrderOpenTime ) )
if ( TimeHour( time ) == TimeHour( dbOrderOpenTime ) )
if ( TimeDay( time ) == TimeDay( dbOrderOpenTime ) )
if ( TimeMonth( time ) == TimeMonth( dbOrderOpenTime ) )
if ( TimeYear( time ) == TimeYear( dbOrderOpenTime ) )
return (TRUE);
}
return (FALSE);
}

- 1
- 6
- 50
- 92

- 66
- 3
-
Your code does not look like code. Moreover, it is not editable by humans. Could you please help by indenting it by four spaces throughout? It will increase the readability – eeerahul Dec 09 '11 at 06:00
-
@eeerahul yes, right. MQL4 community has a very strange habit, to produce MQL-code ( C-syntax ) as if they had to pay a dollar for each char on it ... This habit is IMO non-reformable.The last time 've seen anything like that was in the age of 360KB floppy disks, where big LISP-based software modules were literally stripped down to keep only syntactically "necessary" char-s, and the code then looked prettysimilarly hash-code-d due to a need to fit into a limitted storage capacity of FDD media. (Kids would hardly believe that some PCs did not have HDDs and booted + ran from FDDs ... well...) – user3666197 Jun 14 '14 at 18:50
-
@nbrooks after your several re-edits of the code-block, kindly take into account, there was initially an intention to left the authors approach to the algoritmisation untouched. As you modify both the formatting and the logic, you may be informed, there are much better algorithmisations for operations on different timeframes via `Time[iShift(...)]` and other principal items, that go beyond the not so sharp definition of the question to be answered. The code-block does nothing in a direction of the initial question, but rather extensively re-reads dbPOOL (taking no care of `.Select()` result) – user3666197 Jun 14 '14 at 19:13
-
@user3666197 I'm not sure what you're saying. The only things I changed were the formatting and the grammar, both of which were still unreadable after your initial edit. I made no changes to the operations, or order of invocation. Also, that strange spacing you've added inside the `if` statements is not standard in any style-guide or IDE I've ever seen—it also forces a side-scroll to see all the code. Anyway, it's just spacing and has no effect on execution, so I'm not sure what you're getting at. – nbrooks Jun 14 '14 at 19:54
-
@nbrooks Do not worry, shocked me that right once've saved (format) edits, you have deleted/damaged that efforts. Not much new to invent here. The Metatrader4/MQL4 community is by vast majority of the installed base not a professional C#/C++ elite-group, so clarity of concept weights more, than C-veteran instincts. As you object the IDE/style-guide, needless to say, that even professional circles do not share opinions on this -- just take MISRA-C requirements for automotive industry, vs. opposite priorities from hard Real-Time kernel/OS industry practice. **Ease of MQL4-guy readability rules** – user3666197 Jun 14 '14 at 21:40
-
@nbrooks ... and finally - objecting "*... forces a side-scroll to see all the code.*" **shall IMO rather be addressed to** the **StackOverflow** portal owner, because locking the "valued"-content to LESS THAN ONE THIRD of the screen-layout resource ( with the objected side-effect of scrolling behind the layout border ) in the age of post-FullHD resolution has nothing to do with code per se, but with the presentation layer of this site. **UX counts**, doesn't it? – user3666197 Jun 14 '14 at 22:36