I know I’ve covered parts of this before, but this time I decided to start fresh with a new project. What I’ve built so far is a data pipeline designed specifically for a machine learning–based trading system. The main reason for creating this pipeline is a limitation in MT5: it can only provide up to 80,000 rows of historical data, which is nowhere near enough for training a reliable machine learning model. For my use case, I need at least 2 million historical records to properly train and validate the model.
In this post, I’ll walk you through how I compute three essential technical indicators—RSI, MACD, and the Stochastic Oscillator—which form the foundation of my training dataset. In upcoming posts, I’ll dive into the trading strategy itself. I’ve already implemented and backtested the system, and the results are very promising. That said, I still plan to add more features and refinements so readers can learn from each step. Who knows—maybe following this blog might even help someone build a path toward becoming a billionaire.
Technical indicators are the backbone of most trading strategies. In this post, I’ll walk through how to generate three widely used indicators — RSI, MACD, and the Stochastic Oscillator — using pure Python and Pandas, starting from raw OHLC price data.
This approach is lightweight, transparent, and ideal for backtesting, signal generation, or machine learning feature engineering.
📊 Prerequisites
Your input data must contain the following columns:
-
time -
open -
high -
low -
close
The data is loaded from a CSV file (init_data.csv) and processed into a new dataset (training_data.csv) with the computed indicators.
🔹 Relative Strength Index (RSI)
RSI measures momentum by comparing recent gains and losses. It oscillates between 0 and 100 and is commonly used to identify overbought and oversold conditions.
Formula logic:
-
Compute price differences
-
Separate gains and losses
-
Calculate rolling averages
-
Convert to RSI scale
🔹 Moving Average Convergence Divergence (MACD)
MACD is a trend-following momentum indicator based on Exponential Moving Averages (EMAs).
-
MACD Line = EMA(12) − EMA(26)
-
Signal Line = EMA(9) of MACD
MACD crossovers are commonly used to detect trend reversals and momentum shifts.
🔹 Stochastic Oscillator
The Stochastic Oscillator compares the current close to the recent price range.
-
%K shows the current position within the range
-
%D is a moving average of %K
Values above 80 typically indicate overbought conditions, while values below 20 suggest oversold levels.
🔄 Updating the Dataset with Indicators
All indicators are computed and appended to the dataset in a single function. Rolling calculations naturally produce NaN values, which are removed afterward.
🚀 Final Output
The main program:
-
Loads
init_data.csv -
Computes RSI, MACD, and Stochastic
-
Saves the enriched dataset as
training_data.csv
This output can be used for:
-
Strategy backtesting
-
Signal detection
-
Machine learning model training
-
Trade analytics
🧠Final Thoughts
Generating indicators manually gives you full control and transparency over your trading logic. It also helps avoid black-box dependencies and makes your system easier to debug and extend.
In future posts, I’ll build on this foundation by:
-
Combining indicators into trading signals
-
Adding backtesting logic
-
Preparing features for machine learning models
If you’re building your own trading system, this is a solid place to start.
No comments:
Post a Comment