31.07.2021 Views

Ultimate Algorithmic Trading System

Using automated systems for trading in stock markets

Using automated systems for trading in stock markets

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

AFL uses functions DateNum(), DateTime(), Hour(), Minute(), Second()

to retrieve the date and time of the price bar.

Array programming or vector languages generalize operations on scalars to apply

transparently to vectors, matrices (2d-array), and other higher-dimensional arrays.

Sounds complicated, right? It really isn’t. An array is simply a list of like data. In

most testing platform scripting languages price data is held in arrays. When we code

and use the word high we are usually referring to an array or list of high prices of

the underlying instrument. When we use a subscript in the array, we are referring

to that singular element in the list: High[6] is the high price six bars ago. Most of

the programming discussed thus far in this book has dealt with scalar programing

in a pseudocode framework. The fundamental idea behind array programming is

that operations apply at once to an entire set of data. An example will help clarify.

How do we calculate a 20-day moving average of price bar’s midpoint in a scalar

(bar-by-bar) framework?

sum = 0

for i = 1 to 20

next i

sum = sum + (high[i] + low[i]) / 2 #notice the subscript i

avgMP = sum / 20

This programming structure is known as a for loop. The loop variable is the letter

i and the statement that is indented will be executed 20 times before the next

line of code is processed. The sum variable accumulates the high price over the

past 20 bars. This moving average calculation requires five lines of code. Of course

this code segment could be put into a function and in most testing languages or

scripts it is. However, be it in a function module or inline, the code is executed

on every given bar of data. Here’s how it is done in an array-programming

framework:

avgMP = MA((H + L)/2,20)

A new array labeled avgMP is created and completely loaded with a 20-period

moving average of the price bar’s midpoint. avgMP is not a scalar or single value—it

is a full-blown array just like the High and Low arrays. Array processing eliminates

the need for additional looping and this means much quicker execution. You might

think a language like EasyLanguage is doing the same thing when it invokes the

average function:

119

INTRODUCTION TO AMIBROKER’S AFL

avgMP = average((H + L) / 2,20);

www.rasabourse.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!