= 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# the answer kept for later, not printedprint(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# mean of the last five closesprint(ma) # -> 182.0price =186.00# the newest close, the fifth oneabove = price > ma # holds Trueprint(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.
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# the threshold each close is tested againstsignals = [180.00> level, 182.00> level, 178.00> level, 184.00> level, 186.00> level] # one answer per dayprint(signals) # -> [False, False, False, True, True]print(sum(signals)) # -> 2print(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]).