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.5for 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.
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 += 1continue: 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) # 425Loops 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
breakas 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