Download MetaTrader 5

Programming question, MQL4

To add comments, please log in or register
Hatem Abou Ouf
1448
Hatem Abou Ouf  

Hi, i have just started to learn mql4. Now, I need to store the high/low of current bar ( or current bid/ask) and keep it without change even if new current bar comes? Till, i decide later to reset it again to current. You can help me with Pseudocode, sample code or whatever.

 

Your help is much appreciated.

Thanks 

GreenMoney
379
GreenMoney  
Review the documentation for Static Variables.  That should give you someplace to start.
Hatem Abou Ouf
1448
Hatem Abou Ouf  

Thank you. I have looked at this link and tried to code it  

void OnTick()
  {
    if (OrdersTotal () == 0)
       {
        Comment ( "first price ", FindFirstPrice (),"  ",  " bid is " , Bid);
       return;
       }
  }//+-----------------------------------------------------------------------+
//| Get first price                                                       |
//+-----------------------------------------------------------------------+ 

double FindFirstPrice () // it keeps the first bid but how could i change it!! when condition met 
                         // and call this function again. For now it keeps giving the same value  
    { 
        static double firstPrice = MarketInfo ( "",MODE_BID);;
           
        return (firstPrice);
    }
  

 Still not giving me what i want which is;

1- when i call the function --> it should give me a shot of the bid and keep it without change

2- if i call it again as long as condition not met, it gives me the same shot

3- if condition met, and i call the function , it should give me a new shot of the bid.

 

For now, it keep giving me the same first shot. 

Hatem Abou Ouf
1448
Hatem Abou Ouf  

Let us say, i need to get the first Bid on every new bar started. 

So, the condition is " new bar"

Keith Watford
Moderator
10533
Keith Watford  

What condition?

{ 
        static double firstPrice = MarketInfo ( "",MODE_BID);
        if(ConditionMet)
             firstPrice = MarketInfo ( "",MODE_BID);  
        return (firstPrice);
    }
Hatem Abou Ouf
1448
Hatem Abou Ouf  

I tried to code it again based on your response in this thread https://www.mql5.com/en/forum/145119 regarding finding new bar. 

here is my trial,

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
 if (OrdersTotal () == 0)
       {
        Comment ( "first price ", FindFirstPrice (),"  ",  " bid is " , Bid);
       return;
       }
   
  }
//+------------------------------------------------------------------+
bool FindNewBar() 
{

   static datetime oldTime = 0;
   
   if (oldTime == 0)
   
      {oldTime = Time[0];
      return (false);}
      
     else if (oldTime < Time[0]) {
      Print ("New bar found! (", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), ")");
      oldTime = Time[0];
      return (true);   
   }

   return(0);
}

//+-----------------------------------------------------------------------+
//| Get first price                                                       |
//+-----------------------------------------------------------------------+ 

double FindFirstPrice () 
    {  
        static double firstPrice ;
        
        if ( FindNewBar ())
         {  
        return (firstPrice);
        }
        
        return (firstPrice);
        
    
    }

 It still gives me this input 

first price is 0.0 , bid is " current bid"

 

I am not sure what i am doing here wrong !

Thanks 

Hatem Abou Ouf
1448
Hatem Abou Ouf  
When i changed my code to this , it gives me true and false for new bar. Now, i know the new bar function is working properly
void OnTick()
  {
 if (OrdersTotal () == 0)
       {
        Comment ( "first price ", FindNewBar (),"  ",  " bid is " , Bid);
       return;
       }
   
  }
Hatem Abou Ouf
1448
Hatem Abou Ouf  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
 if (OrdersTotal () == 0)
       {
        Comment ( "first price ", FindFirstPrice (),"  ",  " bid is " , Bid);
       return;
       }
   
  }
//+------------------------------------------------------------------+
bool FindNewBar() 
{

   static datetime oldTime = 0;
   
   if (oldTime == 0)
   
      {oldTime = Time[0];
      return (false);}
      
     else if (oldTime < Time[0]) {
      Print ("New bar found! (", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), ")");
      oldTime = Time[0];
      return (true);   
   }

   return(0);
}

//+-----------------------------------------------------------------------+
//| Get first price                                                       |
//+-----------------------------------------------------------------------+ 

double FindFirstPrice () 
    {  
       
        
        if ( FindNewBar ())
         
           {
         static double  firstPrice = MarketInfo (" ", MODE_BID);
           }

     return (firstPrice);
    }
Undeclared "firstPrice" error 
Syed Naufal Gaddafi
1404
Syed Naufal Gaddafi  

Declare it outside of the function else it would be internally declared.

static double  firstPrice = 0.0


then


//+-----------------------------------------------------------------------+
//| Get first price                                                       |
//+-----------------------------------------------------------------------+ 

double FindFirstPrice () 
    {  
       
        
        if ( FindNewBar ())
         
           {
         firstPrice = MarketInfo (" ", MODE_BID);
           }

     return (firstPrice);
    }
Hatem Abou Ouf
1448
Hatem Abou Ouf  

You mean declare it in global space. If so, i did and still giving me this zero result. 

Thanks, 

GreenMoney
379
GreenMoney  
EgyAlgoTrader:
Undeclared "firstPrice" error 

Try this:

 Not compiled or tested

double FindFirstPrice () {
   static double firstPrice = 0.0;                   // declare static variable and initialize to 0  
   if ( FindNewBar() )                               // if FindNewBar is true
      firstPrice = MarketInfo(_Symbol, MODE_BID);    //    set firstPrice to symbol's current Bid Price
   return(firstPrice);                               // return contents of variable firstPrice
}

 

123
To add comments, please log in or register