Course contents
Making decisions in code
An if statement lets a program choose what to do based on a condition, the foundation of any rule, such as flagging a day when the price crossed a level. This is where code starts to make decisions.
- Write if, elif, and else branches
- Build a comparison that expresses a simple trading rule
- Combine conditions with and and or
Every program so far has run straight through, doing exactly the same thing each time. To react to data, a program has to make choices: if the price is above its average, do one thing; otherwise, do another. That branching is the job of the if statement, and it is the foundation of every rule you will ever write in code, from a simple flag to a full trading signal.
Choosing with if
An if statement tests a condition, something that is either true or false, and runs a block of code only when it is true, with an optional else block for when it is false. This program checks a price against a moving-average level.
# A condition lets the program choose what to do. (A toy rule, not a signal to trade.)
price = 1402.5
sma = 1390.0 # an illustrative moving-average level
rsi = 72 # an illustrative momentum reading
if price > sma:
print("Price is above the average")
else:
print("Price is at or below the average")
# Combine conditions with 'and' / 'or'.
if price > sma and rsi > 70:
print("Above average, but momentum looks stretched")Price is above the average Above average, but momentum looks stretched
The first block asks whether price > sma. Since 1402.5 is greater than 1390.0, the condition is true, so Python runs the first branch and prints Price is above the average, skipping the else. The comparison uses one of Python's comparison operators: greater than, less than, equal to (written as a double equals, ==), and so on, each producing a boolean, the true-or-false type from earlier. The code inside a branch is indented, and that indentation is how Python knows what belongs to the if, so it matters and is not just for looks.
Combining conditions
Real rules usually depend on more than one thing, and you join conditions with and and or. An and is true only when both parts are true; an or is true when either is. The second block asks whether price > sma and rsi > 70, and since both are true here, it prints its message. Had either part been false, the whole condition would have been false and nothing would have printed. With elif, short for else-if, you can also chain several alternatives, checking a second condition only if the first was false, which is how you express a rule with more than two outcomes.
A rule in code is not a good rule
One honest caveat matters here, and it echoes the first chapter. The moment you write a condition about the market, price > sma, you have written something that looks like a trading rule, and it is tempting to believe that expressing it in code makes it a signal worth acting on. It does not. Code will happily evaluate any condition you give it, sensible or senseless, and being able to write a rule says nothing at all about whether that rule makes money. The example here only describes the price relative to an average; it is deliberately not a recommendation to buy or sell. Whether any such rule has an edge is a question answered by careful testing, and by the hard scrutiny of backtests that the final part of this course covers, not by the fact that Python ran it without complaint. This is education, not advice.
What to carry forward
The if statement lets code choose: it runs a branch when a condition is true, uses else for the false case and elif for more alternatives, and combines conditions with and and or, with indentation marking each branch. This is the basis of every rule you will code. But hold the honest line firmly: a rule you can write is not a rule that works, and only testing, done carefully, can tell the difference.
A condition decides once. To apply a rule across a whole series of prices, you need to repeat it over every item, which is the job of the loop. The next chapter introduces loops.