-1

I would like to use Meta Trader 4 Expert Advisor to code a trading strategy.

However, I could not find such function in MT4.

A similar function in Python would be datetime.datetime(year, month, day, hour, minute, second).

Essentially I want to do the following:

Place sell and buy limit when market opens, say UTC +8.

It would be good if someone can help me in this.

Idonknow
  • 177
  • 1
  • 5
  • 13

2 Answers2

1

MT4 time is number of seconds past sinse Jan 1, 1970, a special variable type datetime is used, which is in fact a simple int. How to constuct time? Two simple ways: from string and from struct.

datetime time=StringToTime(StringFormat("%04d.%02d.%02d %02d:%02d",year,month,day,hour,minute));

Alternatively,

MqlDatetime dt;
dt.year=year;
dt.mon=month;
dt.day=day;
dt.hour=hour;
dt.min=minute;
datetime time=StructToTime(dt);

What time is available in MT4? Three types of time can be called: current time of the broker (what you actually see on the chart and in the market window) is the default time, you can get it by calling TimeCurrent() or iTime(_Symbol,PERIOD_M1,0); GMT+0 can be achieved by TimeGMT(); and your local PC time can be achieved via TimeLocal() function. Which of them to use - it is up to you.

Placing order by time condition is similar to placing an order with other conditions.

if(condition)OrderSend(..);
Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
1

In addition to Daniel's answer, the way I use time is simply using Hour(), Minute(), and Seconds().

Note that these return broker server times.

So use case:

if(Hour()==14 && Minute()==30 && Seconds()==0){
    int buy = OrderSend(...);
}
Dean
  • 2,326
  • 3
  • 13
  • 32
  • Actually I have quite a lot of trouble using MT4 editor. If possible, can you provide a sample code that will allow me to place order at 12am at broker time? Thanks in advance. I know basic syntax in MT4 but quite clueless on which section to place order. – Idonknow Oct 04 '19 at 14:53
  • 1
    You can use the above. Change the `14` to `12` and `30` to `00`. And that will allow for execution at `12:00`. – Dean Oct 04 '19 at 14:58