Download MetaTrader 5

direction of current bar

To add comments, please log in or register
rlarz
53
rlarz  
How does one determine the direction (up/down) of the current bar. Close[0] > Open[0] should work but Close value appears to be empty until the bar is complete. I imagine I should be trying to get the most recent value rather than Close. How can I do that?
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI  
Try Close[1] > Open[0] or Close[1] < Open[0]
Carl Schreiber
7111
Carl Schreiber  
rlarz:
How does one determine the direction (up/down) of the current bar. Close[0] > Open[0] should work but Close value appears to be empty until the bar is complete. I imagine I should be trying to get the most recent value rather than Close. How can I do that?
The Close[0] value is not empty, it is the last Bid and so it changes until the bar is completed!
rlarz
53
rlarz  

Thank you deysmacro. I don't understand why, but it does work.

 Edit: I take that back (not the thank you); I was checking more bars and noticed it was working more by coincidence. 

ffoorr
1081
ffoorr  
rlarz:
How does one determine the direction (up/down) of the current bar. Close[0] > Open[0]

This is not a good solution because at the beginning of a new bar, the direction will change and change.

could be

Close[0] > Close[1]; up same problem 

Close[0] > Close[1] - spread*Point ; up 

Close[0] > Open[1]; up

((Close[0] + Open[0])/2)> ((Close[1] + Open[1])/2)
rlarz
53
rlarz  
Using a loop with Close[i] > Open[i] = 1 & Close[i] < Open[i] = -1 works fine in an indicator but returns EMPTY_VALUE when the indicator is used in an EA. I tried comparing the Open with the Bid (and with Ask) but that doesn't work either. I'm not interested in the previous bar. All I want to know is if the current bar is up or down at the moment and I want it to work in an EA.
Ernst Van Der Merwe
5003
Ernst Van Der Merwe  
rlarz:
Using a loop with Close[i] > Open[i] = 1 & Close[i] < Open[i] = -1 works fine in an indicator but returns EMPTY_VALUE when the indicator is used in an EA. I tried comparing the Open with the Bid (and with Ask) but that doesn't work either. I'm not interested in the previous bar. All I want to know is if the current bar is up or down at the moment and I want it to work in an EA.
#define NEUTRAL 0
#define UP 1
#define DOWN 2

int digits;

int OnInit()
  {
   if(_Digits==3) digits=2;
   else           digits=4;
  
int Direction() 
  {
   int direction=NEUTRAL;
   static double price[3];
   price[2]=price[1];
   price[1]=price[0];
   price[0]=NormalizeDouble(Ask,digits);
   
   if(price[0]>price[2])   direction=UP;
   if(price[0]<price[2])   direction=DOWN;
   return(direction);
  } 
rlarz
53
rlarz  
pipPod:

I don't understand. How can comparing the current price with previous prices tell me whether or not the current bar is up or down. I don't want to know how it compares with previous bars; I just want to know if the current price (often called Last in other languages) is greater than or less than the current bar's Open.

I also tried using this function in the EA:

double GetBarDirection()
{
        if(Open[0]>Bid) return -1.0;
        else if(Open[0]<Bid) return 1.0;
        else return 0.0;
}

 It doesn't work either. All bars = 0.0;

rlarz
53
rlarz  
pipPod:

I just realized what your code is doing. That seems to be working. Thanks!

Edit: I spoke too soon again. On closer examination, bars that are obviously down (Red on my chart) are sometimes classified as up bars and vice versa. Unfortunately, the discrepancies are not consistent.

ffoorr
1081
ffoorr  
nice solution piPod
To add comments, please log in or register