Please critique what was wrong with this code

Post Reply
dudest
Trader
Posts: 1847
Joined: Tue May 08, 2012 2:37 pm

Please critique what was wrong with this code

Post by dudest »

Hallo coders,

I put the following code into an EA for the following reasons:
a) If day is Monday: only open new trades after 9am ( broker/Empty4 platform time)
b) If day is Friday: only open new trades before 3pm ( broker/Empty4 platform time)
c) If day is Tue, Wed, or Thur: open new trades any time

However, this last Fri (15th), the EA opened new trades after 3pm

What was wrong with the code below?

Thanks

Code: Select all

int DoW = TimeDay(TimeCurrent());
int currentHour = TimeHour(TimeCurrent());

if ( 1 < DoW < 5 ) // It's not Monday or Friday
{
  // open new trade
}   

if ( DoW == 1 && currentHour > 9 )  // It's Monday after Asian session
{
  // open new trade
}

if ( DoW == 5 && currentHour < 15 ) // It's Friday before NY session
{
  // open new trade
}
.
garyfritz

Re: Please critique what was wrong with this code

Post by garyfritz »

I suspect your problem is the "1 < DoW < 5" bit.

One never knows with MQL, but I suspect that's being evaluated as "(1 < DoW) < 5", and "(1 < DoW)" will evaluate to true or false, which have the integer values 1 and 0. Which is less than 5 so it's always true.

See if "1 < DoW && DoW < 5" works any better.
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Please critique what was wrong with this code

Post by dietcoke »

dudest wrote:Hallo coders,

I put the following code into an EA for the following reasons:
a) If day is Monday: only open new trades after 9am ( broker/Empty4 platform time)
b) If day is Friday: only open new trades before 3pm ( broker/Empty4 platform time)
c) If day is Tue, Wed, or Thur: open new trades any time

However, this last Fri (15th), the EA opened new trades after 3pm

What was wrong with the code below?

Thanks

Code: Select all

int DoW = TimeDay(TimeCurrent());

This is the wrong function. it returns the day of the month. (1-31). you should be using

Code: Select all

TimeDayOfWeek(TimeCurrent());
dudest
Trader
Posts: 1847
Joined: Tue May 08, 2012 2:37 pm

Re: Please critique what was wrong with this code

Post by dudest »

garyfritz wrote:I suspect your problem is the "1 < DoW < 5" bit.

One never knows with MQL, but I suspect that's being evaluated as "(1 < DoW) < 5", and "(1 < DoW)" will evaluate to true or false, which have the integer values 1 and 0. Which is less than 5 so it's always true.

See if "1 < DoW && DoW < 5" works any better.
Thanks Gary! Changed it to the form above just incase MQL is doing funny evaluation

Cheers!
dudest
Trader
Posts: 1847
Joined: Tue May 08, 2012 2:37 pm

Re: Please critique what was wrong with this code

Post by dudest »

dietcoke wrote:
This is the wrong function. it returns the day of the month. (1-31). you should be using

Code: Select all

TimeDayOfWeek(TimeCurrent());
Thanks dc!, you're absolutely right, Scite IDE confirms what you've said.

I've made the correction

Cheers!
Post Reply

Return to “Coders Hangout”