• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 4:55pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 4:55pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

5 Seconds " L " 291 replies

Please add alert to Parabolic Sar 2TF - Please Help! 0 replies

Please help with "int start()" structure - I want to process bars left to right 5 replies

please help me a few seconds! mq4 experts 2 replies

Need Help: Please add an alert to LSAR 3 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
Tags: Need to add "seconds" to start time for EA. Please help!
Cancel

Need to add "seconds" to start time for EA. Please help!

  • Post #1
  • Quote
  • First Post: Jun 5, 2014 12:38pm Jun 5, 2014 12:38pm
  •  Thepipster
  • | Joined May 2012 | Status: Working the mine. | 404 Posts
I have an EA that starts at the same time every day at my designated hour and minute, but I want to further define the start time down to the second...can somebody post some sample code of how I would do this? It needs to be able to be changed from settings. I'm guessing I can copy most of the code already used for the hour and minute part, but I don't know how to add the seconds. Please help???
Too close for pips, switching to points.
  • Post #2
  • Quote
  • Jun 5, 2014 7:45pm Jun 5, 2014 7:45pm
  •  fxdaytrader_
  • Joined Jan 2011 | Status: UberTroll | 1,847 Posts
same as the hours-stuff, e.g. http://docs.mql4.com/dateandtime/timeseconds
PM me with coding requests and I'll probably put you on my ignore list
 
 
  • Post #3
  • Quote
  • Jun 6, 2014 4:21pm Jun 6, 2014 4:21pm
  •  Thepipster
  • | Joined May 2012 | Status: Working the mine. | 404 Posts
here is a sample i robbed off somebody's posted ea...

extern int StartHour = 12;
extern int StartMinute = 30;
extern int StartSecond = 00;
int Magic=10000000000000;

//Start Coding---------------------------------------------------------

int start()
{
if (
OrdersTotal()==0)
{
if (
Hour()==StartHour)
{
if (
Minute()>=StartMinute)
{
if (
Seconds()>=StartSecond)





does this look right? why is there a ">" before the =startminute and =startsecond, but not before starthour?
does the ">" matter?

Please tell me that all I have to do is add:

extern int StartSecond = 00;

under the other "int"things, and

{
if (Seconds()>=StartSecond)

under the "//start coding//" line, under the minute command thingy???

thanks.
Too close for pips, switching to points.
 
 
  • Post #4
  • Quote
  • Edited 5:02pm Jun 6, 2014 4:48pm | Edited 5:02pm
  •  rockit
  • Joined Oct 2013 | Status: Member | 921 Posts
The ckeck for greater-or-equal is because EAs have been tick driven only, not time. So, the EA could only check the time when it has been "ticked". And it is unlikely that no tick would occur within a hour, therefore no greater-or-equal check against the hour value.

But with the 600+ builds a new mechanism has been introduced: a timer. Now EAs can be run by the timer event, too, i.e. by the second.

The code you presented could prove a bit tricky, though.. (in certain circumstances of wrap-around before next tick)
..
 
 
  • Post #5
  • Quote
  • Jun 6, 2014 4:59pm Jun 6, 2014 4:59pm
  •  Thepipster
  • | Joined May 2012 | Status: Working the mine. | 404 Posts
Quoting rockit
Disliked
The ckeck for greater-or-equal is because EAs have been tick driven only, not time. So, the EA could only check the time when it has been "ticked". And it is unlikely that no tick would occur within a hour, therefore no greater-or-equal check against the hour value. But with the 600+ builds a new mechanism has been introduced: a timer. Now EAs can be run by the timer event, too, i.e. by the second. The code you presented could prove a bit tricky, though..
Ignored
Thanks for the info, that makes sense about the ticks...what do you mean by tricky? My EA starts trading at the same time every day and has a stop time, also, but I don't need seconds for stopping. It works great as is, but price whipsaws during that last minute.
Too close for pips, switching to points.
 
 
  • Post #6
  • Quote
  • Edited 5:35pm Jun 6, 2014 5:16pm | Edited 5:35pm
  •  rockit
  • Joined Oct 2013 | Status: Member | 921 Posts
"Tricky" in this context means: For example if you were to define StartSecond = 30, and last tick had occured at 29, and next tick would occur 35 seconds later, then the check for >= 30 seconds would fail because of the wrap-around (29 + 35 = 4).

Here a code for builds 600+

Inserted Code
 
#property strict
input int StartHour = 12;
input int StartMinute = 30;
input int StartSecond = 00;
bool runyesno = false;
int OnInit()
{
    EventSetTimer(1); // set event every second
    return 0;
}
void OnTick()
{
    if(runyesno) {
        // do your stuff here
    }
}
void OnTimer()
{
    if(!runyesno)
        if(Hour() == StartHour)
            if(Minute() == StartMinute)
                if(Seconds() >= StartSecond) {
                    runyesno = true;
                    EventKillTimer(); // do not set event anymore
                    OnTick();
                }
}
..
 
 
  • Post #7
  • Quote
  • Jun 6, 2014 5:32pm Jun 6, 2014 5:32pm
  •  Thepipster
  • | Joined May 2012 | Status: Working the mine. | 404 Posts
Thanks again for your reply...now, do you think it's a problem if all I'm having the EA do is place pending orders?
Too close for pips, switching to points.
 
 
  • Post #8
  • Quote
  • Jun 6, 2014 5:47pm Jun 6, 2014 5:47pm
  •  rockit
  • Joined Oct 2013 | Status: Member | 921 Posts
It does not matter what the EA does. If you do not use a seconds timer you cannot start by the second, but by next tick that is past your mark. And that could fail under certain circumstances. If StartSeconds is always 0 then the EA will start on next tick for sure, however in this case you do not need to check for the second anyway, minute will do.
..
 
 
  • Post #9
  • Quote
  • Jun 6, 2014 5:54pm Jun 6, 2014 5:54pm
  •  Thepipster
  • | Joined May 2012 | Status: Working the mine. | 404 Posts
Thanks a ton, again, rockit, that helps me a lot!! Green pips to you!
Too close for pips, switching to points.
 
 
  • Post #10
  • Quote
  • Jun 7, 2014 12:42am Jun 7, 2014 12:42am
  •  cja
  • Joined Feb 2007 | Status: Member | 2,002 Posts
There are 2 timer setups

EventSetTimer( 1 ); //refresh above 1 second
EventSetMillisecondTimer( 100 ); // refresh below 1 second
Trade what you see not what you hope
 
1
  • Post #11
  • Quote
  • Jun 19, 2014 4:05pm Jun 19, 2014 4:05pm
  •  Thepipster
  • | Joined May 2012 | Status: Working the mine. | 404 Posts
I have tried seeing how to insert this timer into my existing code, but all these brackets get me confused, I don't know where to put OnTick and other functions in relationship to the rest of the code, so here's a sample of my code so you can see the context of it...

input int StartingHour = 12;
input int StartingMin = 29;
input int StartingSecond = 52;
input int StoppingHour = 12;
input int StoppingMin = 30;
double CurrentPL;
string Pair;
double StartingEquity, LS;
double EquityGain, DesiredEquity, MaxLoss;
bool StartTrade = false;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
GlobalVariablesDeleteAll();
Pair = Symbol();
return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}

\\\stuff about start function here in between

CheckTime();
return(0);
}

//+------------------------------------------------------------------+
//| Check Time function |
//+------------------------------------------------------------------+
void CheckTime()
{
if(DayOfWeek()>=4 && DayOfWeek()<=4)
{
if(Hour() == StartingHour && Minute() == StartingMin && Seconds() >= StartingSecond)
StartTrade=true;
}
if(Hour() == StoppingHour && Minute() == StoppingMin)
StartTrade=false;

if(StartTrade)
CheckForOpenOrders();
}





\\\thanks
Too close for pips, switching to points.
 
 
  • Post #12
  • Quote
  • Jun 20, 2014 9:29am Jun 20, 2014 9:29am
  •  Thepipster
  • | Joined May 2012 | Status: Working the mine. | 404 Posts
do I just put the suggested code between these lines, or do I need to insert them into the existing "sections" ?
Too close for pips, switching to points.
 
 
  • Post #13
  • Quote
  • Jun 20, 2014 10:09am Jun 20, 2014 10:09am
  •  rockit
  • Joined Oct 2013 | Status: Member | 921 Posts
What is so confusing about braces? Braces denote scope e.g. what code belongs to what group of (other) code.

Inserted Code
#property strict
 
input int StartingHour = 12;
input int StartingMin = 29;
input int StartingSecond = 52;
input int StoppingHour = 12;
input int StoppingMin = 30;
double CurrentPL;
string Pair;
double StartingEquity, LS;
double EquityGain, DesiredEquity, MaxLoss;
bool StartTrade = false;
 
int OnInit()
{
  GlobalVariablesDeleteAll();
  Pair = Symbol();
  return(0);
}
 
void OnTick()
{
  CheckTime();
  if(StartTrade) {
    //stuff about start function here in between
  }
}
 
void CheckTime()
{
  if(DayOfWeek()>=4 && DayOfWeek()<=4) {
    if(Hour() == StartingHour && Minute() == StartingMin && Seconds() >= StartingSecond)
    StartTrade=true;
  }
  if(Hour() == StoppingHour && Minute() == StoppingMin)
    StartTrade=false;
  if(StartTrade)
    CheckForOpenOrders();
}
..
 
 
  • Post #14
  • Quote
  • Jun 20, 2014 10:29am Jun 20, 2014 10:29am
  •  Thepipster
  • | Joined May 2012 | Status: Working the mine. | 404 Posts
thanks...do I need to put the eventsettimer, and EventKillTimer somewhere, too?
Too close for pips, switching to points.
 
 
  • Post #15
  • Quote
  • Jun 20, 2014 10:58am Jun 20, 2014 10:58am
  •  rockit
  • Joined Oct 2013 | Status: Member | 921 Posts
This code works without a Timer function, so, no. Because in close proximity to high impact news there will frequently come in ticks, so that, in fact, it is unlikely you will miss it : )
..
 
 
  • Post #16
  • Quote
  • Last Post: Edited 12:08pm Jun 20, 2014 11:42am | Edited 12:08pm
  •  rockit
  • Joined Oct 2013 | Status: Member | 921 Posts
Here's a Timer demo code (EA). Put in your local start/stop times into the respective strings in standard notation hh:mm:ss.

[Note: It will print a message every second from start to stop time]

Inserted Code
#property strict
 
input string starttime = "18:37:09"; // start at time
input string stoptime = "18:37:19"; // stop at time
 
datetime startm;
datetime stoptm;
 
int OnInit()
{
  startm=StrToTime(starttime);
  stoptm=StrToTime(stoptime);
  EventSetTimer(1);
  return(INIT_SUCCEEDED);
}
 
void OnDeinit(const int reason)
{
  EventKillTimer();
}
 
void OnTick()
{
  // tick code here
}
 
void OnTimer()
{
  if(TimeLocal()>=startm && TimeLocal()<stoptm)
    Print("we are the things that were and shall be again");
}
..
 
 
  • Platform Tech
  • /
  • Need to add "seconds" to start time for EA. Please help!
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023