Compare Bid and bar open price

 

Hello,

I want to compare the Bid and the Candle open price at the beginning of each candle, in EA.

If Bid goes 20 Point(or more) higher than the Candle open price, Take a position.

If Bid goes 20 Point(or more) lower than the Candle open price, Take a position.

and And then wait for the next candle.

for example:

The candle opened and open price is: 20222.4

if bid is 20224.4 or more : open position.

if bid is 20220.4 or more: open position.

and then wait for the next candle to open...



I searched a lot but couldn't find anything to help.



I use this code to detect the beginning of the candle:

bool IsNewBar()
{
   static datetime lastbar;
   datetime curbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
   if(lastbar != curbar)
   {
      lastbar = curbar;
      return true;
   }
   return false;
}

I was able to write this code, but this code compares the Bid to the previous candle opening price, at the beginning of each candle:

//+------------------------------------------------------------------+
//|                                                      example.mq4 |
//|                                         |
//|                                                                  |
//+------------------------------------------------------------------+



double priceB;
double priceS;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   priceB = Open[0] + (20*Point);
   priceS = Open[0] - (20*Point);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      if(IsNewBar()){
       
  if(Bid > priceB)
       {
        Alert("UP", Bid); 
       }
   if(Bid < priceS)
       {
        Alert("DOWN", Bid); 
       }
    priceB = Open[0] + (20*Point);
   priceS = Open[0] - (20*Point); 
   
   } 
       
//----
   return(0);
  }
//+------------------------------------------------------------------+


bool IsNewBar()
{
   static datetime lastbar;
   datetime curbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
   if(lastbar != curbar)
   {
      lastbar = curbar;
      return true;
   }
   return false;
}


thanks for your help.

 

replace

if(IsNewBar()){
       
  if(Bid > priceB)
       {
        Alert("UP", Bid); 
       }
   if(Bid < priceS)
       {
        Alert("DOWN", Bid); 
       }
    priceB = Open[0] + (20*Point);
   priceS = Open[0] - (20*Point); 
   
   } 

with

if (IsNewBar())
{       
   priceB = Open[0] + (20*Point);
   priceS = Open[0] - (20*Point); 
}
if (Bid > priceB)
{
   Alert("UP", Bid); 
}
if (Bid < priceS)
{
   Alert("DOWN", Bid); 
}
   
 

because otherwise the prices are updated after the checks and not in realtime (1 bar late)

 
Jean Francois Le Bas:

replace

with

because otherwise the prices are updated after the checks and not in realtime (1 bar late)

thanks for answer,

This is true, But this is َAlert in each tick,  I want to َAlert it once in a candle, and wait for the next candle to check Bid and Candle Open[0],  

Whenever a candle is created, check it, once the condition ( if's  ) is right, get out of the checking and wait for the next candle.


Example for more details:

new candle created and Open[0] is: 2002.1

check and compare with the Bid, until bid is: 2004.1(or more) Or 2000.1(or more)

then Alert me and stop checking and alerting,

Until the next new candle is created

 
hamit0111:

This is true, But this is َAlert in each tick,  I want to َAlert it once in a candle, and wait for the next candle to check Bid and Candle Open[0],  

Not tested

   static bool waitNewBarUp=false;
   static bool waitNewBarDown=false;
   if(IsNewBar())
     {
      waitNewBarUp=false;
      waitNewBarDown=false;
     }

   if(!waitNewBarUp && Bid-Open[0]>=20*Point)
     {
      Alert("UP", Bid);
      waitNewBarUp=true;
     }

   if(!waitNewBarDown && Open-Bid[0]>=20*Point)
     {
      Alert("DOWN", Bid);
      waitNewBarDown=true;
     }
 
Keith Watford:

Not tested

thanks its worked
Reason: