How best to skip code on if thn clause.

 

Hi

I have an EA where i want to tell the code (including the Strategy Tester, if that acts differently) to skip a test if a condition is not met.

I am new to MQL4 so i chaecked the web and found i could possibly use "break" is this correct and if so would strategy tester stop or just ignore one test in this circumstance?

Thanks


Paxman

 

You use break to exit a loop when there is no need to continue.

Show your code and you may get some good advice.

 
Keith Watford:

You use break to exit a loop when there is no need to continue.

Show your code and you may get some good advice.

The code before the functions is 4000 lines long so not sure what i would need to show to enable someone to help.

Init is at line 592 after all the variables and inputs etc. The end of the basic code is at line 4061.

In VBA it is simple but the logic in mql4 is very different.

I suspect it is not as simple as telling the code execution to move to a certain place like a "goto" so perhaps i am stuck?

Perhaps there is a simple web page to look at that explains the overall way the code works that might cover what i need?

The EA was written a few years back (by paid-for coders who no longer exist) and i am just trying to add a bit of functionality.

 

I think that an EA with 4000+ lines of code is probably a bit too difficult to modify for anyone with only basic understanding of how code works.

Here is a basic example of using if()

   if(condition_1)  //if condition_1 is not true, 2 and 3 will be skipped
      if(condition_2)  //if condition_2 is not true, 3 will be skipped
         if(condition_3)
           {
            //do what is necessary as all 3 conditions are satisfied.
           }

Or if the conditions are in functions that return bools

   if(condition_1() && condition_2() && condition_3())
     {
      //do what is necessary as all 3 conditions are satisfied.
     }
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                                                            |...
 
Paxman: i want to tell the code …
  1. MT4: Learn to code it.
    MT5: Learn to code. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
  2. or pay (Freelance) someone to code it.
              Hiring to write script - General - MQL5 programming forum
 

I like using a while loop for this, especially if, say, one negative would break the loop.


bool trade=false;
while(!trade){
if(!condition1)break;
if(!condition2)break;

// all conditions met so true
trade=true;
// dont forget this last break; or endless loop!
// (although I just realised you don't need it, but good practise to
// do so...)
break;
}
 
Paxman:

Hi

I have an EA where i want to tell the code (including the Strategy Tester, if that acts differently) to skip a test if a condition is not met.

I am new to MQL4 so i chaecked the web and found i could possibly use "break" is this correct and if so would strategy tester stop or just ignore one test in this circumstance?

Thanks


Paxman

i assume the 4000 lines is a loop .
i assume the "test" is one loop execution
i assume you dont want the rest of the loop to stop but each "test" to depend on a (or more) conditions
if the above stands , then when your conditions for not performing a test are valid use continue; ,it will not break the loop
and it will jump to the next "test" . ;)

 
Keith Watford:

I think that an EA with 4000+ lines of code is probably a bit too difficult to modify for anyone with only basic understanding of how code works.

Here is a basic example of using if()

Or if the conditions are in functions that return bools

So looking at all the replies i can see the basic way they work.

So to try something simple I tried inserting an if immediately after the int start(). The EndHr and StartHr are integers.

I assumed that in the Strategy tester it would not show results when the EndHr was smaller than the StartHr but it does?

It compiles ok BTW


int start()


{    if(EndHr>StartHr)

Rest of the code...


Have i missed something obvious?

 

When you come across invalid settings you should exit the test run as early as possible, during initialization. This will avoid unnecessary cluttering of the test results.

  • use void OnTick() instead of int start()
  • use int OnInit() instead of int init()
  • add this to your OnInit:
    if(60*EndHr+EndMn<=60*StartHr+StartMn)
      {
       Print("invalid trading time");
       return INIT_PARAMETERS_INCORRECT;
      }
 
lippmaje:

When you come across invalid settings you should exit the test run as early as possible, during initialization. This will avoid unnecessary cluttering of the test results.

  • use void OnTick() instead of int start()
  • use int OnInit() instead of int init()
  • add this to your OnInit:

Ok tried this but i get

'deinit' - function declarations are allowed on global, namespace or class scope only    GridTrader_v14.37.mq4    1113    5

'OnTick' - function declarations are allowed on global, namespace or class scope only    GridTrader_v14.37.mq4    1130    6

Also had to put a semi colon here void OnTick();

Not sure what this means perhaps i need to make changes elsewhere once i have added your code?

 

I tried your code on its own without using the new ontick call and it works so i need to look at the way the new call should be handled in code.

I notice the code still allows a start and finish time that are equal.

So if i wanted to use code that allowed a minimum of 15 mins of trading time i would use   60*EndHr+EndMn<=60*StartHr+(StartMn+15) ?