How to find close value of any candle in any time period

 

Hello Expert,

I'm very new at programming.  The following code I copied from the mt4 tutorial.  It helped find the lowest/highest of the day before.  How can I find the "close" value of that "lowest or highest" candle from the day before?  I tried iclose, but nothing come out correctly.  Thank you so much for your input.  Jenny

Below is the program:

 

//--- Reset the last error

   ResetLastError();

//--- Get the last 2 High values on the daily timeframe

   int highsgot=CopyHigh(Symbol(),PERIOD_D1,0,2,highs);

   if(highsgot>0) // If copying was successful

     {

//      Print("High prices for the last 2 days were obtained successfully");

      prevHigh=highs[0]; // The previous day High

//      Print("prevHigh = ",prevHigh);

      if(ObjectFind(0,highlevel)<0) // Object with the name highlevel not found

        {

         ObjectCreate(0,highlevel,OBJ_HLINE,0,0,0); // Create the Horizontal Line object

        }

      //--- Set value for the price level for the line highlevel

      ObjectSetDouble(0,highlevel,OBJPROP_PRICE,0,prevHigh);

      //--- Set the visibility only PERIOD_M15 and PERIOD_H1

      ObjectSetInteger(0,highlevel,OBJPROP_TIMEFRAMES,OBJ_PERIOD_M15|OBJ_PERIOD_H1);

     }

   else

     {

      Print("Could not get High prices over the past 2 days, Error = ",GetLastError());

     }

 

//--- Reset the last error

   ResetLastError();

//--- Get the 2 days values Low on the daily timeframe

   int lowsgot=CopyLow(Symbol(),PERIOD_D1,0,2,lows);

   if(lowsgot>0) // If copying was successful

     {

//      Print("Low prices for the last 2 days were obtained successfully");

      prevLow=lows[0]; // The previous day Low

      Print("prevLow = ",prevLow);

  

      if(ObjectFind(0,lowlevel)<0) // Object with the name lowlevel not found

        {

         ObjectCreate(0,lowlevel,OBJ_HLINE,0,0,0); // Create the Horizontal Line object

        }

      //--- Set value for the price level for the line lowlevel

      ObjectSetDouble(0,lowlevel,OBJPROP_PRICE,0,prevLow);

      //--- Set the visibility only PERIOD_M15 and PERIOD_H1

      ObjectSetInteger(0,lowlevel,OBJPROP_TIMEFRAMES,OBJ_PERIOD_M15|OBJ_PERIOD_H1);

     }

   else Print("Could not get Low prices for the last 2 days, Error = ",GetLastError());

 

   ChartRedraw(0); // redraw the chart forcibly 

 

You can find the index and value of the highest with

  int index=ArrayMaximum(highs,WHOLE_ARRAY,0);
  double max=iClose(Symbol(),PERIOD_D1,index)

 

 Similarly with the lows (with ArrayMinimum() ), but be sure that the lows array is the same size as the number of values copied. Otherwise it may contain some zeroes.

In your code, the start position for CopyHigh is 0, so you can use index with iClose. If the start position is anything else, index would need to be increased accordingly. 

 

Thank you GumRai,

 How does this code know where to start?  I see the period is daily, but how does it know to check yesterday or the day before?  Does the "0" inside the ArrayMaximum means start "today" and "1" would be yesterday?  Thanks again!  Jenny

 
hotaggie98:

Thank you GumRai,

 How does this code know where to start?  I see the period is daily, but how does it know to check yesterday or the day before?  Does the "0" inside the ArrayMaximum means start "today" and "1" would be yesterday?  Thanks again!  Jenny


I forgot to mention that you should also make sure that the target array is "set as series" before actually copying to be able to use the index for iClose.
 
As the Period is Daily, then yes, 0 refers to the current/last day, 1 is the previous bar on the daily chart etc
 
hotaggie98: Does the "0" inside the ArrayMaximum means start "today" and "1" would be yesterday?
  1. You read the manual ArrayMaximum - MQL4 Documentation and you would know the answer. Instead you waste everyones time with trivia and show that you haven't bothered to learn.
    learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
  2. You don't need to even use arrays as yesterdays close is:
    double yestClose = iClose(NULL,PERIOD_D1,1);
 

I made:

double highs [];  //to set "highs" as an array to be used inside ArrayMaximum

  int index=ArrayMaximum(highs,WHOLE_ARRAY,0); 

  double max=iClose(Symbol(),PERIOD_D1,index) 

 

but it kept giving the "close" value of the last candle of the previous day instead of the "close" of the highest candle of yesterday.  I thought that by setting "highs" as an array, it'll store all the "high" from yesterday in an array and then search for the "close" of within those "highs".  My thought is correct, but I'm not sure if my code reflect that.  What am I missing here?  Thanks! 

 

WHRoeder,

 I'm not asking anyone to code anything for me.  I AM trying to learn, and when you first learned something, it's confusing and it doesn't matter how many time you read something, if you don't understand it, then it won't make sense to you.  Have you ever learned a foreign language?  it doesn't matter how many time you read chinese or japanese, it won't make sense to you until someone help you out.  This is a foreign language for someone who has never done any programming before. I've been sitting here and reading and trying for 4 days before I ask someone thank you very much.  If you can't have compassion, then don't be a smart ass either! If you don't want to teach someone, then don't!!!   No one asking YOU! 

By the way, I can find yesterday "CLOSE" all by myself.  What I am originally asking for is "HOW TO FIND THE CLOSE OF THE HIGHEST CANDLE" of yesterday, and that's the PROBLEM that we're trying to work on here.  If you want me to read, then read my problem first then! 

Thanks,

 Jenny. 

 
hotaggie98:

By the way, I can find yesterday "CLOSE" all by myself.  What I am originally asking for is "HOW TO FIND THE CLOSE OF THE HIGHEST CANDLE" of yesterday, and that's the PROBLEM that we're trying to work on here.  If you want me to read, then read my problem first then! 

An EA or indicator can only do what it has been coded to do.

If you are basing calculations on the daily chart, "yesterday" only had a single candle, so "yesterday's" highest candle MUST be that single candle. 

So you have to decide in which time-frame to look for the highest candle. 

You will need to find the datetime value of midnight and use iBarshift to find the start and end index of "yesterday's" candles in the chosen time-frame

Then you can use iHighest to find the index of the highest candle

Then iClose to find the close of that candle 

To be honest, this is probably too complicated for somebody who is just starting to learn. 

 

Thank you GumRai so much for your explanation.  I really appreciate your time.  I'll continue to look into it.  Thanks again!  Jenny  

 
And again, you don't need the array. You want the find the highest candle on the chart timeframe for yesterday. You can just do that.
#define HR2400 (PERIOD_D1 * 60)  // 86400 = 24 * 3600
int      TimeOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when % HR2400 );            }
datetime DateOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when - TimeOfDay(when) );   }
//datetime Tomorrow( datetime when=0){      if(when == 0)  when = TimeCurrent();
//                                          return(DateOfDay(when) + HR2400);   }
//datetime Yesterday(datetime when=0){      if(when == 0)  when = TimeCurrent();
//   int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
//                                       return( iTime(NULL, PERIOD_D1, iD1) ); }

datetime today     = DateOfDay();          int iTod = iBarShift(NULL,0, today);
datetime yesterday = DateOfDay(today - 1); int iYes = iBarShift(NULL,0, yesterday);

int iHH  = iHighest(NULL,0, iYes - iTod, iTod + 1); // Bar with yesterdays highest high
double max = Close[iHH]; // double max=iClose(Symbol(),PERIOD_D1,index)