This repository contains small, runnable machine learning (ML) and neural network (NN) examples for stock classification. Each example focuses on the essential parts of a technique: loading data, training a model, evaluating it, and ranking the latest observations by predicted score.
Educational use only: These examples are demonstrations, not investment advice or evidence of a profitable strategy. Classification scores do not account for slippage, liquidity, taxes, market impact, or regime changes.
- Genetic algorithm
- Gradient boosting
- K-means clustering
- Logistic regression
- Random forest
- Support vector machine (SVM)
- Feed-forward neural network (FFNN)
- Long short-term memory network (LSTM)
- Recurrent neural network (RNN)
The neural network examples use Keras, PyTorch, PyTorch Lightning, and
TensorFlow. Browse the implementations in
simple_examples.
Python 3.10, 3.11, or 3.12 is required.
- Clone this repository and change to its directory.
- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate- Install libraries:
pip install -r requirements.txt
- Download the starter data and place the
extracted
example_datadirectory at the repository root:
example_data/
├── latest.csv
├── test.csv
└── train.csv
- Run an example from the repository root:
python -m simple_examples.machine_learning.logistic_regression
python -m simple_examples.neural_networks.pytorch_ffnnThe neural network tuner supports configurable grid and random searches and uses multiple processes to evaluate configurations.
- Edit
hyperparameter_tuning/config.py. - From the repository root, run:
python -m hyperparameter_tuning.hyper_mainThe tuner uses the chronological tail of train.csv for model selection and
does not inspect test.csv. Configure VALIDATION_FRACTION and EMBARGO_ROWS
in config.py. Results are written to the results directory.
See the hyperparameter tuning guide for the search strategies and available settings.
The examples use the D.AT example dataset.
The dataset contains five years of price data for S&P 500 companies, divided into 30-trading-day windows. Values within each window are divided by the most recent value in that window. A row is labeled positive when the stock gains at least 5% during the following 10 trading days.
train.csvcontains the first four years of labeled data.test.csvcontains the final year of labeled data.latest.csvcontains one unlabeled row per stock for generating current rankings. Its first column contains the ticker symbol.
train.csvandtest.csvhave no header. Every column except the last is a numeric feature; the last column is the binary label (0or1).latest.csvhas a header. Its first column is the ticker and its remaining columns must match the training features in number and order.- Labeled rows must be sorted chronologically. If nearby windows share
observations, set
EMBARGO_ROWSso training and validation cannot overlap. Retain ticker/date metadata upstream so leakage can be audited. - Reserve
test.csvfor evaluation after model and threshold selection. Repeatedly choosing models from test results leaks information.
The examples print precision, recall, F1, balanced accuracy, coverage, and a
Fisher exact-test p-value. For a trading study, provide realized returns to
evaluation.backtest_top_scores and choose realistic transaction costs.
The example dataset is static and does not contain current prices. You can download recent customizable data from D.AT with additional strategy and feature-engineering options.