Skip to main content

Moving-Average Backtest

移動平均バックテストCourse project
Moving-Average Backtest screenshot
One engine object per stock, run in parallel threads

An assignment for the Investment Decisions and Applications course: a simple moving-average strategy written as a backtest and run on 2024 daily data for TSMC, Hon Hai and MediaTek to see where it enters and exits.

Problem

Turn a simple moving-average trading strategy into a repeatable backtest, using 2024 daily data for TSMC, Hon Hai (Foxconn) and MediaTek, to see where the strategy enters and exits and how costs add up.

Key decisions

  1. 01

    One BacktestEngine object per stock

    Cash, holdings, trade records and P&L all live inside the object, with no shared state.

  2. 02

    Backtest several stocks at once with ThreadPoolExecutor

    With no shared state, they can run in parallel without locks.

  3. 03

    Feed the engine one candle at a time

    It simulates only seeing data up to today, so no future prices leak in.

Trade-offs

  • It trades one lot at a time and ignores slippage — a backtesting exercise, not investment advice
  • Revisiting the code in 2026-11 fixed three calculation bugs: no more zero-lot sells recorded when there's no position, sell-side fees and transaction tax are now deducted from the proceeds, and cash is checked before buying

Results

  • Enters and exits on the MA10 and liquidates below the MA60; every trade includes the 0.1425% broker fee and the 0.3% securities transaction tax
  • Three stocks backtested in parallel, outputting final cash, holdings, trades and P&L records
  • Actual 2024 run with NT$1 million: realised P&L was positive for all three stocks, but every win rate was under half (TSMC 9 wins / 11 losses, Hon Hai 4 / 9, MediaTek 1 / 2)
  • TSMC made 41 trades in the year, costing NT$104,541 in fees and tax — about two-thirds of its realised P&L (NT$156,998)

Architecture

one candle at a time

one engine per stock

yfinance
daily data

Tidy columns

Compute MA10 / MA60

Signals
buy / sell / liquidate

Place order
fee + transaction tax

P&L records

ThreadPoolExecutor

one candle at a time

one engine per stock

yfinance
daily data

Tidy columns

Compute MA10 / MA60

Signals
buy / sell / liquidate

Place order
fee + transaction tax

P&L records

ThreadPoolExecutor

Strategy

  • Buy: yesterday's close below the MA10, today's close above it, and no current position
  • Sell: yesterday's close above the MA10, today's close below it
  • Liquidate: sell everything when the close drops below the MA60
  • Costs: 0.1425% broker fee, plus 0.3% securities transaction tax on sells

Design

  • A BacktestEngine class wraps one stock's cash, holdings, trade records and P&L inside the object, with no shared state
  • Because nothing is shared, several stocks can go straight into a ThreadPoolExecutor and run at the same time without locks
  • Candles are fed into the engine one at a time to simulate only seeing data up to today

2024 results

  • Setup: NT$1 million, one lot per buy, daily data for 2024-01-01 to 12-31 (yfinance closes not adjusted for dividends); rerun twice in 2026-11 with identical numbers
  • TSMC (2330): 21 buys and 20 sells, realised P&L +NT$156,998, 9 wins and 11 losses, still holding one lot at year end. Trading costs NT$104,541 (fees 51,519 + tax 53,022)
  • Hon Hai (2317): 13 round trips, realised P&L +NT$31,706 (+3.2% of capital), 4 wins and 9 losses, trading costs NT$12,694
  • MediaTek (2454): its 2024 close ranged from NT$879 to 1,500, so one lot cost close to or over a million — 11 buy signals were skipped for lack of cash; 3 actual round trips, realised P&L +NT$145,737, 1 win and 2 losses
  • What it shows: the MA10 is short, so in sideways markets it keeps trading in and out with mostly small losses, and the money comes from a few big swings — TSMC's two best trades made NT$153,717, 98% of the year's realised P&L; excluding Hon Hai's two best trades, its other 11 lost money
  • The more often it trades, the more costs eat into the result: TSMC's trading costs were about two-thirds of its realised P&L

An assignment for the Investment Decisions and Applications course — a backtesting exercise, not investment advice. The screenshot shows TSMC's actual 2024 buy and sell points, drawn with the program's signal logic.

Type to search projects and posts too