TAYU Academy course material · Python Quant Trading Lab
Text walkthrough
Backtrader architecture
A self-paced text walkthrough, without video or audio. See the article below for worked examples and materials.
Course Notes
1. Understand the core idea of “Backtrader architecture” and explain it in your own words
2. Master this module's focus: Developing strategies with Backtrader
3. Complete the assignment on your own and check it against the reference answer
Backtrader separates the data source, strategy events, execution with the broker, and analyzers. In the next bar, observe the current known bar, default market orders typically simulate a trade at the opening of the next bar, and cannot free-trade based on the known current closing price.
Backtrader's basic structure includes Cerebro (the engine), DataFeed, Strategy (the `next` method), Broker (fees and slippage), and Analyzer (performance analysis). By default, market orders are filled at the opening price of the next candle, which helps to avoid the bias caused by using the current candle's closing price. Backtesting should account for the spread and commissions.
Example: Buy when the Simple Moving Average (SMA) (10) crosses above the SMA (30), and sell when it crosses below, after accounting for commission and slippage, compare the equity curves before and after the cost is included. The cost often significantly reduces the short-term strategy's profitability.
Install dependencies → Load fixed CSV → Add strategy and commission → Record order notifications → Run → Compare results of independent simulator with assumptions.
Run `backtrader_lab.py`, list 10 order events, and the equity at the end, and explain the difference between the equity at the end and the realized profit when there are still positions at the end.
Fixed equity input is approximately 10082.3499777 with 10 events; equity includes the terminal valuation of open positions, not all realized profits. The model is cash-only and does not simulate forex margin.
If the buy signal is generated at 100 on day t, and the opening price on day t+1 is 103 with a closing price of 104, the normal next opening price would be 103, and this cannot be recorded as 100. Costs and slippage should be set and compared with the net result afterward. Orders should not be resubmitted when in the Submitted/Accepted state; the hold flag should be cleared when in Completed/Canceled/Rejected states.
Do not claim to use the default next open execution after closing without enabling cheat-on-close; do not equate the backtest results from different matching agreements directly.
Install the dependencies listed for this lab in the README first. If a dependency or platform is unavailable, mark the lab as not run; downloading the source is not evidence of running it.
python backtrader_lab.py1. Fills at the next candle's open by default
2. Include spread, commission and slippage
3. Use Analyzer to output metrics that can be checked
Risk warning: leveraged trading in forex, precious metals and similar products carries a high level of risk and you may lose all of your capital, or more. This course is for education only. It is not investment advice and does not promise any return. Example prices and contract specifications are teaching assumptions; always use what your own platform discloses. Past performance and backtests do not indicate future results.
All values, prices, accounts and cases in this course are teaching assumptions; the fixed materials can be reproduced offline. Marking a lesson complete only records your study. It is not a certification, a service approval or a promise of returns. Workbooks and answer files are currently provided in Chinese.
Key Takeaways
Completion records your own study, not verified competence. Exercises use fictional data; no real account or deposit is required.