Download MetaTrader 5

Logic error in current bar

To add comments, please log in or register
grknaveen
23
grknaveen  

Hi Techies,

 

I need to initiate order when the current bar is opened at some price and moved 5 pips high or low and return to the open price

Please help me. 

 

regards,

Naveen 

honest_knave
Moderator
2317
honest_knave  
Hi, what have you got so far?
grknaveen
23
grknaveen  
honest_knave:
Hi, what have you got so far?
I am not able to calculate the current price moved to 5 pips or not and returned to opened price.
grknaveen
23
grknaveen  
int Direction() 
  {
   int direction=NEUTRAL;
   static double price[3];
   price[2]=price[1];
   price[1]=price[0];
   price[0]=Ask;
   
   if(price[2] - price[0] >= 0.00006)
     {
      while(price[2] == price[0])
        {
        Alert(price[2], price[0]);
         return direction = UP;
        }
     }
   if(price[0] - price[2] >= 0.00006)
     {
      while(price[2] == price[0])
        {
        Alert(price[2], price[0]);
         return direction = DOWN;
        }
     }
honest_knave
Moderator
2317
honest_knave  

If this is what you want:

grknaveen:

I need to initiate order when the current bar is opened at some price and moved 5 pips high or low and return to the open price

You can make it easier.

Is this MQL4? 

Compare Open[0] to High[0] - this should be greater than 5 pips.

Compare Open[0] to Close[0] - this will tell you if price has returned to the open price.

Use Volume[0] if you want it to happen within so many ticks.

You also might want to change this:

  if(price[2] - price[0] >= 0.00006)

It won't work on JPY crosses, or 4 digit brokers.
 

grknaveen
23
grknaveen  
I am able to get the prices of current bar but the validation is problem for me
grknaveen
23
grknaveen  
yes this is MQL4
grknaveen
23
grknaveen  

you mean to say the below code will work:

 

if((High[0] - Open[0] >= 0.0005) && (Open[0] == Close[0])){

//initiate Buy 

honest_knave
Moderator
2317
honest_knave  

That's the general gist of it.

A few extra things to consider...

I would do Open[0]<=Close[0] rather than ==

I would restrict how many orders can be placed each bar. Otherwise, if price stays below the Open it will keep firing off buy orders. 

whroeder1
16112
whroeder1  
grknaveen: you mean to say the below code will work:
if((High[0] - Open[0] >= 0.0005) && (Open[0] == Close[0])){
   //initiate Buy
}
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. You are not adjusting for 4/5 digit brokers/JPY pairs. (5 * pip)
    double   pip        = StringFind(_Symbol,"JPY") < 0 ? 0.01 : 0.0001;
    int      pip_digits = (int)MathLog10(pip/_Point);
    int      slippage   = 3 * int(pip / _Point);

  3. The two values will never be equal The == operand. - MQL4 forum as honest_knave pointed out.
To add comments, please log in or register