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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

176

This indicator is programmed as a class object. We have ventured over into the

land of objected-oriented programming, but don’t worry; we are just going to work

in an introductory mode to get the job done. You are probably asking yourself, why

didn’t he just program the RSI as a function module? The quick answer is I wanted

to encapsulate the data and functions in one block of code so I could cut down on

using global variables.

The first time an RSI indicator is called, it uses a special calculation to seed the

indicator value. Subsequent calls to the indicator utilize the seed value and a different

set of calculations. So I had to know, within the indicator code, if it was the first

time it was being called. Also, I needed to store the seed and prior values in the class

for subsequent calculations.

By preceding the name of the class object with the keyword class, youare

informing the interpreter that a class definition is about to be defined with the

name that follows the class keyword. In this example, we are defining a class

named rsiClass. The keyword object within parentheses follows the name of the

class. This would be a great time to explain the difference between a class and an

object. A class is a template and an object is an actual thing that reflects the class

template. Think of it like this: A class is blueprint and an object is the house that

is instantiated or created with the use of the blueprint. One blueprint can be used

to create many houses. The following snippet of code informs the interpreter about

data to be stored in the class:

USING PYTHON TO BACKTEST YOUR ALGORITHM

def __init__(self):

self.delta1 = 0

self.delta2 = 0

self.rsi = 0

self.seed = 0

The bit of code, def __init__(self):, is the function name the object itself

calls to set the initial values of the data variables (class members) when the class is

instantiated. The key word self refers to the class itself. The keyword init must be

preceded by two underscores and followed by two underscores. The keyword self

must be followed by a period to access the different data encapsulated in the class.

As you can see from the code, the variables self.delta1, self.delta2, self.rsi,and

self.seed are all initially set to zero.

The next line of code should look familiar—it looks like the functions we

discussed earlier in the chapter.

def calcRsi(self,prices,lookBack,curBar,offset):

The only difference in this function header is the word self in the argument list.

Basically, when the function calcRsi is called, the first argument or parameter is

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!