Close price of the bar

 

Hi, Learning MQ4.. so need help on some basic things

Following is what I need to find:

1. I need to find bar close price only when the bar is closed.

2. How do I identify the time in Mq4 in this format : HH:MM:SS ?

1) To achieve the first one - I coded like this,, does this really provide the close price of the last candle, that just closed ?

extern int StartHour = 10;
extern int GMToffset = 2;
extern int EndHour = 14;

In start section:

liHour = Hour();

if (liHour == EndHour + GMToffset)
double lclose = iClose(Symbol(),0,0); // getting the closed price of 7 AM (PST)candle;

2) Now I need to implement some other logic as soon as the new bar starts

I was thinking of using seconds parameter along with minutes and hour ..(if that is good) buy only after the last bar close.. If I simply use "

int Seconds( )
"I am not sure whether this servs the purpose or not..

Please help..

 

iClose(Null,0,0) actually returns the price at the last tick because the current bar is not closed yet while it is still forming

iClose(Null,0,1) returns price of the last completed bar at close

 

You'll find these useful I think...

Time to string

https://docs.mql4.com/convert/TimeToStr


Finding new bars...

int start()
  {
  Fun_New_Bar();
     if (New_Bar)                                          
        {
        //DO new bar stuff
        }
   }

void Fun_New_Bar()
      {
      static datetime New_Time = 0;
      New_Bar = false;
      if (New_Time!= Time[0])
         {
         New_Time = Time[0];
         New_Bar = true;
         }
      }


hth

V

 

Viffer why not just turn New_Bar itself into a bool function call like so:

int start()
   {
   if (New_Bar()==true)
      {
      //DO new bar stuff
      }
   }
   
bool New_Bar()
      {
      static datetime New_Time = 0;
      if(New_Time!= Time[0])
         {
         New_Time = Time[0];
         return(true);
         }
      else return(false);
      }
 
 

Yeah, cool. It doesn't matter how many times you read it in the book you can't beat real feedback.

Thanks

V

 

ha ha, don't assume I'm some guru or something, that was an honest question! I just so happen to be shopping for a better new-bar detection scheme versus my current implementation and I stumbled across your post. So...seriously is there any reason not to use a bool function like what I proposed? Is there a known weakness to this approach? Is it less robust than an alternative implementation? Eager minds await the answer!

 
1005phillip:

ha ha, don't assume I'm some guru or something, that was an honest question! I just so happen to be shopping for a better new-bar detection scheme versus my current implementation and I stumbled across your post. So...seriously is there any reason not to use a bool function like what I proposed? Is there a known weakness to this approach? Is it less robust than an alternative implementation? Eager minds await the answer!

You're a guru in my eyes Phil, but don't let it go to your head... Gordon and CB have God status :)

I picked that function up from the book I think or maybe something Russel posted... I do remember at the time trying to call it within the If but had trouble... I think that trouble may have been to do with the fact the function was declared as VOID rather than as you have done as a BOOL. I got it to work as I posted and thought no more of it.


However, to offer an opinion on which is more efficient... The flow of my code swaps from using tick to new bar information in 3 or 4 seperate places. I call Fun_New_Bar to set New_Bar as true or false at the head of start and then simply test if(New_Bar)... Presumably if I were to call the full function every time for something I already knew the answer to, this would be less efficient, but if it was being called once, then I think your method is crisper.

V

 
bool    newBar; 
int start() {                   static datetime Time0;
    newBar = Time[0] > Time0;   if (newBar) {   Time0 = Time[0];
    double barClosePrice = Close[1];
 
WHRoeder wrote >>


Vow.. Great.. many good answers . Thank you very much for your help... I appreciate it.
Reason: