σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Python skill›Module 1 · Python foundations for quants›Topic 1.4·~30 minBeginner

Booleans and comparisons

Skill positionModule 1 of 11 · Topic 4 of 33
1.4 Booleans and comparisons
—Lab score— / 100Total XP—Streak—
Lecture0% read

2 min readCode examples: 6

  1. 01True and False
  2. 02Comparison operators
  3. 03Combining conditions: and, or, not
  4. 04Truthiness
  5. 05Comparing floats, and None
01

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.

Python
1
2
3bid, ask = 184.25, 184.5
print(ask > bid)        # True
print(type(ask > bid))  # <class 'bool'>
02

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.

Python
1
2
3
4
5# 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)
03

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".

Python
1
2
3
4
5# 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)  # False

and 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.

Python
1
2
3# Short-circuit: the division never runs when quantity is 0
quantity = 0
print(quantity > 0 and 100 / quantity > 1)  # False
04

Truthiness

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.

Python
1
2
3# 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 True
Return the condition itself

A comparison already is True 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.

05

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".

Python
1
2
3
4
5
6
7# 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)            # True

Next up: conditionals, where a boolean decides which lines of code run: if, elif and else.

Lab · what's graded

4 tasks · 100 pts
  • 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
Explorer · lab-4-1
▾ lab-4-1
…
bash · in-browser
learner@quantcraft:~/lab-4-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-4-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes