Skip to content

Repository files navigation

Stock Prediction with Neural Networks and Machine Learning (Python)

License

Stock prediction neural network and machine learning examples

Contents

Simple Examples

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.

Machine learning examples

  • Genetic algorithm
  • Gradient boosting
  • K-means clustering
  • Logistic regression
  • Random forest
  • Support vector machine (SVM)

Neural network architectures

  • 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.

Getting Started

Python 3.10, 3.11, or 3.12 is required.

  1. Clone this repository and change to its directory.
  2. Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate
  1. Install libraries:

pip install -r requirements.txt

  1. Download the starter data and place the extracted example_data directory at the repository root:
example_data/
├── latest.csv
├── test.csv
└── train.csv
  1. Run an example from the repository root:
python -m simple_examples.machine_learning.logistic_regression
python -m simple_examples.neural_networks.pytorch_ffnn

Hyperparameter Optimization

The neural network tuner supports configurable grid and random searches and uses multiple processes to evaluate configurations.

  1. Edit hyperparameter_tuning/config.py.
  2. From the repository root, run:
python -m hyperparameter_tuning.hyper_main

The 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.

About the Example Stock Data

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.csv contains the first four years of labeled data.
  • test.csv contains the final year of labeled data.
  • latest.csv contains one unlabeled row per stock for generating current rankings. Its first column contains the ticker symbol.

CSV schema and ordering

  • train.csv and test.csv have no header. Every column except the last is a numeric feature; the last column is the binary label (0 or 1).
  • latest.csv has 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_ROWS so training and validation cannot overlap. Retain ticker/date metadata upstream so leakage can be audited.
  • Reserve test.csv for 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.

Getting new data

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.

About

Examples of python neural net and ML stock prediction methods with sample stock data.

Topics

Resources

Stars

274 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages