Algorithmic trading, quantitative analysis and **Zorro Trader**.

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

Forum breadcrumbs - You are here:Algorithmic Trading ForumAlgorithmic Trading: Zorro TraderQuestion about the ADX array

anna_v@anna_v

4 Posts

#1 · May 19, 2022, 4:41 pm

Quote from anna_v on May 19, 2022, 4:41 pmHi, 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 theadx[]array (like in C)? Which would be the very first ADX value in the back-test? I am confused, because in the post you sayadx[0]is the most current bar, which should be the LAST value in theadx[]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.

Click for thumbs down.0Click for thumbs up.0

Algo Mike@algo-mike

18 Posts

#2 · May 19, 2022, 5:19 pm

Quote from Algo Mike on May 19, 2022, 5:19 pmHi 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... atime serieswith the0 indexat thelastelement. 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"

withoutusing 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!

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!

Click for thumbs down.0Click for thumbs up.1

anna_v has reacted to this post.

anna_v

#3 · May 21, 2022, 2:00 pm

Quote from anna_v on May 21, 2022, 2:00 pmThanks 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

areactually stored as arrays? Or are they completely independent data structures?

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?

Click for thumbs down.0Click for thumbs up.1

Algo Mike has reacted to this post.

Algo Mike

Algo Mike@algo-mike

18 Posts

#4 · May 23, 2022, 12:30 pm

Quote from Algo Mike on May 23, 2022, 12:30 pmYes, 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 seriescalculated from the price (like ADX, for example, or a moving average) willchangeevery time a new bar comes in. Anarrayin C (and Lite-C) is by definition a static data structure, so it willnot changewhen new bars come in. You need to take this into consideration when coding your algorithmic trading strategies.

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.

Click for thumbs down.0Click for thumbs up.0

Algo Mike@algo-mike

18 Posts

#5 · July 16, 2022, 1:51 pm

Quote from Algo Mike on July 16, 2022, 1:51 pmAs 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.

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.

Click for thumbs down.0Click for thumbs up.0