Download MetaTrader 5

How do you check CLOSE in EA?

To add comments, please log in or register
selnomeria
254
selnomeria  

Hello.

I use script to force the EA to run once on every bar start.

..... if( Now == Time[0]) { return;  } ....

However, in my EA, let's say, Trade happens if CLOSE>OPEN of the bar (i dont matter ticks)...


however, the EA is executed only on OPENs of bars...


what is the solution? how can i get the CLOSE of that bar? b

Asad Raza
144
Asad Raza  

For check if new bar start. 


double prevtime = 0;

bool is_new_bar(){
  if(prevtime != Time[0]){
    prevtime = Time[0];
    return true;
  }

  return false;

}


you need to call is_new_bar() on every tick... 


If you want bar close value.

double current_close = Close[0];
double prev_close = Close[1];
double prev_prev_close = Close[2];
...
whroeder1
15925
whroeder1  
selnomeria: I use script to force the EA to run once on every bar start.

..... if( Now == Time( 0 )) { return();  } ....

what is the solution? how can i get the CLOSE of that bar? b

  1. You don't use that code, it doesn't compile. Time is an array not a function.
  2. Close of what bar. Bar zero is the newly created one. Perhaps you want the Close of the previous bar, number one.
selnomeria
254
selnomeria  
whroeder1:
  1. You don't use that code, it doesn't compile. Time is an array not a function.
  2. Close of what bar. Bar zero is the newly created one. Perhaps you want the Close of the previous bar, number one.


SOrry, you are right...


abut my goal:  I JUST WANT TO CHECK IF CURRENT 5 MINUTE BAR's close price is above open, then send buy-order.


I dont want to check this on next bar...


asad998

your solution of current close[0]  - If it is checked on first (new_bar_open) tick, then how it can get CLOSE value of the whole 5 min bar (because it is only first tick)? so, that's why I want that only to run on the CLOSE (LAST TICK) of bar in EA.

whroeder1
15925
whroeder1  
selnomeria: your solution of current close[0]  - If it is checked on first (new_bar_open) tick, then how it can get CLOSE value of the whole 5 min bar (because it is only first tick)?
  1. On the first tick of a new bar, Close[0] == Open[0] == High[0] == Low[0].
  2. You can't get the close of the current bar, because that is in the future.
  3. You can't even know which tick will be the last tick.
selnomeria
254
selnomeria  
whroeder1:
  1. On the first tick of a new bar, Close[0] == Open[0] == High[0] == Low[0].
  2. You can't get the close of the current bar, because that is in the future.
  3. You can't even know which tick will be the last tick.


WOW  :((((   

honest_knave
Moderator
2326
honest_knave  

At 11:59:58 are there going to be more ticks before 12:00:00?


At 11:59:59 are there going to be more ticks before 12:00:00? 


At 12:00:00 no more ticks can be added to the 11:59:00 bar. But the 12:00:00 bar doesn't open until the first tick arrives. That might be 12:00:01 or 12:00:03... 


You don't know the final close price of a bar until a new bar starts. Which is why many EAs are coded to trade on the first tick received of a new bar, using the prices of the previous bar.

selnomeria
254
selnomeria  
honest_knave:

At 11:59:58 are there going to be more ticks before 12:00:00?


At 11:59:59 are there going to be more ticks before 12:00:00? 


At 12:00:00 no more ticks can be added to the 11:59:00 bar. But the 12:00:00 bar doesn't open until the first tick arrives. That might be 12:00:01 or 12:00:03... 


You don't know the final close price of a bar until a new bar starts. Which is why many EAs are coded to trade on the first tick received of a new bar, using the prices of the previous bar.


yes, its good explanation.
To add comments, please log in or register