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.

In Python variables are assigned values with the equal sign (=). Equality between

two variables is verified by using a double equal sign (==). Jump over to the Python

Shell really quick. I hope you still have it close at hand. There might be a bunch of

stuff in the window, so just scroll down to the very bottom until you find the >>>

prompt. Once there type a = 5 and hit enter. Type a and hit enter. The number 5

should appear at the prompt. You have basically assigned 5 to the variable a. Now

type b = 5 and hit enter. Type a==b and hit enter. The prompt should return with

True. In this case a equals b. Type a!= b and hit enter. The prompt will return

with False, becausea does equal b. Youareaskingifa doesn’t equal b and the

answer is False because it does. Remember to use == to test for equality and !=

for inequality.

The next line of code is very important because you will see it often.

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

This is an example of a definite loop; the number of repetitions is known ahead

of time. This for-loop will iterate across the number of items in the range

from ((curBar − offset) − (lookBack−1) to curBar − offset +1). This only looks

complicated because of the variables in the range function. Here is a more

simplified version of the Python for loop:

173

myList = list()

myList = [1,2,3,4]

for index in range(0,3):

print(myList[index])

The output of this code will print only the first three elements in the list named

myList. The function Range(0,3) returns 0, 1, and 2. The upper bound in the

Range function is not inclusive. So to print out myList in its entirety, you must

use Range(0,4). Referring back to the original bounds in our Range function, let’s

simplify by substituting some numbers for the values. Assume:

curBar = 500

offset = 1

lookback = 20

USING PYTHON TO BACKTEST YOUR ALGORITHM

So, range ((500 − 1) − (20 −1), 500 − 1 + 1) equals range (499 − 19, 500).

The loop will iterate from 480 to 499 because 500 isn’t included. Since 480 is

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!