True and False
A boolean (bool) has exactly two values: True and False, written with a capital letter. Every trading rule ends in one: "is the market crossed?", "can we afford this order?". Comparing two values produces a boolean, so ask > bid is not a decision yet, it is a value you can store, print and combine.
bid, ask = 184.25, 184.5
print(ask > bid) # True
print(type(ask > bid)) # <class 'bool'>Comparison operators
Python compares with == (equal), != (not equal), <, <=, > and >=. A single = is assignment, not comparison, so price = 5 stores a value while price == 5 asks a question. Comparisons can be chained: low <= price <= high reads like the maths and checks both sides at once.
# Comparison operators give True or False
price = 184.4
print(price == 184.4) # True
print(price != 184.25) # True
print(184.25 <= price < 185) # True (chained)Combining conditions: and, or, not
a and b is True only when both are. a or b is True when at least one is. not a flips the value. Together they express real rules, such as "the market is open, the stock is not halted, and we have the cash".
# Combine conditions with and, or, not
market_open, halted, cash = True, False, 500.0
can_buy = market_open and not halted and cash >= 184.5
print(can_buy) # True
print(halted or cash < 100) # Falseand and or short-circuit: Python stops as soon as the answer is known, and does not evaluate the rest. That is useful as a guard, putting the safe check first so the risky one only runs when it is allowed to.
# Short-circuit: the division never runs when quantity is 0
quantity = 0
print(quantity > 0 and 100 / quantity > 1) # FalseTruthiness
Any value can be used as a condition. 0, 0.0, the empty text "" and None count as false; almost everything else counts as true. bool(value) shows how Python reads a value. Be careful: quantity and price > 0 may give back 0 instead of False, so write the comparison out when a real bool is needed.
# Zero, empty text and None count as False
print(bool(0), bool(0.0), bool(""), bool(None)) # False False False False
print(bool(200), bool("AAPL")) # True TrueTrue or False. Write return quantity > 0, not an if that returns True and another branch that returns False: it is shorter and cannot disagree with itself.Comparing floats, and None
== between floats is risky because of rounding error (see integers and floats): use math.isclose for "equal enough". == compares values; the word is asks whether two names point at the very same object, and you only need it for one thing: x is None, to test for "no value".
# Floats need a tolerance; None is tested with "is"
import math
print(0.1 + 0.2 == 0.3) # False
print(math.isclose(0.1 + 0.2, 0.3)) # True
fill_price = None
print(fill_price is None) # TrueNext up: conditionals, where a boolean decides which lines of code run: if, elif and else.
Lab · what's graded
- Detect a crossed market with
is_crossed25 pts - Check a price is inside a band with
within_band25 pts - Combine the trading conditions with
can_trade25 pts - Validate an order with
is_valid_order25 pts