English

## Correct Way to Loop Through Last [X] Number of Variable Indexes

 2 Cnotey 2016.03.25 19:41   Hi there,New to the MQL4 language and C++ in general.  I am a C# coder by nature, so as you can imagine MQL4 is being a bit difficult.  My normal MQL4 coder is out of town for a week, so I need a bit of help with something.I am trying to loop through the last [X] number of indexes for a defined variable.  I need to access the last 14 values of a variable in the code below.I am not expecting someone to write the code for me.  Just point me in the correct direction.```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[])   { //---    int limit=rates_total-prev_calculated; //--- //ArraySetAsSeries(candle,false); //ArraySetAsSeries(shadow,false); //---    if(prev_calculated>0)       limit++;    for(int i=0; i
13009
WHRoeder 2016.03.25 20:14 #

Cnotey I need to access the last 14 values of a variable in the code below.
`double candle_height[](double open, double high, double low, double close)`
1. `ShadowsBuffer[i] = (shadow_height(open[i], high[i], low[i], close[i])/C`
The variables passed to OnCalculate, are not guaranteed to be in series order (newest = 0.) The Predefined Variables - MQL4 Reference (e.g. Close[]) are.
2. `double candle_height[](double open, double high, double low, double close)`
Bogus. You need to access n values, but you pass single values, so it can't possibly work. Just use the predefined's and you only have to pass the starting index.
3. `double candle_height_raw[] = CHraw - shadow_height_raw; `
You have a empty array, it can't be assigned to. Your function has no loop to fill the array, so can't work.
4.  I don't use any of the arguments. Use the predefined's ```   int limit=rates_total-prev_calculated;    if(prev_calculated>0) limit++;    for(int i=0; i= 0; --i){    :```

2
Cnotey 2016.03.25 20:53 #

WHRoeder:
1. The variables passed to OnCalculate, are not guaranteed to be in series order (newest = 0.) The Predefined Variables - MQL4 Reference (e.g. Close[]) are.
2. Bogus. You need to access n values, but you pass single values, so it can't possibly work. Just use the predefined's and you only have to pass the starting index.
3. You have a empty array, it can't be assigned to. Your function has no loop to fill the array, so can't work.
4.  I don't use any of the arguments. Use the predefined's You have a look back of 14 ("last 14 values") you must handle the it to avoid error 4002.

Thank you for your help.  Do I still need to loop through to fill the predefined variables (e.g. Close[]) to fill the values?  Or do I just give it Close[i..14]?
 13009 WHRoeder 2016.03.25 21:16 #   Entry #5 didn't get saved. I don't know what you are trying to compute; I can't follow your code. Try something like:```double average_body_percent(int shift, int len=14){    double sum=0;    for(int i=shift + len - 1; i >= shift; --i){       double body = MathAbs(Open[i] - Close[i]);       sum += body / (High[i] - Low[i]);    }    return sum / len * 100; }```