Applying MLP and CNN-GAF architectures to Bitcoin price forecasting and direction classification.
This project applies deep learning models to financial time series using Bitcoin (BTC-USD) daily closing prices from January 2018 to June 2023. The central question is: does the way you preprocess financial data affect deep learning model performance?
Three data representations are compared across two model architectures:
| Representation | Description |
|---|---|
| Price Levels | Raw closing prices - non-stationary |
| Log-Returns | ln(Pt / Pt-1) - stationary transformation |
| Fractional Differencing | Lopez de Prado method - stationary with memory preservation |
The raw BTC-USD price series exhibits strong non-stationarity with a clear upward trend over the analysis period. The ADF test confirms non-stationarity (p-value > 0.05), indicating the presence of a unit root. The ACF shows very slow decay, characteristic of a random walk process. The distribution is highly right-skewed with heavy tails, reflecting Bitcoin's explosive growth periods.
Log-returns are computed as r_t = ln(P_t / P_{t-1}). This successfully transforms the series into a stationary representation, confirmed by the ADF test (p-value < 0.05). The series fluctuates around zero with no discernible trend. However, this transformation removes all memory of the original price levels, potentially discarding valuable long-term information. The distribution shows excess kurtosis, indicating fat tails typical of financial returns.
Fractional differencing is implemented using the fixed-width window method (Lopez de Prado, Advances in Financial Machine Learning, Ch. 5). The optimal differencing order d* is found by searching for the minimum d where the ADF p-value < 0.05. This approach achieves stationarity while maintaining a higher correlation with the original price levels compared to log-returns, suggesting better preservation of long-term dependencies.
| Representation | Stationarity | Memory | Suitable For |
|---|---|---|---|
| Price Levels | Non-stationary | Full | Derivative pricing only |
| Log-Returns | Stationary | None | Short-term forecasting |
| Fractional Differencing | Stationary | Partial | Long-horizon forecasting |
Each MLP uses a rolling window of 10 lags to predict the next value (regression task). An 80/20 train-test split preserving time order is applied. MinMaxScaler normalizes inputs. Architecture: Dense(64) → Dropout → Dense(32) → Dropout → Dense(1). Early stopping prevents overfitting.
MLP on Price Levels: Training on non-stationary price levels produces misleadingly good R-squared values because the model learns to predict the trend rather than the actual dynamics. This approach fails to generalize as the model captures spurious correlations inherent in random walk processes.
MLP on Log-Returns: Using stationary log-returns provides more reliable forecasts. The model learns the actual return dynamics rather than trend-following behavior. The lower R-squared is preferable as it reflects realistic forecasting capability in financial markets.
MLP on Fractionally Differenced Series: This approach balances stationarity with memory preservation. Results typically fall between the levels and log-returns models, capturing some long-term dependencies while maintaining stationarity.
Takeaway: Non-stationary inputs create unreliable forecasts that appear accurate but fail in practice. Stationary transformations are essential for robust model generalization. For short-term forecasting, log-returns provide the most stable and generalizable results. For longer horizons where memory effects matter, fractional differencing offers a better balance.
Time series windows are encoded into 2D image representations using the Gramian Angular Field transformation (via pyts). The classification objective is to predict whether the next price will be higher (1) or lower/equal (0) than the current price. CNN architecture: Conv2D → MaxPooling2D → Conv2D → MaxPooling2D → Flatten → Dense(64) → Dropout → Dense(1, sigmoid).
CNN on Price Levels with GAF: The CNN trained on GAF images from price levels shows moderate performance. The non-stationary nature of the underlying data introduces noise, but the GAF transformation helps the CNN identify some patterns.
MLP on Log-Returns with GAF: The GAF images are flattened into a 576-feature vector and fed to an MLP (Dense(256) → Dropout → Dense(128) → Dropout → Dense(64) → Dense(1)). Using stationary log-returns improves performance and allows the model to focus on directional patterns rather than being influenced by price levels.
CNN on Fractionally Differenced with GAF: This representation often achieves the best balance, combining stationarity with preserved memory. The CNN captures both short-term directional patterns and longer-term dependencies.
Takeaway: GAF-CNN models are computationally more intensive than MLPs but can capture complex spatial patterns invisible to flat-vector models. Stationary inputs consistently improve model reliability, and the image-based approach is particularly advantageous for classification tasks.
MLP receives a flat vector of inputs, either lagged time series values or flattened GAF images. All features are treated independently through fully connected layers with no inherent awareness of sequential order or spatial structure. It is fast, simple, and well-suited for regression on stationary inputs.
CNN receives 2D GAF-encoded images and applies convolutional filters that scan local regions, detecting spatial patterns such as angular shapes and textures corresponding to recurring temporal structures. Pooling layers progressively abstract these into higher-level features.
Key observations:
- MLP on regression tasks performs well on stationary inputs but learns the trend when given raw price levels, producing misleadingly high R-squared values.
- MLP on GAF images loses spatial relationships when images are flattened, making it less efficient than CNN for image-based classification.
- CNN on GAF images is naturally suited to 2D inputs and detects local angular patterns that represent directional transitions in the time series.
- Stationary inputs consistently produce more reliable results across both architectures.
- Fractionally differenced inputs capture long-term dependencies better than log-returns at longer forecasting horizons.
Conclusion: CNN with GAF encoding is better suited for direction classification because the image representation exposes geometric patterns that a flat-vector MLP cannot efficiently detect. MLP remains competitive for continuous regression forecasting where the input structure is already informative as a vector. A production system would benefit from ensembling both architectures.
- Training MLP on non-stationary price levels produces misleadingly high R-squared values - the model learns the trend, not the dynamics
- Log-returns provide the most stable and generalizable regression results
- Fractional differencing balances stationarity and memory, often outperforming log-returns at longer forecasting horizons
- GAF-CNN models capture spatial patterns invisible to flat-vector MLP models
- Stationary inputs consistently improve model reliability across both architectures
Python 3.x
TensorFlow / Keras
scikit-learn
yfinance
pyts
statsmodels
pandas
numpy
matplotlib
seaborn
git clone https://github.com/QuantSingularity/CNN-MLP-GAF-Deep-Learning-Finance.git
cd CNN-MLP-GAF-Deep-Learning-Finance
pip install yfinance pyts tensorflow scikit-learn statsmodels matplotlib seaborn pandas numpy
jupyter notebook BTC-MLP-CNN-GAF-Forecasting.ipynbBTC-USD daily closing prices downloaded via yfinance:
- Ticker: BTC-USD
- Period: 2018-01-01 to 2023-06-01
- Observations: 2,000 (trimmed)
- Source: Yahoo Finance
No manual data download required - the notebook fetches data automatically.
deep-learning time-series bitcoin financial-forecasting convolutional-neural-network mlp gramian-angular-field fractional-differencing stationarity adf-test log-returns quantitative-finance tensorflow python jupyter-notebook
- Lopez de Prado, M. (2018). Advances in Financial Machine Learning. Wiley.
- Wang, Z., & Oates, T. (2015). Imaging Time-Series to Improve Classification and Imputation. IJCAI.
- Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press.