Here is something interesting

To add comments, please log in or register
John May
3042
John May  

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:

   iAMA_handle=iAMA(L_my_symbol,L_ama_timeframe,L_ama_period,L_ama_fast_ma_period,L_ama_slow_ma_period,L_ama_shift,L_APPLIED_PRICE);

 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:

   err6=CopyBuffer(iAMA_handle,0,0,5,iAMA_mainbuf);

 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 

   iAMA_mainbuf[0]=1.05
   iAMA_mainbuf[1]=1.04
   iAMA_mainbuf[2]=1.03
   iAMA_mainbuf[3]=1.02
   iAMA_mainbuf[4]=1.01

 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

   ArraySetAsSeries(iAMA_mainbuf,false);
   err6=CopyBuffer(iAMA_handle,0,0,5,iAMA_mainbuf);
   ArraySetAsSeries(iAMA_mainbuf,true);

 Now, my example will reside into the target array like:

   iAMA_mainbuf[0]=1.01
   iAMA_mainbuf[1]=1.02
   iAMA_mainbuf[2]=1.03
   iAMA_mainbuf[3]=1.04
   iAMA_mainbuf[4]=1.05

 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

Achmad Hidayat
12053
Achmad Hidayat  
CappinJack:

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?

To add comments, please log in or register