Loops

Let's create a strategy which opens a long position if the sum of the last 20 close prices is smaller than the latest close price, multiplied by 20.

Sounds pretty straightforward. Let's see what it looks like:

1 enter long when (close * 20) > close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11] + close[12] + close[13] + close[14] + close[15] + close[16] + close[17] + close[18] + close[19]

This works... however that line is quite long. Imagine if we wanted the sum of the last 200 close prices, instead of 20. That's a lot of typing!

In comes the for loop.

Let's analyze the code above:

  • When calculating the sum, we are basically doing the same thing 20 times - adding two numbers together
  • The only thing that changes is the number of bars we're going back - from 0 to 19.

We could use a for loop to calculate the sum without excessive writing.

Here's what it looks like:

1 sumOfLast20ClosePrices = 0
2
3 for distance from 0 to 19
4 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
5
6 enter long when (close * 20) > sumOfLast20ClosePrices

For loops are a way to instruct QuantScript to do the following:

  • Create a variable with a starting value and an ending value:
    for distance from 0 to 19
  • Execute the code found on subsequent lines which are indented by 2 spaces, incrementing the aforementioned variable along the way, until it reaches its ending value. When it does, the code is executed one last time (the range is inclusive).

In our case, line 4 is being executed 20 times. We're doing 20 additions, just as expected. On each execution, the variable

distance
has a different value, ranging from 0 to 19.

On the first execution of the for loop,

distance
is equal to
0
, so we add the current
close[0]
price to the current sum (which is equal to
0
). On the second execution,
distance
is equal to
1
, so we add the previous
close[1]
price to the sum we have so far (which is equal to
close
). Rinse and repeat 18 more times.

Let's "unfold" the loop to better see what's really happening. The following code mimics what QuantScript does whenever it executes a for loop:

1 sumOfLast20ClosePrices = 0
2
3 # "for loop" starts here
4 # set variable to its starting value
5 distance = 0
6 # execute body
7 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
8 # sum is now equal to close
9
10 # increment variable
11 distance = 1
12 # execute body
13 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
14 # sum is now equal to close + close[1]
15
16 # increment variable
17 distance = 2
18 # execute body
19 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
20 # sum is now equal to close + close[1] + close[2]
21
22 # increment variable
23 distance = 3
24 # execute body
25 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
26 # sum is now equal to close + close[1] + close[2] + close[3]
27
28 # increment variable
29 distance = 4
30 # execute body
31 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
32 # sum is now equal to close + close[1] + close[2] + close[3] + close[4]
33
34 # increment variable
35 distance = 5
36 # execute body
37 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
38 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5]
39
40 # increment variable
41 distance = 6
42 # execute body
43 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
44 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6]
45
46 # increment variable
47 distance = 7
48 # execute body
49 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
50 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7]
51
52 # increment variable
53 distance = 8
54 # execute body
55 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
56 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8]
57
58 # increment variable
59 distance = 9
60 # execute body
61 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
62 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9]
63
64 # increment variable
65 distance = 10
66 # execute body
67 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
68 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10]
69
70 # increment variable
71 distance = 11
72 # execute body
73 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
74 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11]
75
76 # increment variable
77 distance = 12
78 # execute body
79 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
80 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11] + close[12]
81
82 # increment variable
83 distance = 13
84 # execute body
85 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
86 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11] + close[12] + close[13]
87
88 # increment variable
89 distance = 14
90 # execute body
91 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
92 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11] + close[12] + close[13] + close[14]
93
94 # increment variable
95 distance = 15
96 # execute body
97 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
98 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11] + close[12] + close[13] + close[14] + close[15]
99
100 # increment variable
101 distance = 16
102 # execute body
103 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
104 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11] + close[12] + close[13] + close[14] + close[15] + close[16]
105
106 # increment variable
107 distance = 17
108 # execute body
109 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
110 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11] + close[12] + close[13] + close[14] + close[15] + close[16] + close[17]
111
112 # increment variable
113 distance = 18
114 # execute body
115 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
116 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11] + close[12] + close[13] + close[14] + close[15] + close[16] + close[17] + close[18]
117
118 # increment variable
119 distance = 19
120 # execute body
121 sumOfLast20ClosePrices = sumOfLast20ClosePrices + close[distance]
122 # sum is now equal to close + close[1] + close[2] + close[3] + close[4] + close[5] + close[6] + close[7] + close[8] + close[9] + close[10] + close[11] + close[12] + close[13] + close[14] + close[15] + close[16] + close[17] + close[18] + close[19] https://en.wikipedia.org/wiki/The_Game_(mind_game)
123 # "for loop" ends here
124
125 enter long when (close * 20) > sumOfLast20ClosePrices

We now have all the tools we need to write our own QuantScript implementation of a Simple Moving Average indicator.