How to make an Array not Initilialize every time i change timeframe?

 

Hello,


Is it possible for an Array to keep its values when i am changing timeframe?

I have seen that every time i change timeframe the prices of the Array are initiliazing again.

I want to create a multi-timeframe indicator and it is crucial to store the values and keep them when timeframe changes.


Any help would be greatly appreciated.

 
You posted in indicators. The indicator should just redraw itself. You should not care. You should not need to store anything.
 

Example:

ENUM_TIMEFRAMES timeframe;
int handle=INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   timeframe=(TimeFrame==PERIOD_CURRENT)?(ENUM_TIMEFRAMES)_Period:TimeFrame;
   if((handle=iRSI(_Symbol,timeframe,Period,Price))==INVALID_HANDLE)
      return(INIT_PARAMETERS_INCORRECT);
//---
   string name=StringFormat("RSI %s(%d)",StringSubstr(EnumToString(TimeFrame),7),Period);
   IndicatorSetString(INDICATOR_SHORTNAME,name);
   IndicatorSetInteger(INDICATOR_DIGITS,1);
//--- indicator buffers mapping
   SetIndexBuffer(0,RSIBuffer,INDICATOR_DATA);
   ArraySetAsSeries(RSIBuffer,true);
   PlotIndexSetString(0,PLOT_LABEL,name);  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
   int ratesTotal=iBars(_Symbol,timeframe);
   static int prevCalculated=0;
//---
   if(BarsCalculated(handle)<ratesTotal)
      return(prevCalculated=0);
//---
   int toCopy=(ratesTotal!=prevCalculated || rates_total!=prev_calculated)?MathMin(ratesTotal,rates_total):1;
   if(CopyBuffer(handle,0,0,toCopy,RSIBuffer)!=toCopy)
      return(prevCalculated=0);
//---
   prevCalculated=ratesTotal;   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Files:
RSI_MTF.mq5  3 kb