What is True and False?

Compare two numbers, store the answer, and build a buy rule out of it.

True and False are the two answers Python gives when you compare values. The type is called a boolean, or bool for short.

In this lesson I compare a price to its moving average, then stack several comparisons into one buy rule and count how many days it fires.

Step 1. Compare two numbers

The comparison operators are > greater than, < less than, >= at least, <= at most, == equal to, and != not equal to. Each one returns True or False.

price = 186.00

print(price > 182.00)         # -> True
print(price < 182.00)         # -> False
print(price >= 186.00)        # -> True
print(price == 186.00)        # -> True
print(price != 186.00)        # -> False
print(type(price > 182.00))   # -> <class 'bool'>
True
False
True
True
False
<class 'bool'>

= stores a value. == compares two values. price = 182.00 would replace the price; price == 182.00 asks whether it equals 182.

You can store an answer like any other value:

above = price > 182.00
print(above)                  # -> True
True

Step 2. Is the price above its moving average?

A moving average is the mean of the last few closing prices. Here are five closes, their average, and the comparison:

ma = (180.00 + 182.00 + 178.00 + 184.00 + 186.00) / 5
print(ma)                     # -> 182.0

price = 186.00
above = price > ma
print(above)                  # -> True
182.0
True

The last close sits above the five day average, so above is True.

Step 3. Combine conditions with and, or, not

and is True when both sides are True. or is True when at least one side is. not flips an answer.

print(True and False)         # -> False
print(True or False)          # -> True
print(not True)               # -> False
False
True
False

The same operators join two comparisons about a stock:

volume = 4_200_000
rsi    = 64

liquid         = volume > 1_000_000    # holds True
not_overbought = rsi < 70              # holds True
new_high       = price > 190.00        # holds False

print(above and liquid)                # -> True
print(above and new_high)              # -> False
print(above or new_high)               # -> True
print(not new_high)                    # -> True
True
False
True
True

Chaining them with and gives one rule:

buy = above and liquid and not_overbought
print(buy)                             # -> True
True

Raise the RSI above 70 and the last condition turns False, so the whole chain turns False:

rsi            = 82
not_overbought = rsi < 70              # holds False

print(above and liquid and not_overbought)   # -> False
False

Step 4. True equals 1

Python treats True as 1 and False as 0, so you can add booleans.

print(True == 1)              # -> True
print(False == 0)             # -> True
print(True + True)            # -> 2
print(int(above))             # -> 1
True
True
2
1

Put one comparison per day in a list and sum counts the days the answer was True. The list below asks, for each of the five closes, whether it closed above 182.00:

level = 182.00

signals = [180.00 > level, 182.00 > level, 178.00 > level, 184.00 > level, 186.00 > level]

print(signals)                         # -> [False, False, False, True, True]
print(sum(signals))                    # -> 2
print(sum(signals) / len(signals))     # -> 0.4
[False, False, False, True, True]
2
0.4

Two of the five days closed above 182.00.

Your turn

A stock trades at 179.50, its five day average is 182.00, and volume is 4,200,000. Build above and liquid, print above and liquid, then print sum([above, liquid]).

price  = 179.50
ma     = 182.00
volume = 4_200_000

above  = price > ma                    # holds False
liquid = volume > 1_000_000            # holds True

print(above and liquid)                # -> False
print(sum([above, liquid]))            # -> 1

One False is enough to make an and chain False. The sum still counts the one condition that passed.