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.

Python has a ton of reusable code and helper modules. This little bit of code relies

on the built-in csv.Reader. If you noticed the dot notation, then you realize that

csv is class and .Reader is a method from that class. Before using the csv class, it

had to be imported. If you look back at the source code, you will see it is the first

module to be imported. After the file is opened using with open(fileName) as f: ,

the file pointer (f) is passed to the csv.Reader method. The f_csv is then utilized

to read each row of data in the file. The columns in each row are demarked by using

a subscript. So row[0] is the commName, row[1] is the big point value, and row[2]

is the minimum move. Since we are inside a definite for loop, the loops ends when

the last row of data is read. Once the loop has terminated, the file is closed. These

six or seven lines of code open the file, loop through each row of data, read the data

into predefined lists, and then close the file. Pretty simple and concise!

This next snippet of code does a lot and is a little more sophisticated, so only

the most important parts will be discussed.

184

USING PYTHON TO BACKTEST YOUR ALGORITHM

root = tk.Tk()

root.withdraw()

cnt = 0

files = askopenfilenames(filetypes=(('CSV files', '*.txt'),

('All files', '*.*')),

title='Select Markets To Test')

fileList = root.tk.splitlist(files)

fileListLen = len(fileList)

for marketCnt in range(0,fileListLen):

head,tail = os.path.split(fileList[marketCnt])

tempStr = tail[0:2]

for i in range(totComms):

if tempStr == commName[i]:

commIndex = i

newDataClass = marketDataClass()

newDataClass.setDataAttributes(commName[commIndex],

bigPtVal[commIndex],minMove[commIndex])

with open(fileList[marketCnt]) as f:

f_csv = csv.reader(f)

for row in f_csv:

newDataClass.readData(int(row[0]),float(row[1]),

float(row[2]),float(row[3]), float(row[4]),

0.0,0.0)

cnt = cnt + 1

dataClassList.append(newDataClass)

f.close

return(dataClassList)

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!