Newbie error. Please Help!

 

Part of my EA looks like this.

int start()
{
   static datetime t;if(t!=Time[0])
   {
      t=Time[0];
      
      //My code goes here...

      if(Volume[0]>1){Print(" Invalid tick volume. ");return;}

      //My code goes here...

      return(0);
   }
   return(0);
}
If I run it as it is, IT DOES NOTHING.

However if I remove the datetime t if clause completely, I get the message " Invalid tick volume. "

What am I doing wrong?

-ssn
 
ssn wrote >>

Part of my EA looks like this.

If I run it as it is, IT DOES NOTHING.

However if I remove the datetime t if clause completely, I get the message " Invalid tick volume. "

What am I doing wrong?

-ssn


I have tried changing my source to this but I still get the same error message...
int start()
{
   static datetime t;
   if(t!=Time[0]){t=Time[0];Sleep(3000);start();}

   //Code...

   if(Volume[0]>1){Print(" Invalid tick volume. ");return;}

   //Code...

    return(0);
}
What am I missing
-ssn
 

How are you testing this?

First off, if I remove the datetime clause, the code does not compile.

Anyway, what is happening is that on the first run of the script, the variable t is initialised as 0, which the takes you into the if loop. Since this is the first iteration of the loop, the volume is only 1 which does not trigger your "Invalid Volume" message, since you have told it to look for volume > 1. Try looking for volume > 0 and it should work. Essensially you will have to wait for the next bar to open before you get the message.

Also, you cannot call the start function from within itself. In fact I don't think you can call it ever, as it is a special MT4 reserved function.

Here's another tip......try placing Print statments in the code to try and follow the program flow and allow you to inspect the variables.

 

Don't put if statements in the middle of code.
If you are new to programming dont compound your statement on one line ; x=0 ; y=0 ; if(x=0){Print("hi");};y=1;
is unreadable

In the second code you wrote you do a call to start() so the code would never reach your if(Volume[0]>1) statement.
the return statement after the Print is not needed

My personal preference is to define static variables globally that is before the init() statement

 
volume does not have to be one at the start of a new bar and does not necessary increment by one each tick (sometimes it won't change at all.)
My personal preference is to define static variables when the variable is used only within a routine and globally when it's used in multiple functions.
 
kennyhubbard wrote >>

How are you testing this?

First off, if I remove the datetime clause, the code does not compile.

Anyway, what is happening is that on the first run of the script, the variable t is initialised as 0, which the takes you into the if loop. Since this is the first iteration of the loop, the volume is only 1 which does not trigger your "Invalid Volume" message, since you have told it to look for volume > 1. Try looking for volume > 0 and it should work. Essensially you will have to wait for the next bar to open before you get the message.

Also, you cannot call the start function from within itself. In fact I don't think you can call it ever, as it is a special MT4 reserved function.

Here's another tip......try placing Print statments in the code to try and follow the program flow and allow you to inspect the variables.


Hey thanks for the feedback. I was actually able to resolve this. Apparently the cause of this error was from the if-clauses before my ordersend function!! I had to normalize the doubles and also ensure I was comparing doubles to doubles etc...

Regarding your feedback, if I atually omitted the Volume[0]>1 if-clause from my code, the EA would still not run! I have not tried Volume[0]>0 though...
 
Ickyrus wrote >>

Don't put if statements in the middle of code.
If you are new to programming dont compound your statement on one line ; x=0 ; y=0 ; if(x=0){Print("hi");};y=1;
is unreadable

In the second code you wrote you do a call to start() so the code would never reach your if(Volume[0]>1) statement.
the return statement after the Print is not needed

My personal preference is to define static variables globally that is before the init() statement


Hi... actually my code did reach the 'if(Volume[0]>1)' statement... and returned the invalid volume error
 
WHRoeder wrote >>
volume does not have to be one at the start of a new bar and does not necessary increment by one each tick (sometimes it won't change at all.)
My personal preference is to define static variables when the variable is used only within a routine and globally when it's used in multiple functions.


Hi am using t only once in the start function i.e. whenever a new bar is formed. Do I need to define it outside this function or would this be sufficient? Thanks