A research-oriented project for transforming market time series into Gramian Angular Field (GAF) images and training Convolutional Neural Networks (CNNs) to classify trading direction from those images.
This repository explores the idea from the paper:
Encoding Candlesticks as Images for Pattern Classification Using Convolutional Neural Networks
In this implementation, OHLC price data is converted into image representations across multiple timeframes, then used to train CNN-based models for LONG/SHORT classification. The repo also includes experiments with CNN+LSTM and hyperparameter tuning.
The project pipeline is roughly:
- Load historical OHLC market data
- Slice data into rolling windows across multiple timeframes
- Convert each window into GAF image representations
- Label samples as LONG or SHORT
- Train a CNN model on the generated image dataset
- Evaluate and export trained models
This repo is best understood as an experimental ML workflow for financial time series classification rather than a polished end-user package.
- Convert candlestick / OHLC time series into GAF-based images
- Build training datasets from multiple timeframes
- Train CNN models for LONG vs SHORT classification
- Experiment with CNN + LSTM
- Run hyperparameter tuning with Keras Tuner / AutoKeras
- Save trained models and run predictions on new data
preprocessing.py— data preprocessing, timeframe splitting, decision mapping, image generationtrain.py— model training, generator creation, evaluation helpers, tuning entry pointspredict.py— inference utilities for prediction on new datacnn_model.py— CNN model definitions and model factory functionssplit.py— logic for splitting time series into windows / timeframeswriter.py— writing generated GAF imagesdata.py— data loading helpersdata_models.py— core profile/mode modelsutil.py— utility functionsconstants.py— project constants and path definitionscnn.ipynb— notebook showing an example end-to-end workflowcnn_lstm.ipynb— notebook exploring a CNN + LSTM architecturetuner.ipynb— notebook for hyperparameter optimization experimentsbacktest.py— backtesting-related experimentationtests/— test filesexports/— generated model artifactsuser_data/— local runtime data and downloaded market data
This project is Python-based and depends on ML / data libraries including:
- pandas
- numpy
- scipy
- keras / tensorflow
- scikit-learn
- pillow
- pyts
- matplotlib
- diskcache
- loguru
- pydantic
- autokeras
It also references local editable dependencies in requirements.txt:
../ftworkdir/lazyft_pkg/../freqtrade/
The project currently assumes a local research environment where those repositories are already cloned and available at the relative paths above.
git clone https://github.com/raphant/cnn_gaf.git
cd cnn_gafpython -m venv .venv
source .venv/bin/activateOn Windows:
.venv\Scripts\activatepip install -r requirements.txtNote: Installation may fail unless the local editable dependencies referenced in
requirements.txt(../ftworkdir/lazyft_pkg/and../freqtrade/) exist on your machine. This project is currently set up for a local research environment and does not yet provide a fully self-contained installation.
LazyFT is used as part of the broader workflow for downloading data and testing strategies.
python train.pyThis uses the default configuration in train.py, which currently trains on BTC/USDT with the following defaults:
- image size:
40 - training timerange:
20170102-20211231 - testing timerange:
20220101- - timeframes:
["1h", "4h", "12h", "1d"]
The notebooks are the easiest place to understand the project end-to-end:
cnn.ipynb— example workflowcnn_lstm.ipynb— CNN + LSTM architecture experimentstuner.ipynb— hyperparameter optimization
This project is for research and experimentation only.
It should not be considered financial advice or a production-ready trading system.
This project is licensed under the MIT License.