After mastering your first steps in programming and understanding functions and variables it’s time to move on and make use of the more advanced data structures in Tradesignal: Arrays
To see the possibilities of the array data structure first let’s recap variables.
Variables are place holders to store the outcome of a calculation. In the example above the value of a moving average is stored in the variable av. To store the outcome of an operation into a variable has got some advantages. First, the calculation has only to be done once. Later on, you just see what is stored in the variable. This saves processing time. The second advantage of variables is, that they store all historic values. As shown in the example above, the average is calculated on every bar on the chart. Its historic values can be referenced by using the square brackets. This makes it easy to compare current results with historic ones.
While variables store every value in history, arrays are more like a storage for specific events. The best analogy to an array would be a locker room.
In the example above I have defined an array called “locker”. It has got 10 spaces available. Using the syntax given above you can store whatever you want in your locker. It is no good programming style to mix numeric and text in one array, but you would have the freedom to do so.
Be careful when you overwrite cells. At the end of the code above there will be nothing in cell 2. The “bag” which has been in there originally is replaced by “0” in line 9. On the other side, if you want to know what has been in cell 7 on the bar before, you can use a similar syntax as with variables. locker will return the moving average’s value 21 bars ago.
Array Example 1: Pattern based Trend Detection
Now that you got the idea of what an array can do, we can go on and do a more realistic example. Therefore, I would like to store the last swing lows of the market and then use these points to decide if the market is in an uptrend or not.
The example above starts by defining an array, but not giving any space declaration to it. The “locker” had fixed 10 spaces to store something in it, the “SwingLows” array will be expanded to create the needed space whenever there is a new Swing Low detected. (line 8) Therefore a counter variable is increased by 1 every time a new swing low is found.
In line 14/15 the actual magic is done. As the array holds all swing lows, you just have to see if the latest swing low is above the swing low in the cell before. Then you have found a rising swing low and can do a simple trend detection.
Array Example 2: Standard Deviation of an Array
As shown above an array is a useful thing when you want to store individual events, in this case the swing lows of the market. Another useful example for “individual events” are the individual trades of your trading strategy. The indicator below will calculate the standard deviation of closed profits and use an array and a pre-defined array function to do so.
If the indicator is put on a “closed equity” line it works as follows: “Price” will be the indicator you drag & drop this indicator on. Line 5 to 9 expands the array every time a new “price” change is detected, and the change is stored into the trades array. Attention: do not get confused in line 8: trades would be the space number 1 in the array trades. price is the value of price on the bar before. Price is a variable; trades is an array. As soon as there are at least 10 stored events the code uses the “standarddevarray” function to calculate the standard deviation of all values stored in the trades array.
On the chart above I applied the indicator two times. The first one is applied on the “closed equity” line, the second is applied on the open equity line. The indicator calculates the standard deviation of all “price” changes. Once it will be all closed trades. The second uses the bar-to-bar rolling price changes. Thus, the first indicator gives you the standard deviation of the closed trades and the second one shows the standard deviation of your bar-to-bar equity changes.
Also have a look at the available functions for arrays. There is way more that can be done with this data structure than I have shown over here with these simple examples.
Please feel free to contact us via email to email@example.com if you need any help in programming in Tradesignal and keep in mind that we can do all the programming for you with our custom programming service.
Tradesignal® is a registered trademark of Tradesignal GmbH. Unauthorized use or misuse is strictly prohibited. Data by Refinitiv Eikon.