Preserve the value of the variable in the chart change (periods)

 

Hi guys,

I have this problem: I have indicator which calculate some arithmetic operations.

If change period on chart --> all aritmetic operations have to be make again. This very very slowing down and placing a burden on my pc. And some data is loss.

My idea was - simply demonstrative mql code:

#property version   "1.00"
#property strict
#property indicator_chart_window
int count=0;

void OnDeinit(const int reason)
{
  if (_UninitReason==REASON_CHARTCHANGE) Alert("change chart OnDeinit REASON_CHARTCHANGE   count=",count); 
}

int OnInit()
{
  Alert(__FUNCTION__," change chart OnInit REASON_CHARTCHANGE   count=",count); 
  return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{}
//+------------------------------------------------------------------+
void start()
{ 
  count++;
  if (count==5) { Alert(StaticValue()); count=0; }
}

//+------------------------------------------------------------------+
int StaticValue()
{
   static int static_value=0;
   return(static_value++);
}

 

 But this is not solution for my problem. Variable static_value after change period on chart always begin from zero.

This is simply code. In real code I need remember value of 4 arrays.

Can you help me, please? Thank you.

 
You could write the data to a file in OnDeinit
 

It isn´t good solution. 

works with file (time write and reading)  because fast repeat change periods on chart would phase out activities code!

 
Global Variables?
 

I know about it. Thank you, gooly. But - for arrays data? Global variable for variable type of array mql not supported.

I would have to more individual variable and then select, sorte global variables to properly match the value into the index array.  

I am afraid the situation when user quckly repeat change period. Does it other solution?... 

 

use a loop?

for(int i=0;i<ArrSze;i++) GlobalVariableSet( "arrName "+(string)(i+10000), arr[i]);

=> i+10000 is an easy way (if ArrSze<=9999) to add leading zeros for a lexical==numerical sort order.

But don't forget to subtract it..

 

Thanks for the willingness to help. I'm thinking about the potential problems of running the indicator.. As an example of the situation when it will be completed candle and the same time user change period.

I have solution next problem - synchronize. My next idea for solution - divide the indicator into two parts. The first part (only calcul buffer indicator) is running one chart. This chart will be user prohibited swith period, the second part is running next chart, when will be user enabled switch period. I have to think comunications between parts of indicators.I think that this way will be the source of any smaller problems to futures my code and for debuggering code soo. 

Reason: