Freelancers needed to develop Expert Advisors for traders

init() wait until something... and then continue with start()

Back to topics list To post a new topic, please log in or register
avatar
11
moneysmile 2011.05.19 20:26
 

Hi,

is it possible in MT4 in init() function of EA to do something like this... and how?

Example:

1. Price=Bid

2. ticket1=OrderSend( Price+0.005, OP_BUYSTOP)
ticket2=OrderSend( Price+0.005, OP_SELLLIMIT)
ticket3=OrderSend( Price-0.005, OP_BUYSTOP)
ticket4=OrderSend( Price-0.005, OP_SELLLIMIT)

3. wait until the "case A" {Bid >= Price+0.005} or "case B" { Bid <= Price-0.005}

4. and after that "case A" {OrderDelete (ticket3 and ticket4)} or "case B" {OrderDelete (ticket1 and ticket2)}

5. and then continue with start() function

 
CryproCurrency News - Californian Regulators: “Coinbase is not regulated or licensed”
The California Department of Business Oversight (DBO) recently issued a press statement where it denied offering Coinbase any kind of business license status to operate in California. The press release was in response to Coinbase website which openly proclaims itself to have been offering bitcoin trading services in California.
avatar
11
moneysmile 2011.05.19 20:29 #
 

correction for number

2. ticket1=OrderSend( Price+0.005, OP_BUYSTOP)
ticket2=OrderSend( Price+0.005, OP_SELLLIMIT)

ticket3=OrderSend( Price-0.005, OP_BUYLIMIT)
ticket4=OrderSend( Price-0.005, OP_SELLSTOP)
 
avatar
893
7bit 2011.05.19 21:18 #
 

The programming is event based: You don't wait and then continue, instead in every run of the start function (which will happen with every tick) you check your conditions and then either do something or do nothing but in in every case you return as fast as possible. In 99.999% of the ticks you will do nothing and let it return immediately, only in the rare case that the condition is met you do something and then return. You never wait.


There are exceptions to this general rule but before you do this you should understand and master the event driven approach since this is the norm and the recommendation while the other approach with polling and waiting is the exception and only used in rare cases and you need a good reason to do it because it is ugly.

 
avatar
11
moneysmile 2011.05.19 22:10 #
 
7bit:

The programming is event based: You don't wait and then continue, instead in every run of the start function (which will happen with every tick) you check your conditions and then either do something or do nothing but in in every case you return as fast as possible. In 99.999% of the ticks you will do nothing and let it return immediately, only in the rare case that the condition is met you do something and then return. You never wait.


There are exceptions to this general rule but before you do this you should understand and master the event driven approach since this is the norm and the recommendation while the other approach with polling and waiting is the exception and only used in rare cases and you need a good reason to do it because it is ugly.


Thanks for the answer. I used word "wait" because init() function will work on the initialization of EA once and after finish everything in the function init() will "continue/initialize" start() function. Start function will be affected with every TICK. In the end when we want to exit our EA it will run deinit() once. I understood that way... please tell me if I'm wrong? My question is: Does init() function can use "for" loop receiving Tick-s and when condition is True Bid >= Price+0.005 do something and then initialize start() function?
 
avatar
10209
WHRoeder 2011.05.19 22:31 #
 
Init CAN NOT WAIT. It must return in under two seconds IIRC
bool first;
init(){ first=true; }
start(){
  if (first){
     if (!condition) return;
     first=false;
     ...

 
avatar
893
7bit 2011.05.19 22:33 #
 

The init() fuction must return immediately. It is only meant for initializing variables and stuff, this goes fast, so it may immediately return. If your init() is stuck in some long running task then some menu items like EA settings will not work anymore, MT4 is expecting init() to return immediately and behaving strange if it does not.

The same with the deinit() function. MT4 will kill it if it does not return within 2 seconds.

You should do any trading always only in start. You can declare a boolean variable (in the global scope, outside of all functions) to let the EA remember in which phase of the trading it is: Set it to False in init() and then in start() you check whether it is False or True.

If it is False this means we are in the first phase, the very first tick after init(), you open the positions and set the variable to True (and then return) to let the next incarnation of start() know that phase 1 is now done. In all subsequent ticks it will then find this variable is already True (which means we are now in phase 2) and check for the condition to close the trades, either close them when your close condition is met and then return or simply do nothing and return.

closing trades in deinit() is dangerous because deinit() may only run a few seconds and will be killed without warning if it takes longer.


 
Back to topics list  

To add comments, please log in or register