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.

20120531 921 -408

20120629 -1735 -2143

20120731 955 -1188

One last bit of code that shows off the power of Python follows. After this, I will

demonstrate how to pull all this together and test a trading algorithm.

for i in range(0,len(masterDateList)):

cumuVal = 0

for j in range(0,len(self.systemMarkets)):

skipDay = 0

try:

idx = self.systemMarkets[j].equity.equityDate.

index(masterDateList[i])

except ValueError:

skipDay = 1

if skipDay == 0:

cumuVal += self.systemMarkets[j].equity.

dailyEquityVal[idx]

combinedEquity.append(cumuVal)

This snippet of code is important because two very important concepts are

covered. If you review the code, you will see the keyword try: The code controlled

by try is simply finding if and where the last day of a month is located in a

systemMarket’s equityDate stream. Let’s say we are trying to see if July 31,

2012, is in an equityDate list. If it is, then the variable idx returns the location

in the list. If it’s not located in the list, then an error is returned. This is where

except comes into play. The code controlled by except is only executed if the

method index returns an error. Back to the Python Shell!

Type the following and hit enter after the last line:

>>> a = list()

>>> a = [100,300,200,800]

>>> a.index(300)

1

This example returns the location of the number 300 in the a list. Remember Python

is zero based. The first element is number 0.

193

USING PYTHON TO BACKTEST YOUR ALGORITHM

■ Getting Down to Business

Okay, we now have somewhat of an understanding of how the PSB works. I designed

it to be like one of those calculators or watches that is encased in see-through plastic.

Everything is there if you just look for it. However, just like the watch or calculator,

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!