σμ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ᵀμ = μ*∫Σ∂∞λ
σμ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 AcademySkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
Skills
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—

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)  # 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.

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

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.

# 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 itselfA 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.

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)            # True

Next 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
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

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub