6  Appendix

6.1 Python Loops For

The For Loop is a sequence iteration that assigns items in sequence to target one at a time and runs the block of code for each item.

Example. Create a loop for printing each of the elements in list num.

num=[34,56,78,90,35,6]
num
[34, 56, 78, 90, 35, 6]

Doing it manual, the procedure would be:

print(num[0])
print(num[1])
print(num[2])
print(num[3])
print(num[4])
print(num[5])
34
56
78
90
35
6

Now with a Loop for:

for i in num:  # for each i-element inside the object num:
  print(i)# print that i-element
34
56
78
90
35
6

Instead of the letter “i”, we can use other letter, of word. But we have to use that new word inside the loop:

for nu in num:  # for each i-element inside the object num:
  print(nu)#
34
56
78
90
35
6

Example 2. Create a loop to multiply each element in list num and multiply it by 3 and store it in a list.

Doing it manual, the procedure would be:

nu2=[] # is an  empty list, where we want to store the results.
nu2.append(num[0]*3)
nu2.append(num[1]*3)
nu2.append(num[2]*3)
nu2.append(num[3]*3)
nu2.append(num[4]*3)
nu2.append(num[5]*3)
nu2
[102, 168, 234, 270, 105, 18]

Now with a Loop for:

nu2=[] # An empty list, where we want to store the results.
for z in num: # for each z-element inside the object num:
  nu2.append(z*3) # multiply the z-element by 3
nu2
[102, 168, 234, 270, 105, 18]

Some times is helpful using the range() function in Python, which generates a sequence of numbers. The syntax for range() has three possible forms:

range(stop) range(start, stop) range(start, stop, step)

range(10): Generates numbers from 0 to 10 - 1.

for j in range(3):
  print(j)
0
1
2

range(5, 22) Generates numbers from 5 to 22 - 1.

range(7, 14, 2): Generates numbers from 7 to 14 - 1, incrementing by 2.

6.2 Python, conditionals

In Python, conditionals allow you to execute certain blocks of code based on whether a condition is true or false. The primary conditional statements in Python are if, elif, and else.

Here’s how they work: if Statement:

The if statement is used to test a specific condition. If the condition is true, the code block following the if statement will be executed.

For example, create a conditional that prints x is small if x < 50

x = 10
if x<50:
  print("x is small")
x is small

else Statement:

The else statement follows an if or an elif statement and runs a block of code if all preceding conditions are false.

For example, “x is small” if x < 25 and “x is large” otherwise

x = 50
if x<25: # first condition 
  print("x is small")
else: # otherwise
  print("x is large")
x is large

In Python, the elif statement (short for “else if”) allows you to check multiple conditions in a sequence. It follows an if statement and provides an additional condition to test if the previous if condition (or another elif condition) was false.

Example 3. Now that prints “x is small” if x < 25, “x is large” if x > 25

x = 26
if x < 25:
    print('x is small')
elif  x > 25:
    print('x is large')
else:
  print("x=25")
x is large

6.3 The MACD and signals.

According to (Lothian, n.d.), the Moving Average Convergence Divergence (MACD) is a trend-following momentum indicator that shows the relationship between two moving averages of a security’s price. The MACD is calculated by subtracting the 26-period Exponential Moving Average (EMA) from the 12-period EMA.

The result of that calculation is the MACD line. A nine-day EMA of the MACD called the “signal line,” is then plotted with the MACD line, which can be a signal for buy and sell. Traders may buy the security when the MACD crosses above its signal line and sell - or short - the security when the MACD crosses below the signal line.

6.4 The Bollinger Bands

According to (Chen, n.d.), The Bollinger Bands are a technical analysis tool developed by John Bollinger for generating oversold or overbought signals.

There are three lines that compose Bollinger Bands: A simple moving average (middle band) and an upper and lower band.

The upper and lower bands are typically 2 standard deviations +/- from a 20-day simple moving average (which is the center line), but they can be modified.

As the price touches or moves outside the upper band, it could be overbought, suggesting a potential selling or short opportunity. Similarly, if the price touches or falls outside the lower band, the asset may be oversold, indicating a possible buying opportunity.