Risk warning

All articles, strategies and indicators are just reflecting a single traders opinion and should be viewed as that.
I advice everybody to trade with a DEMO account!

Sunday, June 5, 2011

What are arrays and how to use them

Arrays are used to store items of the same type. The type of an element can be integer, number, bool and so on....Arrays are especially beneficial when you need to store a large amount of data. Before you can use an array you need to declare it. The array can be global or local. The information stored in a global array is kept when the strategy runs from 1 candle to another. If you use a local array the information is lost with each loop of you strategy. Before getting lost in all that technical talk lets give some examples:

declaration examples

integer[16] test;

This creates an array named test that contains 16 elements. Each element has his position. The first position in this array is position 0. The last position in the above array is position 15.

integer[16] [2] test2;

This creates an 2 dimensional array. The name of the array is test2. This array has 32 elements. Imagine an excel sheet with 2 columns and 16 lines.

Bool[4] [4] [4] test3;

This creates a 3 dimensional array. This array has 64 elements. Imagine a cube. The name of this array is test3. Each element of this array contains a bool.
Global or local variable

integer[4] test; => this is a global array because its declared above the strategy name
strategy test;
input period=10;
begin
integer[4] test2; => this is a local array because its declared below the strategy name

Example of putting values in an array

for i:=0 to 5 do
begin
test[i]:=1;
end;

=> this fills the array nammed test with 1's, the array has 6 elements. Remember the first position is 0 not 1!

Example of extracting the biggest value out of an array

biggestvalue:=0;
for i:=0 to 5 do
begin
if test[i]>biggestvalue then biggestvalue:=test[i]
end;

 Arrays are particularly useful in strategies that are using large amount of variables. The use of arrays makes those strategies easier to write , to handle and to understand. Arrays are also ideal if your strategy is based on statistics. You can store the statistics in a array and you can add data while the strategy is running.

4 comments:

  1. Interesting post. Are arrays a recent introduction
    into CTL or have they always been available but not well documented?
    Please excuse my ignorance, lack of knowledge, but can you think of a existing indicator in CTL that would show/demonstrate the usefulness of a 3 dimensional array. Thanks.

    ReplyDelete
  2. I don't know if they existed in the old versions. True. I don't see any use for a 3 dimensional array. That was for demonstration only. I sometimes use the 2 dimensional array in my own strategies. Most of the time when the strategy keeps some kind of statistics to trade on.

    ReplyDelete
  3. I took a look at the chartstudio help file it seems it has changed since I last printed it out.
    The array function detailed below caught my eye.
    Have you used this? Would it allow you to copy series data from different timeframes into
    a indicator?
    Sorry I am being a bit lazy I thought I would ask you the question first before seeing what it actually does.

    Thanks for your advice.


    array_copy_series
    Input: destArray(Number[]), instrument_name(String), symbol_code(Number), timeframe(Integer);
    destArray - Destination array for the function result.
    instrument_name - Name of the currency pair.
    symbol_code - Series identifier.
    timeframe - Requested instrument timeframe (can be one of timeframe constanst).
    Return: Number of copied elements or data_uploading constant if data about requested instrument is not ready.
    Function requests data from the server about particular instrument and copies it to the given destination array. Note that server can't answer immediately and will need some time for prepairing data. During this period function will be returning data_uploading result.

    ReplyDelete
  4. I haven't used it. Looks like you have to store the data in a global array cause you can't load it at each candle. (due to slow loading)

    You can check if the uploading has finished with that data_uploading result. It doesn't mention if data_uploading is a bool or something else. You have to try it out.

    ReplyDelete