{ function: _CircularIndex Description: Calculates the circular index from non-circular indices, including situations where the index is a very large positive or negative number. My preference is to use circular indices that do not use the number 0. It is easier to debug code when calculated element 3 belongs in array[3] than always juggling the offset when using the A[0] position for data. Secondly, the Array[0] can often be used to hold "status" information about the array or is processing. When these arrays are passed to functions for further calculations, having the "status" information in element Array[0] avoids having to create a separate status variable to pass this information. For example, a circular array of size 12 is set up and loaded with data starting with an index of 1 (BarNumber = 1) etc. BarNumber = 1: Array[1] = close; BarNumber = 2: Array[2] = close; ..... ..... BarNumber = 12: Array[12] = close; BarNumber = 13: Array[1] = close; etc. What is the correct circular index to use when BarNumber = 4092 ? The answer is given by the formula: CircularIndex = Index - floor((index - 1)/size) * size; = 4092 - floor((4092 - 1)/12) * 12 = 4092 - 340 * 12 = 4092 - 4080 = 12 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: Index = 5027 - 1137 = 3890 CircularIndex = 3890 - floor((3890 - 1)/12) * 12 = 3890 - 324 * 12 = 3890 - 3888 = 2 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 It is also worth noting that the behavior of the MOD function in Excel is different from the MOD function in Tradestation Easylanguage when negative offsets are involved: Excel: MOD(-1, 10) = 9 MOD(-11, 10) = 9 Mod(-21, 10) = 9 EasyLanguage: MOD(-1, 10) = -1 MOD(-11, 10) = -11 MOD(-21, 10) = -21 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[0], A[1]...etc. but not A[-2]. Author: MarkSanDiego Updated: 08/30/09 original version } inputs: Index(NumericRef), Size(NumericRef); _CircularIndex = Index - floor((Index - 1)/Size) * Size;