Index, usually
Index, that is number, is an identificator of numbered element of any complex entity, such as arrays, lists, series and similar
Often used also "j" and "k" as indexes, especially in two- and three-dimentional computations
Ending indexes usually named as N, M, L respectively
Hi Ais,
Thanks for the help, but how dow I know which position in the index is being used ?
Here is a short example form the code base.
In it the sets the value of 'i', but I don't see anywhere that it is declared and specified as a (type) of variable. thus I would of thought that it would create an error when complied, but it doesn't. For example can I use the actual value of the # of a 'shift' such as '3' rather than 'i'. otherwise how do I know what ''i' is except by declaring it and seting the value for it?
When I encounter square brackets such as 'ma[5]' this is a specificed position in an array (moving average in this case) (or the 'index' position '5' within the array?) isn't it?
I know that the first position in an array is the 'zero' position. I hope I at least get one of these things correct!
I think I need to get clear on the difference between a 'buffer', an 'index' and an 'array' is.
Sorry to ask so many questions in one posting. but is enumerating a timeframe such as using the constant "PERIOD_H4" the the same as just using "240" in something like a moving average is? Are these two readily inerchangable.
I'm hoping at can get at least one of these figured out! To start with anyway! Lol
======================================================
Here's anohter example where they just use 'i' for the # of bars shifted for '0' I would imagine. But not sure how the iMA would know what to use for the vaule for the 'i' though as it isn't defined anywhere?
======================================================
#define major 1
#define minor 0
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_width1 1
#property indicator_width2 1
extern int _Period = 14;
extern int MaxBars = 500;
double sell[];
double buy[];
void init()
{
SetIndexBuffer(0, sell);
SetIndexBuffer(1, buy);
SetIndexEmptyValue(0, 0);
SetIndexEmptyValue(1, 0);
SetIndexStyle(0, DRAW_ARROW);
SetIndexArrow(0, 234);
SetIndexStyle(1, DRAW_ARROW);
SetIndexArrow(1, 233);
}
void start()
{
int counted = IndicatorCounted();
if (counted < 0) return (-1);
if (counted > 0) counted--;
int limit = MathMin(Bars-counted, MaxBars);
double dy = 0;
for (int i=1; i <= 20; i++) {
dy += 0.3*(High[i]-Low[i])/20;
}
for (i=0+_Period; i <= limit+_Period; i++)
{
sell[i] = 0;
buy[i] = 0;
if (MovingAverage(i) == 1) sell[i] = High[i]+dy;
if (MovingAverage(i) == -1) buy[i] = Low[i]-dy;
}
}
int MovingAverage(int i)
{
double ma[3];
int period = _Period;
ma[0] = iMA(NULL,0,period,0,MODE_EMA,PRICE_MEDIAN,i);
ma[1] = iMA(NULL,0,period,0,MODE_EMA,PRICE_MEDIAN,i+1);
ma[2] = iMA(NULL,0,period,0,MODE_EMA,PRICE_MEDIAN,i+2);
int candles = 6;
double min = Low[iLowest(NULL,0,MODE_LOW,candles,i+2)];
double max = High[iHighest(NULL,0,MODE_HIGH,candles,i+2)];
bool horizontal = false;
if(ma[0] < max && ma[0] > min)
{
horizontal = true;
}
if(ma[1] < ma[0] && horizontal == false)
{
return(1);
Print(" ");
}
else if(ma[1] > ma[0] && horizontal == false)
{
return(-1);
}
else
{
return(0);
}
}
Hi FourX
Please take a look at the same slightly restyled code
Best regards
//< Program 79 >////////////////////////////////////////////////////< >
// //< >
//< 1. Constant part 10 >=========================================//< >
// //< >
#property indicator_chart_window //< >
#property indicator_buffers 2 //< >
// //< >
#property indicator_width1 1 //< >
#property indicator_width2 1 //< >
// //< >
#property indicator_color1 Red //< >
#property indicator_color2 Green //< >
// //< >
#define major 1 //< >
#define minor 0 //< >
// //< >
extern int _Period = 14 ; //< >
extern int MaxBars = 500 ; //< >
// //< >
//</1. Constant part 10 >=========================================//< >
// //< >
//< 2. Variable part 69 >=========================================//< >
// //< >
//< 2.1. Data 2 >-------------------------------------------------//< >
// //< >
double sell [ ] ; //< >
double buy [ ] ; //< >
// //< >
//</2.1. Data 2 >-------------------------------------------------//< >
// //< >
//< 2.2. Functions 67/3 >-----------------------------------------//< >
// //< >
//< 2.2.1. Special function init() 11 >```````````````````````````//< >
void init ( ) //< >
{ //< >
SetIndexBuffer ( 0, sell ) ; //< >
SetIndexBuffer ( 1, buy ) ; //< >
SetIndexEmptyValue ( 0, 0 ) ; //< >
SetIndexEmptyValue ( 1, 0 ) ; //< >
SetIndexStyle ( 0, DRAW_ARROW ) ; //< >
SetIndexArrow ( 0, 234 ) ; //< >
SetIndexStyle ( 1, DRAW_ARROW ) ; //< >
SetIndexArrow ( 1, 233 ) ; //< >
} //< >
//</2.2.1. Special function init() 11 >```````````````````````````//< >
// //< >
//< 2.2.2. Special function start() 22 >``````````````````````````//< >
void start ( ) //< >
{ //< >
int counted = IndicatorCounted ( ) ; //< >
if ( counted < 0 ) return ( -1 ) ; //< >
if ( counted > 0 ) counted-- ; //< >
// //< >
int limit = MathMin ( Bars - counted , MaxBars ) ; //< >
// //< >
double dy = 0 ; //< >
// //< >
int i ; //< >
// //< >
for ( i = 1 ; i <= 20 ; i++ ) //< >
{ //< >
dy = dy + 0.3 * ( High [ i ] - Low [ i ] ) / 20 ; //< >
} //< >
// //< >
for ( i = 0 + _Period ; i <= limit + _Period ; i++ ) //< >
{ //< >
// //< >
sell [ i ] = 0 ; //< >
buy [ i ] = 0 ; //< >
// //< >
if ( MovingAverage ( i ) == 1 ) //< >
sell [ i ] = High [ i ] + dy ; //< >
// //< >
if ( MovingAverage ( i ) == -1 ) //< >
buy [ i ] = Low [ i ] - dy ; //< >
// //< >
} //< >
// //< >
} //< >
//</2.2.2. Special function start() 22 >``````````````````````````//< >
// //< >
//< 2.2.3. Function MovingAverage 34 >````````````````````````````//< >
int MovingAverage ( int i ) //< >
{ //< >
// //< >
double ma [ 3 ] ; //< >
// //< >
int period = _Period ; //< >
// //< >
ma [ 0 ] = iMA ( NULL , 0 , period , 0 , //< >
MODE_EMA , PRICE_MEDIAN , i ) ; //< >
// //< >
ma [ 1 ] = iMA ( NULL , 0 , period , 0 , //< >
MODE_EMA , PRICE_MEDIAN , i + 1 ) ; //< >
// //< >
ma [ 2 ] = iMA ( NULL , 0 , period , 0 , //< >
MODE_EMA , PRICE_MEDIAN , i + 2 ) ; //< >
// //< >
int candles = 6 ; //< >
// //< >
double min = Low [ iLowest ( NULL , 0 , MODE_LOW , //< >
candles , i + 2 ) ] ; //< >
// //< >
double max = High [ iHighest ( NULL , 0 , MODE_HIGH , //< >
candles , i + 2 ) ] ; //< >
// //< >
bool horizontal = false ; //< >
// //< >
if ( ma [ 0 ] < max && ma [ 0 ] > min ) //< >
{ //< >
horizontal = true ; //< >
} //< >
// //< >
if ( ma [ 1 ] < ma [ 0 ] && horizontal == false ) //< >
{ //< >
return ( 1 ) ; //< >
Print ( " " ) ; //< >
} //< >
else if ( ma [ 1 ] > ma [ 0 ] && horizontal == false) //< >
{ //< >
return ( -1 ) ; //< >
} //< >
else //< >
{ //< >
return ( 0 ) ; //< >
} //< >
// //< >
} //< >
//</2.2.3. Function MovingAverage 34 >````````````````````````````//< >
// //< >
//</2.2. Functions 67/3 >-----------------------------------------//< >
// //< >
//</2. Variable part 69 >=========================================//< >
// //< >
//</Program 79 >////////////////////////////////////////////////////< >
Programmers tend to use "i" as the name of the index variable in loops due to the fact that many of us saw our first loops about 30 yrs ago written as "FOR I=1 TO I =20, blah blah, NEXT I".
In your code example above "i" is just a plain old integer-type variable and is declared within the function in which it is to be used. "for int i=..."
You will declare variables outside of functions when they need to carry information between functions for you.
CB
Hi
There are two cycles "for (..."
Statement "for ( int i=..." is possible for one of them only
And
int MovingAverage ( int i ) //< >
^ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Best regards
Thanks everyone
I recognize the old loop forms. The 'GoTo' as was found in some 'unstructured' languages like the early basics. They were handy, but could sure create an unbelievable hodge poge with them very easily.
I rermember having only 4 lines of codes with multiple GoTo's in it and even with the tracer on and single stepping through it, I couldn't figure out all of what it was doing. I guess if I would of had a computer to help me.... LoL
So it's just another variable that has become a defacto standard then?
I though perhaps it might have something to do with all of the 'inidcator' functions specificall which of course only made it more confusing for me. you think they do stuff like this just to be more 'challenges for new comers?
Well at least I can say that I had a breif moment of clarity until the bubble poped on me! Lol
Thanks everyone
I recognize the old loop forms. The 'GoTo' as was found in some 'unstructured' languages like the early basics. They were handy, but could sure create an unbelievable hodge poge with them very easily.
I rermember having only 4 lines of codes with multiple GoTo's in it and even with the tracer on and single stepping through it, I couldn't figure out all of what it was doing. I guess if I would of had a computer to help me.... LoL
So it's just another variable that has become a defacto standard then?
I though perhaps it might have something to do with all of the 'inidcator' functions specificall which of course only made it more confusing for me. you think they do stuff like this just to be more 'challenges for new comers?
Well at least I can say that I had a breif moment of clarity until the bubble poped on me! Lol
The variable "i" is the standard across pretty much every language i've seen (I use python/php/c++/c regularly and have seen it used in many other places). I can probably guess what happened with the above code...whoever wrote it used something that is very common in c++, probably got yelled at by the MQL4 compiler, and then removed the second declaration of "int i" so his thing would compile. The problem with the above setup is that it isn't very explicit and if you aren't already aware of the issue...can make things confusing. With that said, I don't think anyone is doing it to make things harder. If anything I think it's just one of the quirks of MQL4's attempt to mimic c++. I have been programming in c++ for many years and I continually get hit by small things such as this.
I'm not sure how curious you really are, but if you'd like to know more about the issue...
The scoping rules of MQL4 and c++ are slightly different when it comes to for loops (and probably other loops as well -- I just haven't noticed personally).
- C++: variables declared within for(..) loop calls, such as for(int i=0; ...) are treated as if they were created within the actual curly braces of the for loop. As a result, when the for loop finishes/terminates, the values of any local variables, such as i, are destroyed. This allows/requires you to either declare the variable outside of all of the for loops (and share access to the same physical variable in memory across many loops -- which can be a problem with nested loops), OR you can simply declare it within the for loop and let the compiler do the rest.
//VALID in C++ for(int i=0; i<10; i++) { ... } for(int i=0; i<10; i++) { ... } //INVALID in C++ for(int i=0; i<10; i++) { ... } for(i=0; i<10; i++) //need to have "int i" because i has already been deleted from the previous for loop and doesn't exist anymore { ... }
- MQL4: Unfortunately, MQL4 is slightly different. It operates more in line with what C++ USED to do (i.e. look up information on google regarding for loop scoping issues with visual c++ 6.0 if I remember correctly). Essentially, in this case the variable is automated created outside of the for loop's scope, therefore making it illegal to have multiple for loops that declare the same index variable.
//INVALID in MQL4 for(int i=0; i<10; i++) { ... } for(int i=0; i<10; i++) //produces an error since it thinks you have declared i twice! { ... } //VALID in MQL4 for(int i=0; i<10; i++) { ... } for(i=0; i<10; i++) //CAN'T have "int i" { ... }
The way I handle it is to just make sure I declare the for loop index outside of the for loop itself so that i know explicitly what is going on (this prevents me from forgetting about it). As far as worrying about it being special or anything...it's nothing more than the standard way of iterating through an element with a numerical index. You could rename the variable to "myIndex" or "spiffyIndex" or "BAROFDEATH" and it should still work provided you change all of the references. I personally use barIndex in some cases and just i in others. It really depends on what I'm doing/iterating through/etc.
Hope this helps ;D I haven't verified the above within the past 10min as a proof of concept, but I know I have encountered it in the past.
Good luck :)
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm trying to figuer out thy the letter 'i' is used frequently instead of a numverical value?
Is the 'i' a standardized variable or constant or ????
Is it indicitave of an 'interval' or ???
If this is so, how does one know what that interval is refering to if there is no 'int i = 5; //etc?'