English

## Help with arrays and memory

 30 forexbob1970 2016.07.22 15:26   From the diaries of the world's worst programmerSo I'm learning about arrays but not being very bright, struggling to understand how they actually populate themselves and how that affects memory allocation and resources.I'm trying to build my own currency strength indicator. First step, build an indicator to measure an individual currency. I tested a few different algo's and came up with this;```//+------------------------------------------------------------------+ //|                                                         boom.mq4 | //|                        Copyright 2016, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, MetaQuotes Software Corp." #property link      "https://www.mql5.com" #property version   "1.00" #property strict #property indicator_separate_window #property indicator_buffers 1 #property indicator_plots   1 //--- plot Strength #property indicator_label1  "Strength" #property indicator_type1   DRAW_LINE #property indicator_color1  clrBlue #property indicator_style1  STYLE_SOLID #property indicator_width1  1 //--- input parameters input int      L_Period=21; input int      TimeFrame=30; //--- indicator buffers double         StrengthBuffer[]; //--- internal arrays double         LongCandle[],AbsCandle[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function                         | //+------------------------------------------------------------------+ int OnInit()   { //--- indicator buffers mapping    SetIndexBuffer(0,StrengthBuffer);    //---    return(INIT_SUCCEEDED);   } //+------------------------------------------------------------------+ //| 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[])   { //---    ArrayResize(StrengthBuffer,Bars+L_Period,Bars+L_Period);    ArrayResize(LongCandle,Bars+L_Period,Bars+L_Period);    ArrayResize(AbsCandle,Bars+L_Period,Bars+L_Period);    for(int i=0; i0) counted_bars--;    limit=Bars-counted_bars; //---- calculate values    for(int i=0; i
 13009 WHRoeder 2016.07.22 16:20 #   ```   SetIndexBuffer(0,StrengthBuffer); :    ArrayResize(StrengthBuffer,Bars+L_Period,Bars+L_Period);```Do not resize buffers, they are already sized before the call. Make your other arrays non-visible buffers.`   for(int i=0; i
 30 forexbob1970 2016.07.23 10:25 #   Many thanks for your response. I've cleaned my mess up to look like this```//+------------------------------------------------------------------+ //|                                                         boom.mq4 | //|                        Copyright 2016, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, MetaQuotes Software Corp." #property link      "https://www.mql5.com" #property version   "1.00" #property strict #property indicator_separate_window #property indicator_buffers 1 #property indicator_plots   1 #property indicator_level1     4 #property indicator_level2     4.8 #property indicator_level3     5 #property indicator_level4     5.2 #property indicator_level5     6 #property indicator_levelcolor clrWhite #property indicator_levelstyle STYLE_DASHDOTDOT //--- plot Strength #property indicator_label1  "Strength" #property indicator_type1   DRAW_LINE #property indicator_color1  clrBlue #property indicator_style1  STYLE_SOLID #property indicator_width1  2 //--- input parameters input int      L_Period=21; //--- indicator buffers double         StrengthBuffer[]; //--- internal arrays double         LongCandle[],AbsCandle[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function                         | //+------------------------------------------------------------------+ int OnInit()   { //--- indicator buffers mapping    IndicatorBuffers(3);    SetIndexBuffer(0,StrengthBuffer);    SetIndexBuffer(1,LongCandle);    SetIndexBuffer(2,AbsCandle); //---    return(INIT_SUCCEEDED);   } //+------------------------------------------------------------------+ //| 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[])   { //---    for(int i=rates_total-1-MathMax(L_Period,prev_calculated); i>=0; --i)      {         {          double Body     = MathAbs(iOpen(Symbol(),0,i)-iClose(Symbol(),0,i));          double BodySize = Body/Point;          if(BodySize==0.0) BodySize=0.01;          if(iOpen(Symbol(),0,i)=i; cnt--)         {          deltalong=deltalong+LongCandle[cnt];          deltaabs=deltaabs+AbsCandle[cnt];         }       StrengthBuffer[i]=(deltalong/deltaabs)*10;      } //--- return value of prev_calculated for next call    return(rates_total-1);   } //+------------------------------------------------------------------+ ``` Hope that's got the right  count and taken the oranges out. Seems to work smoothly in ST.
 13009 WHRoeder 2016.07.23 16:08 #   Why use function calls when you can use the Predefined Variables - MQL4 Reference```if(iOpen(Symbol(),0,i)=i; cnt--)`