# Help Me Understand Iterating Over 'N' Periods For Indicator Calculations

28

Hello.

I've been working with some code that does simple things to get a hang of the language and make sure I understand the basic concepts.

For instance, I use the following to iterate over the entire bar history to the most current bar for calculations (see code below).

My question is how to handle going over an entire history of bars, for a given sample period for an indicator doing some calculations along the way -- so I'd need to use a rolling "window" of some kind to do this. I'm not too clear on how to manipulate/use prev_calculated, though I think that is part of the solution.

If anyone could please show me how to construct a simple loop that iterates for say, every 15 bars and does a calculation to display on the indicator plot in a separate window, I'd be very grateful. I think this is the only major stumbling block I have, and it is driving me nuts trying to work with array indexes and getting it all wrong.

int bars = rates_total - 1;

if(prev_calculated > 0) // If we have calculated things before...
{
bars = rates_total - (prev_calculated - 1);
}

for(int i = bars; i >= 0; i--)
{
RangeArray[i] = HighArray[i] - LowArray[i]; // Calculate range

TestIndicatorBuffer[i] = RangeArray[i]; // Set value and plot it -- This works, ranges are being calced and displayed in proper order

}

39020

28

Alain Verleyen:
There are thousands of indicators in the Codebase.

You are correct that there are indicators in the codebase - but none of them seem to have the granular annotations for me to understand it deeply.

I'm sorry I'm not as smart or clever as most of you, I need just a little bit of clarification.

39020

You are correct that there are indicators in the codebase - but none of them seem to have the granular annotations for me to understand it deeply.

I'm sorry I'm not as smart or clever as most of you, I need just a little bit of clarification.

You will get it, hopefully. Sorry but I just can't understand what you need more.
28

Alain Verleyen:
You will get it, hopefully. Sorry but I just can't understand what you need more.

I clearly stated - please show me a basic loop where it iterates over bar history for 'n' defined periods to calculate a custom indicator.

If that is too much, then I guess I'll just stumble through it while you laugh at my lack of knowledge.

39020

I clearly stated - please show me a basic loop where it iterates over bar history for 'n' defined periods to calculate a custom indicator.

If that is too much, then I guess I'll just stumble through it while you laugh at my lack of knowledge.

You are misinterpreting what I wrote.

I am just saying I have no idea how to help you, some hopefully someone will able to do it. It's not about your lack of knowledge, it's about my lack of "teaching" skills.

28

Alain Verleyen:

You are misinterpreting what I wrote.

I am just saying I have no idea how to help you, some hopefully someone will able to do it. It's not about your lack of knowledge, it's about my lack of "teaching" skills.

Fair enough.

An open entreaty then, to those that have mastered this topic to visit upon me a singular simple example of iterating over a given bar history for 'n' periods - I beseech you, out of frustration from having combed through the docs, looked at other indicators (with nearly no programmer comments), and have finally come to these rocky shores to seek redemption.

I feel that I'm on the cusp of understanding, I just need an example that isn't cluttered with trying to calculate an EMA/SMA/FIB/Bollinger/SAR/RSI ... just something dead-simple doing a plain task like calculating ranges, as I've pasted in my original comment.

5624

Fair enough.

An open entreaty then, to those that have mastered this topic to visit upon me a singular simple example of iterating over a given bar history for 'n' periods - I beseech you, out of frustration from having combed through the docs, looked at other indicators (with nearly no programmer comments), and have finally come to these rocky shores to seek redemption.

I feel that I'm on the cusp of understanding, I just need an example that isn't cluttered with trying to calculate an EMA/SMA/FIB/Bollinger/SAR/RSI ... just something dead-simple doing a plain task like calculating ranges, as I've pasted in my original comment.

Is this an MQL4 or MQL5 code?
28

Laszlo Tormasi:
Is this an MQL4 or MQL5 code?

Greetings, I am working with MQL5 hence my query in this forum.

Thanks again

28

Greetings, I am working with MQL5 hence my query in this forum.

Thanks again

Just a polite reminder that I still exist, and my problem still festers.

Hope is a precious commodity that I must cling to.

4945

tradertimm: Just a polite reminder that I still exist, and my problem still festers. Hope is a precious commodity that I must cling to.

In the ".\MQL5\Indicators\Examples" folder, you will find several examples of Indicators provided by MetaQuotes themselves.

Choose one (or more) of the indicators that best fits your needs and study how it works, while always referring to the documentation.

Trying to explain it to you is going to be difficult. It will be easier for you to just "do it" by using one of those Indicators as a "skeleton" or starting point and then you can alter it according to your needs.

After that, you can re-post here with more specific queries about what you did not understand after trying it out yourself.

The following Article may also be of assistance: How to Write an Indicator on the Basis of Another Indicator

How to Write an Indicator on the Basis of Another Indicator
• 2010.07.26
• Dmitry Fedoseev
• www.mql5.com
In MQL5 you can write an indicator both from a scratch and on the basis of another already existing indicator, in-built in the client terminal or a custom one. And here you also have two ways - to improve an indicator by adding new calculations and graphical styles to it , or to use an indicator in-built in the client terminal or a custom one via the iCustom() or IndicatorCreate() functions.