Signals tell you when a trade starts. To know how it ends, the code has to walk forward bar by bar, just as you did with bar replay. By the end of this lesson, you'll code that walk and turn each trade into an R result. You'll also know what to do when one bar touches both the stop and the target.
1 of 9
What you'll learn
- Enter at the next open. Set the stop and target, then walk forward bar by bar.
- End at the first level hit. The trade closes at the stop or target it touches first.
- Hold one trade at a time. Jump ahead to the exit bar before looking for the next signal.
- Measure each trade in R. Divide the move by the distance from entry to stop.
- Count the stop when both hit. A bar can't show which came first, so assume the stop.
Exercise
- Run the walk on your own file and print the first five trades.
- Find each one on a chart, and check the exit bar is the first bar that touched the stop or the target.
Quick check
1. A signal fires on a bar's close. Where does this code enter?
2. One bar touches both the stop and the target. What does this code count?
3. Why does run_trade return None sometimes?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
To see how each trade ends, the code enters at the next bar's open, sets the stop and target, and walks forward bar by bar until one is touched. When a bar touches both, it counts the stop.
- Example settings, made up for learning the code: enter at the next bar's open after a signal, 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 1.5 × An indicator that averages recent candles’ true ranges, so it shows how far price has been moving in each candle, in the same units as price.More in the glossary below, An exit order that closes a trade when price reaches a level you choose in your favour.More in the glossary twice that distance above.
- One trade as a function: def run_trade(df, i, ...) sets the entry, stop and target, then walks j from the entry bar onwards. If the low reaches the stop, the trade ends there (or at a worse open after a gap); if the high reaches the target, it ends there. return None means the data ran out with the trade still open, so it's left out.
- The loop: start after the warm-up (row 20), call run_trade on each signal, store each trade, and jump i to the exit bar so only one trade is open at a time. Made-up file: 36 trades.
- A result measured in units of the amount at risk. If a trade risks 4 points, 1R is 4 points and a 6-point gain is +1.5R.More in the glossary: (exit − entry) ÷ (entry − stop) (Measuring results in risk units).
- Both levels in one bar: a bar can't say which came first. This code checks the stop first, the cautious choice. Finer bars can settle it, unless both levels sit inside the same small bar too.
Worked example
Made-up numbersOne trade, walked by hand
Made-up numbers · long at 5,000.00 · ATR 40 · stop 1.5 ATR · target 2 × risk
- Risk 1.5 × 40 = 60 One whole unit of a price. On the S&P 500, a move from 5,000 to 5,001 is one point, and each futures contract sets what a point is worth in dollars.More in the glossary: stop 4,940, target 5,120.
- Bar 1: low 4,972, high 5,031: neither.
- Bar 2: low 4,990, high 5,064: neither.
- Bar 3: high 5,126 reaches 5,120: the trade ends at the target.
120 points won on 60 risked: +2R.
Your turn
DrillMade-up numbers · long at 4,800 · ATR 20 · stop 1.5 ATR · target 2 × risk
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.