I have been coding for quite some time, but today I learned something new.
For all of the experienced programmers out there, this maybe pretty obvious, but for anyone just learning, please read.
When calling an indicator within an EA like this example, you assign a handle of type int:
But in order to extract indicator values for further computation, the next step is to copy the indicator data to a dynamic array, In this case iAMA_mainbuf is my dynamic target array:
Now, you must pay attention to the CopyBuffer command and how it handles things. The documentation is here
What I did not know is that the last indicator value occupies the first element of the target array. So, for example, if you request five candles of indicator data and your indicator returned 1.01 for candle 0, 1.02 for candle 1, 1.03 for candle 2, 1.04 for candle 3, and 1.05 for candle 4. It copies into your target array like this
I did not catch that, and have been wondering why my calculations with the indicator data have been off just a little bit. I have only been working with the past four or five candles so the effect was not that big. It wasn't until I was reading some ZigZag data that it became apparent to me.
To fix this and swap the copied data around, I used the ArraySetAsSeries command. I just surrounded my CopyBuffer command
Now, my example will reside into the target array like:
Works like a charm, and it sure made a difference in my further calculations. Actually, I am almost embarrassed that I did not know this, but learning these little tricks is what keeps us programmers coming back for more.
On a side note, read the article about arrays from Dmitry
Fedoseev, it is very good. Find it here.
So, you've got important points?