omarcevi.dev / workshops / pytorch-finance-workshop / 6 · Challenges

Part 6 · Challenges

Goal: Use the leak-free evaluation pipeline we built to try other tickers, a new feature (volume_change), other architectures (GRU, TinyTransformer), and a strategy test with trading costs (15 bps).

Catch-up cell: 🔄 Catch up: Part 6 (prepares every variable for the challenges)

Challenges and solution hints:

  1. Challenge 1 — Try your own ticker: Load data with load_prices("ASELS.IS"), load_prices("BTC-USD") or load_prices("AAPL"). Pass an invalid ticker (load_prices("INVALID_TICKER_XYZ")) and see how the ValueError is caught.
  2. Challenge 2 — Add the trading-volume feature (volume_change): Pass cols_5 = FEATURE_COLS + ["volume_change"] to fit_scaler_on_train and retrain the model with five features (on THYAO.IS test accuracy comes out around 55.7%; compare it with the ±4.8-point uncertainty from Part 5 before calling it an improvement).
  3. Challenge 3 — nn.GRU instead of nn.LSTM, with a 15-day window: Try the GRU model, which has fewer parameters, with train_direction_classifier(..., window_size=15, model_cls=GRUModel) (Test Acc: 52.6%).
  4. Challenge 4 (Bonus) — TinyTransformerModel and a backtest with costs: Call backtest_with_costs(test_rets, cls_res["y_pred"], cost_bps=15.0) and compare buy-and-hold with the strategy before and after a 0.15% commission-and-spread cost on every trade. On the THYAO test window the model is in the market only about one day in five and changes position just 29 times, so costs take little off: buy-and-hold ends at 0.99x, the strategy at 1.24x gross and 1.18x net. Before believing it, ask why: one 22-month window, an accuracy indistinguishable from 50%, and a strategy that mostly sat in cash during a flat market. Rerun it with other seeds, tickers and walk-forward folds.

Troubleshooting (if this step fails):

  • Not enough history for a BIST ticker you picked: Recently listed stocks can have fewer than 120 days of data. Pick an older ticker or lower the min_rows parameter.
  • To see the solution code: Double-click the 💡 Solution cell under each challenge in the notebook to open the code, or simply run it.