Define open of a candle

 

Greetings all,
Im trying to define the 16 hour candle open price.

The code Im using is ....


   MqlRates ratesM[];

   ArraySetAsSeries(ratesM,true);

   int copied=CopyRates(Symbol(),PERIOD_H1,0,2,ratesM);

   double Open = ratesM[0].open;


I know how to shift it back, but dont know how to define the 16 candle.

Any help greatly appreciated.

 
MPFX1:


Search this website, and you will find your answer. I reckon the answer will be in the compiler help files. if not, then in the online documents. Do a search.

 
Michael Charles Schefe #:

Search this website, and you will find your answer. I reckon the answer will be in the compiler help files. if not, then in the online documents. Do a search.

Ive tried this site and web, but Ill try the compiler help files.. To be honest I thought I could find it so quickly, to make a post here is always my last resort.. 

 
MPFX1 #:

Ive tried this site and web, but Ill try the compiler help files.. To be honest I thought I could find it so quickly, to make a post here is always my last resort.. 

https://www.mql5.com/en/docs/matrix/matrix_initialization/matrix_copyrates

OR

https://www.mql5.com/en/docs/series/copyrates

There is even examples towards the end of the page. If this is not what you want then, give more detail of what you mean by "define the 16th candle".

Documentation on MQL5: Matrix and Vector Methods / Initialization / CopyRates
Documentation on MQL5: Matrix and Vector Methods / Initialization / CopyRates
  • www.mql5.com
CopyRates - Initialization - Matrix and Vector Methods - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Michael Charles Schefe #:

https://www.mql5.com/en/docs/matrix/matrix_initialization/matrix_copyrates

There is even examples towards the end of the page. If this is not what you want then, give more detail of what you mean by "define the 16th candle".

Ive had a read of that but I have to be honest I just dont understand a lot of it.. I was ok using MT4 code, but since switching to MT5 Im completely lost as I dont have a coding background.. 

All i want is to get the open value of the 16 hour candle, on an hourly chart, so I can set my buffer for a horizontal line to it.. 

Thing is I know its simple, but its just doing my head in.. 

And thx for your input so far, I do appreciate it.. 


This is my code Im trying to use..

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

   MqlRates ratesM[];

   ArraySetAsSeries(ratesM,true);

   int copied=CopyRates(Symbol(),PERIOD_H1,0,2,ratesM);


   double Open = ratesM[0].open;

 
MPFX1:

Any help greatly appreciated.

ok. maybe this is what you want?

   MqlRates ratesM[];

   ArraySetAsSeries(ratesM,true);

   int copied=CopyOpen(Symbol(),PERIOD_H1,15,1,ratesM);; // rates start with 0 for current candle, therefore we want candle #15 to pass candle #16 open price.

   double Open = ratesM[0].open;

If i am not mistake then Open = rates.M[0].open will report the 16th candles' Open price. I am sure that a moderator will correct me if need be.

 
MPFX1 #:

Please use the Code button.

But it is easier to do in OnCalculate as the rates are already loaded on every tick.

I could give you example, however I know that you can find the answer on almost every indicator that you will find on codebase.

 
Michael Charles Schefe #:

Please use the Code button.

But it is easier to do in OnCalculate as the rates are already loaded on every tick.

I could give you example, however I know that you can find the answer on almost every indicator that you will find on codebase.


Thanks again, It returned this code error

'CopyOpen' - no one of the overloads can be applied to the function call


int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
    MqlRates ratesM[];

   ArraySetAsSeries(ratesM,true);

   int copied=CopyOpen(Symbol(),PERIOD_H1,15,1,ratesM);; // rates start with 0 for current candle, therefore we want candle #15 to pass candle #16 open price.

   double Open = ratesM[0].open;
 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot 50%
#property indicator_label1  "Open"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Magenta
#property indicator_style1  STYLE_SOLID
#property indicator_width1  4




//--- indicator buffers      
double         OpenBuffer[];


//+-----------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,OpenBuffer,INDICATOR_DATA);

  
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
    MqlRates ratesM[];

   ArraySetAsSeries(ratesM,true);

   int copied=CopyOpen(Symbol(),PERIOD_H1,15,1,ratesM);; // rates start with 0 for current candle, therefore we want candle #15 to pass candle #16 open price.

   double Open = ratesM[0].open;
   
  

   int pos=prev_calculated-1;
   if(pos<0) pos=0;

   for(int i=pos; i<rates_total; i++)
     {
      OpenBuffer[i]= Open;
   
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

This is complete code.. Im dare say its far from the best way, but I just try my best to make things work

 
MPFX1 #:

yes, I am not sure that MqlRates will work in OnCalculate. Did you open a few indicators from codebase. use a few search terms like Open price and you will undoubtedly find your answers. I can give you the answers, but the only thing you will learn is to copy and paste. By reading a real, working example you will not only read the answer, but you will read the lines leading up to it and what you need beforehand.

 
Michael Charles Schefe #:

yes, I am not sure that MqlRates will work in OnCalculate. Did you open a few indicators from codebase. use a few search terms like Open price and you will undoubtedly find your answers. I can give you the answers, but the only thing you will learn is to copy and paste. By reading a real, working example you will not only read the answer, but you will read the lines leading up to it and what you need beforehand.

Thx, Ive searched lots, but the lead on CopyOpen might help me big time..  Like I said, I self taught myself MT4 over 15 years ago, but I like MT5 platform far more.. So the leaning curve is starting all over again. MT5 is not as user friendly, but totally get its a far more powerful code to use..  Thanks again for your help

Reason: