Forum

Algorithmic trading, quantitative analysis and Zorro Trader.

Please ask all Zorro Trader questions in the Zorro Trader forum. You need to sign up first.

Please or Register to create posts and topics.

Question about the ADX array

Hi, I have a question about this post:

https://algotrading.pro/zorro-trader/zorro-trader-trend-following-in-mean-reverting-assets/

Please explain in more detail how the indexing works here. Isn't adx[0] supposed to be the FIRST value in the adx[] array (like in C)? Which would be the very first ADX value in the back-test? I am confused, because in the post you say adx[0] is the most current bar, which should be the LAST value in the adx[] array.

Hi anna-v,

I will provide a more general answer here, as it may help users with less programming experience. You are correct, in the C programming language the index works exactly as you described. For example, let's define and initialize an array of 5 integers in C.

int arr[5] = {12, 8, 6, 11, 3};

The indexes are as follows:

arr[0] = 12;
arr[1] = 8;
arr[2] = 6;
arr[3] = 11;
arr[4] = 3;

If these were stock prices, in C the last price (today at close) would be arr[4] = 3. But... how would you know the index (4) in advance, in order to use it? There may be years of historical data in the array. What would be the index of "today"?

C does not provide a built-in way to get the size of an array. Sure, you could use the sizeof operator, like this:

int size = sizeof(arr) / sizeof(arr[0]);

But this is more work, so the creators of Zorro Trader decided to make our lives easier. When you create an array using the internal Zorro Trader series() function, what you get is NOT a regular C array, but... a time series with the 0 index at the last element. So you know that at index 0 you always find the most recent value of the time series. The rest of the indexes go backwards incrementally. In our example from the post, adx[0] is the value of the ADX today, adx[1] is the value of yesterday, and so on and so forth. Much easier to use for our purposes than a regular C array!

Please note that if you define an "array" without using the series() function, you will get a "normally" indexed C array, like the one we initialized in the example above. The first element will be at index 0, and you can do with it anything you can do with a regular C array. You can read more about this subject here:

Zorro Trader series() function

Hope this helps, and happy trading!

anna_v has reacted to this post.
anna_v

Thanks for the detailed explanation. So I can use both C arrays and time series in the same script? Is this "safe"? It appears from the documentation that time series are actually stored as arrays? Or are they completely independent data structures?

Algo Mike has reacted to this post.
Algo Mike

Yes, you can use them both in the same script. There is no problem with doing that, and sometimes you can't even avoid it. However, please note that a time series calculated from the price (like ADX, for example, or a moving average) will change every time a new bar comes in. An array in C (and Lite-C) is by definition a static data structure, so it will not change when new bars come in. You need to take this into consideration when coding your algorithmic trading strategies.

As a general recommendation, always use time series for historical price and volume data, technical indicators etc. Arrays are more useful for trade management functions, lists of assets and any other situation when the size of the data structure is known in advance.