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.

going to be the class structure itself. The other arguments are: the data to be used

in the calculations, the lookback period, the current bar, and the offset.

Take a close look at the following code snippet:

upSum = 0.0

dnSum = 0.0

if self.seed == 0:

self.seed = 1

for i in range((curBar - offset) - (lookBack-1),curBar - offset):

if prices[i] > prices[i-1]:

diff1 = prices[i] - prices[i-1]

upSum += diff1

if prices[i] < prices[i-1]:

diff2 = prices[i-1] - prices[i]

dnSum += diff2

self.delta1 = upSum/lookBack

self.delta2 = dnSum/lookBack

else:

The function starts out by setting the variables upSum and dnSum to zero. The

next line of code utilizes the keyword self and accesses the value of seed through

the dot (.) notation. If self.seed is equal to zero, then all the indented code below

the if construct will be executed. If self.seed is equal to zero, the very first line of

code sets self.seed to one. In doing so, these lines of code will only be executed

the very first time the function is called. Notice the variables upSum and dnSum

do not use the dot notation. These variables are not members of the class and are

temporary. They do not need to be remembered from one function call to the next.

By using the dot notation, you are setting the data member to a value that will be

remembered from one call to the next. By the way, a function inside a class is called

a method. This function/method calculates the initial RSI value by looping back

in time and calculating, accumulating, and averaging the up and down sums. The

upSum and dnSum averages are stored in the class data members, self.delta1 and

self.delta2, respectively. The next time this function is called, this portion of the

function will be bypassed, but the data stored in the data members will be available

for future use. The second part of the function:

177

USING PYTHON TO BACKTEST YOUR ALGORITHM

else:

if prices[curBar - offset] > prices[curBar - 1 - offset]:

diff1 = prices[curBar - offset] - prices

[curBar - 1 - offset]

upSum += diff1

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!