Arrays with the double& declaration, how to make them global?
Because of the use of that ampersand char (needed because their values
are changed within this function) it seems impossible to declare these
types a global (please correct me when I'm wrong.)
Your wrong;//+------------------------------------------------------------------+ //| Ema's | //+------------------------------------------------------------------+ double ema.fast[], ema.slow[]; // Exported to STD, STC int emaBarsFast; double emaAlphaFast; void OnInitEmaFastRB(){ emaBarsFast=0; emaAlphaFast=2.0/(EMA.Fast+1); } int emaBarsSlow; double emaAlphaSlow; void OnInitEmaSlowRB(){ emaBarsSlow=0; emaAlphaSlow=2.0/(EMA.Slow+1); } void OnTickEmaFastRB(){ CalcEMA(Close.RB, emaAlphaFast, emaBarsFast, ema.fast); } void OnTickEmaSlowRB(){ CalcEMA(Close.RB, emaAlphaSlow, emaBarsSlow, ema.slow); } void CalcEMA(double signal[], double alpha, int& barsDone, double& buffer[]){ double barsToDo = ArraySize(signal); if (!ResizeBuffer(buffer, BarsToDo)) return; if (barsDone == 0){ int shift = barsToDo - 1; double sigPrev = signal[shift]; } else{ shift = barsToDo - barsDone; sigPrev = buffer[shift+1]; } for(; shift >= 0; shift--){ sigPrev += alpha * (signal[shift] - sigPrev); buffer[shift]=sigPrev; } barsDone = barsToDo; return; } bool ResizeBuffer(double buffer[], int size){ if (ArraySize(buffer) != size){ ArraySetAsSeries(buffer, false); // Shift values B[2]=B[1]; B[1]=B[0] if (ArrayResize(buffer, size) <= 0){ trading.disabled = "ArrayResize failed: "+GetLastError(); return(false); } ArraySetAsSeries(buffer, true); } return(true); }
Greatings MQL4 coders and a happy new year!,
My current AE has a function like this:
Because of the use of that ampersand char (needed because their values are changed within this function) it seems impossible to declare these types a global (please correct me when I'm wrong). As a result of this I'm forced to use the ArrayResize() function within this function. This is not only inefficient (this function is called many times and has 16 arrays) but also makes my function too inflexible for my purpose.
Is there a way to declare arrays global and that I can resize them only once within the Init() function?
Thank you in advance for any feedback,
Jim