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.

inclusive, the number of iterations turns out to be 20. Count on your fingers if you

want to prove this starting at 480 and going to 499. The loop will start at yesterday

if the offset is one and go back 20 days and sum up whatever data is in the list that

is passed to the function. Once the loop completes, the summation is divided by

the number of days in the calculation—the average of lookback days. This value is

then returned to the calling program. That’s all there is to this function. Sum up all

requested values and divide by the number of requested values. The complicated

part was deciphering the values in the Range function. Notice how the only line

that was indented was result = result + prices[index]. This was because the

for-loop was only controlling this one line of code. The rest of the lines were not

under the influence of the for-loop. They were still indented in relationship to the

def keyword and this was because the lines were encapsulated in the function body.

Can you tell what these other functions are doing?

174

USING PYTHON TO BACKTEST YOUR ALGORITHM

def highest(prices,lookBack,curBar,offset):

result = 0.0

maxVal = 0.00

for index in range((curBar - offset) - (lookBack-1),

curBar - offset + 1):

if prices[index] > maxVal:

result = maxVal

return result

maxVal = prices[index]

def lowest(prices,lookBack,curBar,offset):

result = 0.0

minVal = 9999999.0

for index in range((curBar - offset) - (lookBack-1),

curBar - offset + 1):

if prices[index] < minVal:

result = minVal

return result

minVal = prices[index]

If you guessed calculate the highest high and lowest low of lookback days, then

give yourself a gold star. If not, just go back over the code until you get it. Notice

the indentations and the use of the colon.

rsiClass That wasn’t too bad now, was it? Are you ready to attack the indicators

that are defined as a class? Let’s take a close look at the RSI indicator:

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!