Correct Way to Loop Through Last [X] Number of Variable Indexes
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)
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.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.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.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<limit; i++)
You have a look back of 14 ("last 14 values") you must handle the it to avoid error 4002. int counted = IndicatorCounted(); for(int i=Bars -1 -MathMax(14, counted); i >= 0; --i){ :
WHRoeder:
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]?
- The variables passed to OnCalculate, are not guaranteed to be in series order (newest = 0.) The Predefined Variables - MQL4 Reference (e.g. Close[]) are.
- 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.
- You have a empty array, it can't be assigned to. Your function has no loop to fill the array, so can't work.
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.
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; }
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<limit; i++) { ShadowsBuffer[i] = (shadow_height(open[i], high[i], low[i], close[i])/Candle_SMA);//<-----------This should return the SUM of the last [X] values of shadow_height and divide by the input variable Candle_SMA CandlesBuffer[i] = (candle_height(open[i], high[i], low[i], close[i])/Candle_SMA);//<-----------This should return the SUM of the last [X] values of candle_height and divide by the input variable Candle_SMA } //--- return value of prev_calculated for next call return(rates_total); } double candle_height[](double open, double high, double low, double close) { double CH = MathAbs(open - close); double TOC = MathMax(open, close); double BOC = MathMin(open, close); double TS = high - TOC; double BS = BOC - low; double AH = TS + BS + CH; double TSraw = 100 * (TS/AH); double BSraw = 100 * (BS/AH); double CHraw = 100 * (CH/AH); double shadow_height_raw = TSraw - BSraw; double candle_height_raw[] = CHraw - shadow_height_raw; //<------------ Don't know if this is the correct use of the array? for(int p=0; p<Candle_SMA; p++) { candle += candle_height_raw[p]; //<------------The issue is here. I am trying to pull the last 14 values of candle_height_raw and add them together, return the value, and provide an SMA in the code above. } return(candle); } Also, if anyone knows a better way to do what I am trying to do, I'm open for suggestion! Thanks, Cnotey