How to get the close price of yesterday and the open price of today

 

Hello, I have looked at both the iClose to get the close price and the iOpen for the open price.  I have also checked into using the MqlRates, but with no luck.  I am getting the error that the array is out of range with the MqlRates.  For the iClose and iOpen, I am getting 0 as the value.

The symbol is #GOOGL

Here is what I was doing for the MqlRates. :

                  MqlRates rates[];
                  ArraySetAsSeries(rates,true);
                  int copied=CopyRates(_Symbol,PERIOD_D1,0,3,rates);
                  double pr0_close= rates[0].close;
                  double pr1_close= rates[1].close; 
                  double pr0_open= rates[0].open;
                  double pr1_open= rates[1].open;
                  datetime t1 = rates[1].time;

Here is what I was doing for the iClose:

                 dblPreviousClose = iClose(_Symbol,PERIOD_D1,0);


Here is what I was doing for the iOpen:

                  dblOpen = iOpen(_Symbol,PERIOD_D1,0);


All of them are giving array out of range.

Can you see anything that needs changing?

 

Your array doesn't have a size.

Arrays need sizes.

 
kypa:

Your array doesn't have a size.

Arrays need sizes.

Thanks .. I will make the changes for the mqlrates.

What about the iClose and the iOpen.  I am only getting 0 as the result.

 

Tried adding MqlRates rates[3];  It is now giving 0 as the result for both the close and open values.

 
Eddi Rae Melton:

Tried adding MqlRates rates[3];  It is now giving 0 as the result for both the close and open values.

I found my solution. 

I had to refresh the symbol.  Once I did that, then the I was getting the values I needed.

     mySymbol.Name(strTAU_Symbol);
     mySymbol.RefreshRates();
     mySymbol.Refresh();


This is solved.

 
Eddi Rae Melton:

I found my solution. 

I had to refresh the symbol.  Once I did that, then the I was getting the values I needed.

     mySymbol.Name(strTAU_Symbol);
     mySymbol.RefreshRates();
     mySymbol.Refresh();


This is solved.

You don't need to call CSymbolInfo::Refresh() because it has already been called from CSymbolInfo::Name(). Also, here is a class that you can use to access time, price, and symbol info from the same object. 


#include <expert/expertbase.mqh>
#include <trade/trade.mqh>
#include <indicators/indicators.mqh>

class ExpertWrapper : public CExpertBase
{
public: 
   ExpertWrapper(){
      m_used_series = (
         USE_SERIES_OPEN|USE_SERIES_HIGH|USE_SERIES_LOW|
         USE_SERIES_CLOSE|USE_SERIES_SPREAD|USE_SERIES_TIME|
         USE_SERIES_TICK_VOLUME|USE_SERIES_REAL_VOLUME
      ); 
   }
   virtual bool Init(CSymbolInfo *symbol,ENUM_TIMEFRAMES period,double point) {
      if(period == PERIOD_CURRENT)
         period = _Period;
      bool parent = CExpertBase::Init(symbol,period,point);
      m_other_period = true;
      m_other_symbol = true;
      return parent;
   }
   ENUM_TIMEFRAMES timeframe() const { return m_period; }
};

class AllSeriesInfo : public CObject
{
protected:
   CSymbolInfo      *m_symbol;
   ExpertWrapper     m_expert;
   CIndicators       m_indicators;
public:
   AllSeriesInfo(){}
   int               init(string symbol, ENUM_TIMEFRAMES timeframe);
   void              refresh();
   //--- TimeSeries access
   double            bid()              const {return m_symbol.Bid();}
   double            ask()              const {return m_symbol.Ask();}
   double            open(int i)        const {return m_expert.Open(i);}
   double            high(int i)        const {return m_expert.High(i);}
   double            low(int i)         const {return m_expert.Low(i);}
   double            close(int i)       const {return m_expert.Close(i);}
   int               spread(int i)      const {return m_expert.Spread(i);}
   datetime          time(int i)        const {return m_expert.Time(i);}
   long              tick_volume(int i) const {return m_expert.TickVolume(i);}
   long              real_volume(int i) const {return m_expert.RealVolume(i);}
   //--- returns the CSymbolInfo object
   CSymbolInfo*      symbol_info()            {return m_symbol;}
   //--- symbol and timeframe
   string            symbol()           const {return m_symbol.Name();}
   ENUM_TIMEFRAMES   timeframe()        const {return m_expert.timeframe();}
};
int AllSeriesInfo::init(string symbol,ENUM_TIMEFRAMES timeframe)
{
   m_symbol = new CSymbolInfo();
   bool symbol_up = m_symbol.Name(_Symbol);
   bool init_up   = m_expert.Init(m_symbol, PERIOD_CURRENT, _Point);
   bool expert_up = m_expert.InitIndicators(&m_indicators);
   if(!symbol_up || !init_up || !expert_up)
      return INIT_FAILED;
   return INIT_SUCCEEDED;
}
void AllSeriesInfo::refresh(void)
{
   m_indicators.Refresh();
   m_symbol.RefreshRates();
}


EXAMPLE USE:


AllSeriesInfo info;

int OnInit()
{
   return info.init(_Symbol, PERIOD_CURRENT);
}

void OnTick()
{
   info.refresh();
   string comment = StringFormat(
      "\n\n\n"
      + "Symbol: %s\n"
      + "TimeFrame: %s\n"
      + "Tick-volume: %d\n"
      + "O[%.5f], H[%.5f], L[%.5f], C[%.5f]\n"
      + "Bid[%.5f], Ask[%.5f], Spread[%d]\n",
      info.symbol(), EnumToString(info.timeframe()),
      info.tick_volume(0),
      info.open(0), info.high(0),info.low(0),info.close(0),
      info.bid(), info.ask(),
      int((info.ask() - info.bid()) / info.symbol_info().Point())
   );
   Comment(comment);
}
Reason: