Skip to content
AlphaShkPublic

About

WUTIS SS25 team project I led: Keras CNN+GRU forecasting SPY intraday volatility, Optuna tuning, backtrader backtest.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

45 Commits

Folders and files

Repository files navigation

CRNN volatility forecasting (WUTIS SS25 team project)

Team project from the Algorithmic Trading division of WUTIS, a Vienna student investment society, in the 2025 summer semester (pitched in June 2025; pitch slides on wutis.at). A convolutional-recurrent neural network (CRNN) forecasts the short-term volatility of SPY from one-minute bars of SPY and VIXY. A backtrader backtest then trades VIXY on the forecasts. A separate Optuna search looks for intraday trading windows, following a "payday anomaly" idea from the project proposal.

This repository copies the main-branch history of the team repository SukramReburg/wutis-rnn-vol and adds one commit with the post-pitch state (slides and an updated trade log).

Team

Member Role (team slide in source/crnn_slides.pdf)
Denys Shkola Analyst / Project Lead: strategy, task management, implementation
Markus Gruber Fellow Analyst: model training, optimisation, backtesting
Orest Mykhailiuk Fellow Analyst: asset analysis

Denys led the three-person team. He wrote the project proposal and is first author of the two-page report (source/wutis_crnn.pdf, LaTeX source in source/latex/).

Code ownership is shared. By git blame at HEAD, Denys wrote 1,177 of the 2,300 Python lines: the data pipeline, the CRNN model, the training and tuning scripts, the trading model and most of the backtest. Markus Gruber wrote the trading-window search (analysis/trading_window/), the evaluation script and the trade plots, and maintained the upstream repository.

What the code does

Step Files Details
Data data/fetch.py One-minute bars for SPY and VIXY from the Alpaca market-data API, starting in 2022
Features data/indicators.py MA(14), RSI(14), Bollinger bands (20, 2σ), MACD (12/26/9), stochastic oscillator (14, 3, 3), 60-minute rolling volatility of one-minute log returns
Dataset data/preprocess.py Merge on timestamp, 60-minute input windows with 31 features, chronological 70/10/20 train/validation/test split, min-max scaling fitted on the training split. Target: the next value of SPY's 60-minute rolling volatility
Model model/models.py, model/train.py Keras CRNN: three Conv2D + max-pooling blocks over the 60 × 31 window, two GRU(128) layers, Dense(64) and a linear output. MSE loss, Adam optimiser
Tuning model/tuning.py Optuna multi-objective study (MAE, MSE, 1 − MDA) over learning rate and batch size, stored in studies/tuning_crnn.sqlite3
Evaluation model/evaluate.py Error metrics of the saved model on the test split (analysis/model_metrics.yaml)
Trading windows analysis/window_search.py, analysis/trading_window/ Optuna search over the number of days and the intraday window length (15 to 720 minutes). Each window is scored on normalised realised volatility, normalised volume and a window-length term. optim_slots.py also contains a tabular Q-learning agent, but that stage is commented out in the driver script
Backtest trader/backtest.py, trader/trading_model.py backtrader strategy on the test split. It opens a long or short VIXY position when the forecast volatility change crosses a threshold and the last observed change points the other way. Position size scales with the forecast change. trading_model.py also has an AlpacaTradingClient class for an Alpaca paper account, but no script in this repository runs it

The report and the slides describe a broader design, including a Q-learning time-slot agent, streaming data and broker execution. This repository contains the forecasting pipeline, the window search and the backtest. It has no streaming or order-execution loop.

Forecast (pred) vs. realised target (test) on part of the test split, in scaled units

Results (backtest) and caveats

The pitch reported these backtest metrics. They come from backtrader's analyzers and are stored in analysis/results/backtest_metrics.yaml, which has the exact values.

Metric Value
Sharpe ratio 1.11
Win ratio 55.6%
Max drawdown 42.1%

This is a single historical backtest and does not show a tradable edge:

  • No costs. The backtest uses backtrader's default broker: 10,000 starting cash and zero commission, with no slippage or borrow costs. Position sizes are fractional.
  • Synthetic timestamps. The script gives the test split a continuous one-minute index starting at 2024-11-01. Dates in the trade log and the plots are therefore not real trading times. The slides give the underlying test period as 1 Nov 2024 – 27 Jun 2025.
  • Sharpe ratio. This is backtrader's default SharpeRatio analyzer: calendar-year returns, a 1% risk-free rate, no annualisation. The synthetic index spans about 14 weeks across one year-end, so the ratio rests on two partial-year returns. It cannot be compared with a conventional Sharpe ratio from daily returns.
  • Win ratio. This counts backtrader position round-trips. The per-fill log analysis/results/trades.csv has 56,098 rows, and 46.4% of them have a positive P&L.
  • One jump dominates. In trades.csv, 78% of the summed P&L falls on a single synthetic day early in the test period (see analysis/plots/cumulative_pnl.png). The pitch slides note this as well: "Sudden jump caused unexpected huge profit".
  • Not re-run. The last upstream commit is an unfinished stop-loss change. As committed, trader/backtest.py stops at start-up with a TypeError: it passes stop_loss_percent to BacktestModel, whose __init__ does not accept it. The metrics were committed together with that change and have not been re-run for this README.

Model metrics (analysis/model_metrics.yaml)

The file lists MAE 0.089014, MSE 1.0, MDA 0.02474 and SMAPE 58.745524. These values are not what their labels say:

  • model/evaluate.py imports calculate_metrics() from model/tuning.py. That function returns (mae, mda, mse, smape), but the script unpacks the result as mae, mse, mda, smape. The MSE and MDA labels are therefore swapped: 1.0 is the MDA value and 0.02474 is the MSE.
  • The MDA compares the signs of the forecast and target levels rather than of their changes. For a non-negative target such as volatility it is close to 1 almost by construction, so it says nothing about direction.
  • evaluate.py loads scalers/y_scaler.pkl, but data/preprocess.py saves scalers/y_scaler.joblib. With the committed code the scaler is not found, so the errors are in min-max-scaled units rather than volatility units.
  • The target is a 60-minute rolling window moved forward by one minute, so 59 of its 60 returns are already in the input window. This task is much easier than forecasting volatility over a fresh horizon.

How to run

The original project pinned no dependency versions. The scripts import tensorflow (Keras), optuna, backtrader, alpaca-py, pandas, numpy, scikit-learn, scipy, joblib, pyyaml, matplotlib, seaborn, plotly (with kaleido for image export) and ipython.

Alpaca credentials go in config/config.yaml, which git ignores:

alpaca_api:
  api_key: 'your_api_key'
  secret_key: 'your_secret_key'
  base_url: 'https://paper-api.alpaca.markets'

Run each step from the repository root, because the scripts use paths relative to it:

python data/fetch.py             # download one-minute bars (needs config/config.yaml)
python data/indicators.py        # add indicator columns
python data/preprocess.py        # windows, split, scaling -> data/processed/
python model/train.py            # train with config/model_config.yaml
python model/tuning.py           # Optuna study; reuses studies/tuning_crnn.sqlite3
python model/evaluate.py --plot  # test-split metrics for the saved model
python analysis/window_search.py # trading-window search

As committed, trader/backtest.py does not start (see the caveats above). The pipeline has not been re-run for this README.

Repository layout

analysis/   plots, metrics, trading-window search, trade plots
config/     data and model configuration (YAML)
data/       Alpaca download, indicators, preprocessing
model/      CRNN definition, training, Optuna tuning, evaluation; saved model
source/     proposal, report (PDF and LaTeX), pitch slides (PDF and Keynote)
studies/    Optuna studies (SQLite)
trader/     backtrader backtest and trading model

License

MIT, see LICENSE. Copyright the WUTIS CRNN team.


More of Denys Shkola's work: alphashk.github.io

About

WUTIS SS25 team project I led: Keras CNN+GRU forecasting SPY intraday volatility, Optuna tuning, backtrader backtest.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages