Init() starting program logic.

 

Hi i am after some simple help in coding some starting logic in MQl4

I am trying to ensure that if a value is set to true then a bit of code runs to exclude certain values from being run through the EA code.

If it is not set to tru then the normal program execution would take place.

My problem is that i can not get the code to ignore the code if the value is false.

It may just be a bracketing problem.

Can anyone suggest how to fix it?

Thanks

Neil


int init()
{
  {if(UseTimeSlot = True)
     {
         if((60*StartHr)+StartMn+NumberOfMins!=(60*EndHr)+EndMn)    //extern bool UseTimeSlot = false;
        {
         //Print("invalid trading time")
         return INIT_PARAMETERS_INCORRECT;
        }
      }
   }
   Rest of code
 

Be sure that you are using #property strict and declare init function as OnInit() instead of init().

 
Fabio Cavalloni:

Be sure that you are using #property strict and declare init function as OnInit() instead of init().

The code works perfectly as it is without my addition of the if statement so i need to know what is wrong with the if statement.

Would either of your suggestions fix that? I am no expert but I am not sure they would.

Thanks

Neil

 

What I wrote are initial tasks that need to be done at the beginning and on all projects.

Your if statement is coded to avoid the EA to be initialized when that specific condition is met

((60*StartHr)+StartMn+NumberOfMins!=(60*EndHr)+EndMn)

We don't know anything about your code and your wanted behavior.

 
Fabio Cavalloni:

What I wrote are initial tasks that need to be done at the beginning and on all projects.

Your if statement is coded to avoid the EA to be initialized when that specific condition is met

((60*StartHr)+StartMn+NumberOfMins!=(60*EndHr)+EndMn)

We don't know anything about your code and your wanted behavior.

The code is not mine but i am trying to add some functionality. The EA was coded by experts years ago and i am just trying to help the guy who is running it add an ability to test timeslots in the trading day.

The wanted behaviour is to have the main code run if the   if(UseTimeSlot = False). If the if(UseTimeSlot = True) and the condition of the if statement is met it should "Return" but if the if statement is not met the code should run.

I hope that makes sense.

in effect it is comparing a start and end time. If the difference in minutes is equal to a variable that holds the length of time to trade it should run and reject those that do not. You can then run strategy tester and it will give results of a timeslot moving through the day. It currently does that bit but when you set the UseTimeSlot to false it still seems to access the if statement due to a mistake in this bit of code.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 

What you need is a time filter function on your EA.

You don't have to put it into OnInit function but OnTick, avoiding the EA to open new trades if the time "is not good".

 
Fabio Cavalloni:

What you need is a time filter function on your EA.

You don't have to put it into OnInit function but OnTick, avoiding the EA to open new trades if the time "is not good".

That may be true. But the timeslot testing is only done using the Strategy tester. When you have the best timeslot you set the UseTimeSlot to False and the EA runs with that time (or should do but will not ignore the if statement currently).

That is why i chose the On Init. Was i wrong to try it this way?

Can you point me in the direction of a time function if you can not spot my coding error please.

Thanks.

 
Fabio Cavalloni:

What I wrote are initial tasks that need to be done at the beginning and on all projects.

Your if statement is coded to avoid the EA to be initialized when that specific condition is met

((60*StartHr)+StartMn+NumberOfMins!=(60*EndHr)+EndMn)

We don't know anything about your code and your wanted behavior.

It works to allow the timeslot testing in the Strategy Tester but if i set the UseTimeSlot to False it still uses the NumberOfMinutes in the testing.

I tried the below which again works in the Strategy tester to allow timeslot testing but still manages somehow to use the NumberOfMinutes in the testing with UseTimeSlot set to False

I am obviously missing something. I am completely baffled. Perhaps this bit of code needs to be somewhere else?

int init()
{
   if(UseTimeSlot = True) // if False then miss out this code and run the Values as entered in the Strategy Tester.
     {
         if((60*StartHr)+StartMn+NumberOfMins==(60*EndHr)+EndMn);    // this is the only place in the code that has the variable NumberOf Minutes
         else // the values do not match so get next value to test
         {
          return;// INIT_PARAMETERS_INCORRECT;
         }
       }

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
Reason: