A written rule like buy when the close crosses above the average has to become something code can check on every bar. By the end of this lesson, you'll turn a rule into a column of True and False, and find the exact bars where it fires. Then you'll add a filter, and write an exit the same way.
1 of 10
What you'll learn
- Ask one question per row. A comparison gives True or False for every day at once.
- Find a cross. It is above the average today and was not above it the day before.
- Start after the warm-up. The first day with an average can show a false cross.
- Add filters as columns. Each filter is another column, and each one cuts some trades.
- Write exits the same way. An exit fires when the close drops back below the average.
Exercise
- Write one of your own rules as a True or False column on your file.
- Count how often it fires, then look up three of the dates on a chart and check the rule really was met.
Quick check
1. What does ~ mean in pandas code like above & ~was_above?
2. Why might the first signal in a test be false?
3. What does adding a filter column do to the signal count?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
A written rule becomes columns of True and False: is the close above the The average of the last few closing prices, worked out again on every candle and drawn as a line.More in the glossary today, and was it the day before? A cross is above today and not the day before, and filters and exits are more columns.
- Example rule, made up for learning the code: buy when the close crosses above its 20-day average. It isn't a recommendation.
- A question per row: above = df["close"] > df["sma20"] gives True or False for every day at once. A comparison with a blank gives False.
- A cross: was_above = above.shift(1, fill_value=False); df["cross_up"] = above & ~was_above. & means and, ~ means not. .sum() counts the Trues: 60 in the made-up file.
- Warm-up trap: the first day the average exists can show as a false cross, because the day before had no average. Start the test after the warm-up.
- Filter: df["cross_up"] & uptrend, where uptrend = close above its 100-day average (made up): 24 signals. Every filter costs trades (Adding filters). Exit signal: ~above & was_above. Exits at a An order that closes a trade if price reaches a level you set against it. It aims to limit the loss, but in a fast market it can fill at a worse price.More in the glossary or An exit order that closes a trade when price reaches a level you choose in your favour.More in the glossary need the bar-by-bar walk.
Worked example
Made-up numbersFinding crosses by hand
Made-up numbers · five days of closes and averages
- Day 1: 99 vs 100.0, below. Day 2: 101 vs 100.2, above, so day 2 is a cross.
- Day 3: 102 vs 100.5, above, but so was day 2: not a cross.
- Day 4: 100 vs 100.6, below.
- Day 5: 101 vs 100.7, above after below: a cross.
Two signals in five days: a cross needs above today and not above the day before.
Your turn
DrillMade-up numbers · above: False, True, True, False, False, True, True, True
Trading is risky. You can lose money. Test results are hypothetical, and a test can't tell you what will happen next. This lesson explains how testing works. It isn't advice.