Before the days of OOEL and more advanced data structures, such as vectors, you had to work with multidimensional arrays.
Recreate the CCI indicator with Multi-D Array
Derive Your Function First
Create the function first by prototyping what the function will need in the formal parameter list (funciton header). The first thing the function will need is the data – here is what it will look like.
- OHLCArray[1,1] = 1210903.00 // DATE
- OHLCArray[1,2] = 4420.25 // OPEN
- OHLCArray[1,3] = 4490.25 // HIGH
- OHLCArray[1,4] = 4410.25 // LOW
- OHLCArray[1,5] = 4480.75 // CLOSE
- OHLCArray[2,1] = 1210904.00 // DATE
- OHLCArray[2,2] = 4470.25 // OPEN
- OHLCArray[2,3] = 4490.25 // HIGH
- OHLCArray[2,4] = 4420.25 // LOW
- OHLCArray[2,5] = 4440.75 // CLOSE
Visualize 2-D Array as a Table
|Column 1||Column 2||Column 3||Column 4||Column 5|
CCI_2D Function Formal Parameter List
2-D Array Must Run Parallels with Actual Data
The rest of the function expects the data to be just like the H, L, C built-in data – so there cannot be gaps. This is very important when you pack the data and you will see this in the function driver code a.k.a an indicator. The data needs to align with the bars. Now if you are using large arrays this can slow things down a bit. You can also shuffle the array and keep the array size to a minimum and I will post how to do this in a post later this week. The CCI doesn’t care about the order of the H,L,C as long as the last N element is the latest values.
Function Driver in the form of an Indicator
Notice lines 16 and 17 where I am plotting both function results – my CCI_2D and CCI. Also notice how I increment numRows on each bar – this is the housekeeping that keeps that array synched with the chart. In the following graphic I use 14 for CCI_2D and 9 for the built-in CCI.
Now the following graphic uses the same length parameters for both functions. Why did just one line show up?
Make Your Unique Coding Replicate a Known Entity – If You Can
Here is where your programming is graded. The replication of the CCI using a 2-D Array instead of the built-in H, L, C data streams, if programmed correctly, should create the exact same results and it does, hence the one line. Big Deal right! Why did I go through all this to do something that was already done? Great programming is not supposed to re-invent the wheel. And we just did exactly that. But read between the lines here. We validated code that packed a 2-D array with data and then passed it to a function that then accessed the data correctly and applied a known formula and compared it to a known entity. So now you have re-usable code for passing a 2-D array to a function. All you have to do is use the template and modify the calculations. Re-inventing the wheel is A-Okay if you are using it as a tool for validation.