Value of index parameter in Calculate function

Indicators coded by community members
Message
Author
amberwine
Posts: 17
Joined: Fri Jul 12, 2013 3:47 am

Value of index parameter in Calculate function

#1 Postby amberwine » Thu Jul 18, 2013 6:54 am

I've built the C++ version of the sample Ishimoku indicator and installed it (as Ishimoku2) into FT2. I set a breakpoint at the start of the Calculate function as I wanted to better understand the value and purpose of the index parameter.

To trigger the breakpoint, I made a parameter change. OnParamsChanged was triggered, then Calculate. I would have thought the the index would have been = 0, so that the first bar would be re-calculated for the new parameter value, followed by the second (index = 1), the third, and so on. However, the value of index was large (59427), when looking at AUDUSD between 2001 and June 2003. I'm just trying to understand how this works, with a view to coding my own indicator.

The Indicators API help file says that the Calculate procedure is called for every bar that must be calculated, but it's a bit unclear how index is used. I can see that it's used to identify the bar that should be calculated, but how?

e.g.

1. Is the first bar displayed in the graph given index = 0?

2. Are the index values then assigned in numerically increasing order as you go forward in time. So that the second bar in the graph has index = 1, third: index = 2, etc?

3. Is the large number I got for index likely to be for the final bar, which might be 59427 bars on from the first bar shown on the graph? The graph ran from 2nd Jan 2001 to 16th Jun 2003. If so, why was the index of the last bar given for a Calculate after a parameter change, instead of the first bar?

4. If there are multiple bars to be calculated, are the bars processed by Calculate in sequential time order - i.e. from oldest to most recent? Can I expect that the value of index will increase by 1 for each invocation of Calculate? In the Ishimoku sample it looks like index is decreasing by 1 for each invocation of Calculate, so does this work by starting at the most recent bar and working backwards in time?

5. How is the number of bars requiring calculation determined? The first time through, I imagine that all the bars from the start to the current end of the graph need calculating - is that so? Then I imagine that those values will be stored internally until either the indicator is removed, or its parameters are changed. Is that right? Then if the graph is advanced further into the future, e.g. when running a strategy, i imagine that the indicator will be updated for the new dates (and bars), so Calculate will be called for each new bar. Have I understood this correctly?

FX Helper
Posts: 1479
Joined: Mon Apr 01, 2013 3:55 am

#2 Postby FX Helper » Fri Jul 19, 2013 3:50 am

Hello,

1. The last (the bar on the right) bar has index=0.
2. Indexes are increased when you go back at time. So previous bar has index=1.
3. So the first bar in time will have the biggest index.
4. Yes, bars will processed in sequential time order, but the index doesn't increase with each invokation of Calculate because Calculate is invoked on every tick and one bar can assist of several ticks.
5. When you apply the indicator it's calculating bars from bar with biggest index till bar with index=0 (the bar on the right at the moment) and then after new bars are appearing it just calls Calculate with index=0.

amberwine
Posts: 17
Joined: Fri Jul 12, 2013 3:47 am

#3 Postby amberwine » Fri Jul 19, 2013 6:41 am

Thanks, that's clear.

A couple of follow-up question, if I may...

You point out that each bar can consist of multiple ticks, and presumably the structure of each bar is determined by the selected timeframe. So, if timeframe is set to 4-hours, then there will probably be hundreds of ticks per 4-hourly bar. Is that right? So, if Calculate() is invoked with index=1, does that relate to the previous tick, or to the previous timeframe-period?

Regarding index-buffers. Do the indexed slots in these buffers also relate to bars, rather than ticks? So, does buffer[1] contain data relating to the bar that occured before the bar whose data is stored in buffer[0]?

And also, do buffer-indices for previously-stored values update as bars are added? In other words, if buffer[0] was assigned the value 123 in Calculate(), and then 3 new bars are added and Calculate is invoked again, I would expect that 123 is now in buffer[3]. Is that correct?

Finally, if Calculate() is invoked with index=a ( where a>0), then should one loop around inside Calculate() to process all the values between index = a and index = 0, as in index=a, a-1, a-2, ..., 0, or will Calculate() be invoked separately for each index=a, a-1, a-2, ..., 0, so that only one of these values should be processed on each invocation ?

Many thanks.

FX Helper
Posts: 1479
Joined: Mon Apr 01, 2013 3:55 am

#4 Postby FX Helper » Mon Jul 22, 2013 4:44 am

Hello,

1. On 4-hours timeframe if Calculate() is invoked with index=1, it is related to previous 4-hours bar.
2. Yes, buffer[1] contains data relating to the bar which is previous to the bar whose data is stored in buffer[0]
3. Yes, this is correct. If buffer[0] was assigned the value 123 in Calculate(), and then 3 new bars are added and Calculate is invoked again, the value 123 is now in buffer[3].
4. You don't need to loop through because Calculate() function will be called for each index below zero when you attach the indicator to the chart.


Return to “Indicators”

Who is online

Users browsing this forum: No registered users and 17 guests