An automated cryptocurrency trading system that uses a Convolutional Neural Network (CNN) to predict buy, sell, and hold signals from technical indicator data. Built from scratch in Java with no machine learning libraries.
Disclaimer: This project is a personal educational project and should not be used for live trading with real funds.
CNNTrader treats time-series technical indicator values as a 2D image and feeds them through a custom-built CNN to classify the next price action as Buy, Sell, or Hold. The system fetches historical candlestick data from the Binance API, computes a suite of technical indicators, normalises the values into a matrix, and uses that matrix as input to the neural network.
-
Fetch — Historical OHLCV candlestick data is retrieved from the Binance REST API for a user-selected cryptocurrency and timeframe. Data is cached locally to avoid repeated API calls when training.
-
Compute Indicators — 21 technical indicator values are calculated for every bar using multiple parameter variations:
Indicator Variations RSI 15, 20, 25, 30 SMA 15, 20, 25, 30 MACD (12,26), (14,28), (16,30) EMA 15, 20, 25 Williams %R 14, 18, 22 Stochastic RSI 14, 18, 22 Volume — -
Normalise — All indicator values are min-max normalised to the range [-1, 1].
-
Image Creation — A sliding window across time produces a 21×21 matrix (21 indicators × 21 preceding bars) which acts as the "image" input to the CNN.
-
Labelling — Training labels are generated automatically using a sliding window peak/trough detection algorithm: local minima are labelled Buy, local maxima are labelled Sell, and everything else is Hold.
The CNN is implemented entirely from scratch — forward pass, backpropagation, and all matrix operations — with no external ML libraries.
Input (21×21 matrix)
- Convolution Layer (32 filters, 3×3, stride 1)
- Convolution Layer (64 filters, 3×3, stride 1)
- Max Pooling Layer (3×3 window, stride 2)
- Fully Connected Layer (3 output nodes: Hold / Buy / Sell)
Trained models are serialised to disk and can be reloaded for backtesting or live testing.
The system offers three modes via a CLI menu:
- Training — Train a new model on a user specified date range.
- Backtesting — Load a trained model and simulate trading on historical data. Outputs an equity curve CSV with balance, drawdown, and profit metrics.
- Live Testing — Poll the Binance API at each candle interval and generate signals in real time on a simulated account.
BTC/USDT, ETH/USDT, BNB/USDT, XRP/USDT, SOL/USDT — across 15 timeframes from 1 minute to 1 month.
CNNTrader/
├── src/
│ ├── account/
│ │ ├── Menu.java # CLI entry point and mode selection
│ │ ├── Account.java # Simulated trading account and backtest engine
│ │ └── Trade.java # Individual trade tracking and profit calculation
│ ├── data/
│ │ ├── Image.java # Normalised indicator matrix record
│ │ └── MatrixUtility.java # Matrix operations (add, multiply, convolve helpers)
│ ├── layers/
│ │ ├── Layer.java # Abstract base layer
│ │ ├── ConvolutionLayer.java # Convolution forward pass and backpropagation
│ │ ├── MaxPoolLayer.java # Max pooling forward pass and backpropagation
│ │ └── FullyConnectedLayer.java # Fully connected layer with leaky ReLU
│ ├── market/
│ │ ├── Bar.java # OHLC candlestick representation
│ │ ├── HistoricalData.java # Data fetching, indicator computation, image creation
│ │ └── indicators/
│ │ ├── Indicator.java # Abstract indicator base class
│ │ ├── RSI.java # Relative Strength Index
│ │ ├── SMA.java # Simple Moving Average
│ │ ├── EMA.java # Exponential Moving Average
│ │ ├── MACD.java # Moving Average Convergence Divergence
│ │ ├── WilliamsR.java # Williams Percent Range
│ │ └── StochasticRSI.java # Stochastic RSI
│ ├── network/
│ │ ├── NeuralNetwork.java # Network orchestration, training loop, serialisation
│ │ └── NetworkBuilder.java # Fluent builder for assembling network layers
│ └── utils/
│ └── Types.java # Enums for currency pairs and candlestick intervals
└── pricedata/
└── BTCUSDT_1m.txt # Sample cached price data
- Java 17+
Compile and run account.Menu as the main class:
javac -d out $(find src -name "*.java")
java -cp out account.MenuThe interactive menu will guide you through selecting a mode, currency pair, timeframe, and date range.
- Trained model — Serialised
.serfile saved to Desktop. - Equity curve —
equity.csvsaved to Desktop containing trade number, balance, drawdown percentage, and cumulative profit percentage.