σμ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.7·~30 minBeginner

while loops, break and continue

Skill positionModule 1 of 11 · Topic 7 of 33
1.7 while loops, break and continue
—Lab score— / 100Total XP—Streak—

Repeat while a condition holds

A while loop checks a condition before every pass and runs its block as long as the condition is True. Use it when you do not know in advance how many passes you need: halve an order until it is small enough, wait until a price crosses a level, grow a balance until it reaches a target. Something inside the block has to change the condition, or the loop never ends.

# Halve an order until it is small enough to trade at once
quantity, limit = 1000, 100
steps = 0
while quantity > limit:
    quantity = quantity / 2
    steps += 1
print(steps, quantity)  # 4 62.5

for or while?

Use for when you already have the items or know the count: it cannot run forever. Use while when the stopping point depends on what happens during the loop. A good test: if you can say "for each of these", write for; if you say "until this becomes true", write while.

Make sure the loop can endBefore you run a while loop, check three things: the condition uses a variable the loop changes, the change happens on every pass, and the change moves toward making the condition False. If a loop runs for too long here, it is stopped after 10 seconds.

break: leave the loop now

break ends the loop on the spot and continues with the line after it. It is the way to stop as soon as you have your answer, such as the first price that breaks a limit. Here the loop walks through positions with an index that it increases itself.

# break leaves the loop at once
prices = [184.25, 184.5, 186.0, 185.0]
limit = 185.5
index = 0
while index < len(prices):
    if prices[index] > limit:
        print("breach at", index)  # breach at 2
        break
    index += 1

continue: skip to the next pass

continue skips the rest of the current pass and goes back to the condition. It is handy to ignore bad data without nesting the real work inside an if. In a while loop, change the loop variable before continue; otherwise the loop meets the same item forever.

# continue skips the rest of the pass; step first, or it never moves on
fills = [100, -5, 250, 0, 75]
total = 0
index = 0
while index < len(fills):
    value = fills[index]
    index += 1
    if value <= 0:
        continue
    total += value
print(total)  # 425

Loops that search or converge

Many quant tasks are "keep going until": compounding until a target, refining an estimate until it stops changing, retrying until a request succeeds. while True: with a break in the middle is a common shape when the exit test belongs in the middle of the work.

# How many years until 10,000 grows to 15,000 at 7% a year?
balance, target, rate = 10_000, 15_000, 0.07
years = 0
while balance < target:
    balance = balance * (1 + rate)
    years += 1
print(years)  # 6

# while True + break: the exit test sits in the middle
attempt = 0
while True:
    attempt += 1
    if attempt * 25 >= 100:
        break
print(attempt)  # 4
  • The condition uses a variable that the loop changes.
  • The change happens on every pass, even before a continue.
  • Use break as soon as the answer is known.

Next up: defining and calling functions, to give a block of code a name and reuse it.

Lab · what's graded

  • Count the halvings an order needs with halving_steps25 pts
  • Find the first price above a limit with first_breach25 pts
  • Add up valid fills until the end marker with sum_until_zero25 pts
  • Compound a balance up to a target with years_to_target25 pts
Explorer · lab-7-1
▾ lab-7-1
…
bash · in-browser
learner@quantcraft:~/lab-7-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-7-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