An end-to-end pipeline combining NLP sentiment analysis, LSTM price forecasting, and Deep Reinforcement Learning to train an autonomous trading agent - validated on Tesla (TSLA) stock data.
The pipeline runs in four stages across five Jupyter notebooks:
🗞️ Raw Data (tweets + OHLCV prices)
│
▼
🧠 [1] Financial_Sentiment_Extraction_System.ipynb
Extracts per-day sentiment scores using FinBERT
│
▼
🔮 [2] predicting_close_LSTM.ipynb
Trains a 3-layer LSTM to predict next-day closing prices
│
▼
🔧 [3] Finalising_predicting_close_LSTM.ipynb
Filters dates and assembles final RL training datasets
│
▼
🤖 [4a] gym_anytrading_with_technical_indicators_and_sentiment.ipynb
Trains the A2C trading agent with the full feature set
🔬 [4b] Abelation Test.ipynb
Repeats training across feature subsets to isolate which
inputs drive performance
algo-trading-system/
├── 📂 Code/
│ ├── 1. Sentiment Extraction/
│ │ └── 🧠 Financial_Sentiment_Extraction_System.ipynb
│ ├── 2. Close Price Prediction/
│ │ ├── 🔮 predicting_close_LSTM.ipynb
│ │ └── 🔧 Finalising_predicting_close_LSTM.ipynb
│ └── 3. RL Logic/
│ ├── 🤖 gym_anytrading_with_technical_indicators_and_sentiment.ipynb
│ └── 🔬 Abelation Test.ipynb
└── 📂 Data/
├── Tweets data/
│ ├── tweets.csv
│ └── processed_tweets.csv
└── TESLA data/
├── 1. Raw data/
│ └── PAPER_TSLA_data.csv
├── 2. Processed data/
│ ├── processed_PAPER_TSLA_data.csv
│ └── processed_PAPER_TSLA_data_probabilities.csv
├── 3. Normalised data/
│ ├── processed_normalised_PAPER_TSLA_data.csv
│ └── processed_normalised_PAPER_TSLA_data_probabilities.csv
├── 4. LSTM data/
│ ├── processed_normalised_PAPER_TSLA_data_LSTM.csv
│ └── processed_normalised_PAPER_TSLA_data_probabilities_LSTM.csv
└── 5. RL training data/
├── TESLA RL training data - 1 Sentiment 12.2024.csv
└── TESLA RL training data - 3 Sentiment 12.2024.csv
In our research we used this Kaggle dataset of Tesla-related tweets.
Required columns:
Date- publication date of the headlineTweet- headline or news text
We used Yahoo Finance to download TSLA OHLCV data (2013–2020).
Required columns:
Date- trading dayOpen,High,Low,Close,Adj Close,Volume
Notebook: Code/1. Sentiment Extraction/Financial_Sentiment_Extraction_System.ipynb
Cleans raw tweets, runs them through FinBERT (a pre-trained financial BERT model), and aggregates daily sentiment scores.
Days with no news receive a neutral score of 0.
Configure the ADD_ONE_SENTIMENT_COLUMN flag to choose the output format:
- ✅
True→ a singleSentimentcolumn with values{-1, 0, 1} - 🔢
False→ three probability columns:Positive Sentiment,Negative Sentiment,Neutral Sentiment
In our research, the ADD_ONE_SENTIMENT_COLUMN flag is set to True.
The notebook also Z-score normalises the financial features (Open, High, Low, Close, Adj Close, Volume).
📤 Outputs:
Data/Tweets data/processed_tweets.csvData/TESLA data/2. Processed data/processed_PAPER_TSLA_data.csvData/TESLA data/3. Normalised data/processed_normalised_PAPER_TSLA_data.csv
Notebook: Code/2. Close Price Prediction/predicting_close_LSTM.ipynb
Trains a 3-layer LSTM network on 5 features (Open, High, Low, Close, Volume) using an 80-day sliding window.
Predictions are inverse-transformed back to the original price scale and saved in a new Predicted_Close column.
⚙️ Key Hyperparameters:
| Parameter | Value |
|---|---|
| 🪟 Window size | 80 days |
| 🏗️ LSTM layers | 3 |
| 🧩 Hidden units | 150 |
| 💧 Dropout | 60% |
| ✂️ Train/val split | 95% / 5% |
| ⚡ Optimizer | Adam (lr=0.001) |
| 🔁 Max epochs | 50 (early stopping, patience=10) |
📤 Output: Data/TESLA data/4. LSTM data/processed_normalised_PAPER_TSLA_data_LSTM.csv
Notebook: Code/2. Close Price Prediction/Finalising_predicting_close_LSTM.ipynb
Filters the LSTM-augmented data to dates from 2014-01-01 onwards and produces two versioned CSV files - one for each sentiment format - ready for RL training.
📤 Outputs:
Data/TESLA data/5. RL training data/TESLA RL training data - 1 Sentiment MM.YYYY.csvData/TESLA data/5. RL training data/TESLA RL training data - 3 Sentiment MM.YYYY.csv
Notebook: Code/3. RL Logic/gym_anytrading_with_technical_indicators_and_sentiment.ipynb
Trains an A2C (Advantage Actor-Critic) agent inside a custom gym-anytrading environment while setting a trading WINDOW_SIZE.
The agent learns a long/short trading policy over ~1,258 training days (up to 2019-01-01) and is evaluated on ~252 test days.
In our research, WINDOW_SIZE was tested across {10, 15, 20, 25}.
🏆 Performance is reported as: Calmar Ratio · Total Reward · Total Profit
Notebook: Code/3. RL Logic/Abelation Test.ipynb
Runs the same A2C training loop for the selected FEATURE_STATE to measure the contribution to final trading performance.
The FEATURE_STATE parameter controls which signals the agent observes:
| 🏷️ FeatureState | 📡 Signals |
|---|---|
BasicMode |
Close price only |
WithTechInd |
Close + SMA, RSI, MOM, EMA, AROONOSC |
WithPredict |
Above + Predicted_Close |
WithLag |
Above + lag features |
FullMode |
Above + Sentiment |
| Component | Library / Model |
|---|---|
| 🧠 Sentiment analysis | |
| 🔮 Price prediction | |
| 🎮 RL environment | |
| 🤖 RL algorithm | |
| 📊 Technical indicators | |
| 🔢 Data / ML utilities |