TAYU Academy course material · Python Quant Trading Lab
Course Notes
1. Understand the core idea of “Order and position management” 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
The states of an order—Submitted, Accepted, Completed, Canceled, and Rejected—can differ. Only looking at positions might miss in-progress orders. Repeatedly sending the same order can lead to out-of-control size with Next.
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.
Save the pending flag → clear it when the callback reports a final state → update the actual size after a fill → record the reason for a rejection → reconcile first when the status is unknown.
A buy was sent on this bar and the next bar hasn't confirmed it; the strategy sees the signal again. Should it buy again? Design a minimal state table.
Conditional repetition should not occur; distinguish between pending, in progress, executed, and failed states. Signals going in the same direction do not necessarily mean additional positions should be added. The quantity should be based on the actual holdings and the difference from the target.
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.