I was recently developing code to perform circular indexing of arrays where my need was to calculate the correct circiular index of a bar that was far into the future or far into the past relative to the current bar.
I was perplexed when my code did not produce the desired results. I spent quite a bit of time locating the problem and also learned some new things about formulas for calculating circular indexes that I felt were worth sharing with others.
To create a circular index for an array, you will often see simple code as follows:
vars: index(0), Size(12);
Index = Index + 1
if Index > Size then Index = 1;
vars: index(0), Size(12);
Index = Index - 1;
if Index < 1 then Index = Size;
There are some situations, however, where you wish to calculate the circular index of a point many bars into the future, or many bars into the past.
My initial attempt at creating a more robust formula was:
CircularIndex = MOD(Index - 1, Size) + 1;
This seemed to work fine when tested in Excel, but when using it in EasyLanguage I was getting errors for referencing the Array out of bounds.
So, the first thing I learned about circular indexing is that the MOD function in Excel is NOT the same as the MOD function in EasyLanguage!
With positive offsets, the values are the same:
But with negative offsets, the values are different:
The EasyLanguage MOD function does not work for negative offsets. Therefore, it can not easily be used when backreferencing circular arrays with negative offsets since Arrays can only reference non-negative offsets:
e.g. A, A...etc. but not A[-2]
Fortunately, I did find an EasyLanguage function that did work well:
CircularIndex = Index - Floor((Index - 1)/Size) * Size
This function calculates the circular index from non-circular indices, and is especially useful in situations where the index offset is a very large positive or negative number (several multiples of the Size).
My preference is to use circular indices that do not use the number 0. It is easier to debug code when, for example, calculated element 3 belongs in array rather than in array. This avoids the mental juggling of the offset required when also utilizing the element  to store data.
Secondly, the Array element can often be used to hold "status" information about the array or it's state of processing. When circular arrays are passed to functions for further calculations, having the "status" information in element Array avoids having to create a separate status variable to pass this information.
The following simple example may be used to illustrate the CircularIndex function and the corresponding CircularIndexTest indicator, attached below.
A circular array of size 12 is set up and loaded with data starting with an index of 1 (BarNumber = 1):
What is the correct circular index to use when BarNumber = 4092 ?
The answer is given by the formula:
Suppose you are at the LastBarOnChart in the above case. The BarNumber is now 5027 and you need to know what is the correct circular index to use for a bar that is 1137 bars earlier. The answer is:
It is interesting to note that the correct circular index is given by the SAME FORMULA for both positive and negative numbers! This is because of the unique properties of the floor function when calculated for negative fractions.
For example floor(-1/10) = -1
A test indicator CircularIndexTest is shown here calculating the circular index with Length = 12:
Fig. 1. CircularIndexTest Indicator demonstrating the function CircularIndex
The OutputBar from the above CircularIndexTest Indicator shows:
While this is admittedly a trivial program, I think it is useful to:
Initial posted version: 08/31/09
Latest Update: 01/31/10
*.ELD files are compiled for TS 8.7
All ELD and code text files packaged here:
Users of earlier versions of Tradestation may compile the code
The code may be visualized here: