FinanceCraft is an advanced stock market analysis and prediction project. It integrates a range of technical indicators with a Random Forest Classifier to forecast whether a stock's next day closing price will rise or fall.
This project demonstrates how to:
- Gather historical stock data (using
yfinance) - Compute advanced technical indicators (e.g., RSI, Bollinger Bands, MACD)
- Construct a classification target (Up or Down) for the next trading day
- Train and tune a Random Forest Classifier to predict price movement direction
- Visualize results (price/indicators, cumulative returns, etc.)
- Implement a dynamic loading bar (via
ipywidgets) to track progress - Display a real-time stock snapshot for user reference
- RSI (Relative Strength Index): Measures momentum by comparing recent gains and losses. Indicates overbought or oversold conditions.
- Bollinger Bands: Utilizes a moving average and standard deviation to form upper and lower bands, highlighting volatility and potential price extremes.
- MACD (Moving Average Convergence Divergence): Tracks momentum through the difference between two EMAs and a signal line. Crossovers suggest trend changes.
- ADX (Average Directional Index): Assesses trend strength by comparing positive and negative directional movements.
- ATR (Average True Range): Measures market volatility based on daily price range expansions.
- Stochastic Oscillator: Compares a closing price to a price range over a set period, signaling potential reversals.
- Williams %R: Similar to Stochastic, it identifies overbought or oversold conditions.
- OBV (On-Balance Volume): Evaluates volume flow to determine buying or selling pressure.
These indicators capture various momentum, trend, and volatility signals. Combining them provides a comprehensive view of market conditions, enhancing the model’s ability to predict price direction.
- Ensemble of Decision Trees: Trains multiple decision trees and aggregates their outputs, reducing overfitting and variance.
- Bootstrap Sampling & Random Subspace: Each tree is trained on a random subset of data and features, ensuring diversity among trees.
- Majority Voting for Classification: Each tree votes on the price movement direction. The final prediction is based on the majority vote.
- Advantages: Ensemble methods generally outperform single trees, especially with noisy financial data. They are also resilient to correlated indicators and missing data.
By combining advanced technical indicators (for comprehensive feature engineering) with the Random Forest classification approach, FinanceCraft effectively captures diverse market signals and mitigates noise, aiming to provide reliable predictions for next-day price movements.
Below is the complete code in a single cell. You can copy and paste it as a standalone README.md or Python/Notebook cell. It employs the RandomForestClassifier to classify the next day’s close as higher or lower, incorporates a dynamic loading bar to monitor progress, and includes real-time stock snapshots for user reference.
- Data Fetching: Utilizes
yfinanceto retrieve 30 years of historical data forAAPL. - Indicator Computation: Calculates RSI, Bollinger Bands, MACD, and other indicators to capture various market signals (momentum, volatility, etc.).
- Classification Target: Labels each entry with
1if the next day’s close is higher, else0. - Feature Preparation: Integrates multiple signals into features, creating an array
Xfor the model. - Random Forest Classifier:
- Uses
GridSearchCVwithTimeSeriesSplitto optimize hyperparameters (e.g.,max_depth, n_estimators). - Leverages ensemble learning on diverse features to enhance prediction reliability.
- Uses
- Backtesting Logic:
- Transforms predicted classes into trading signals (long if up, short if down).
- Combines
CombinedSignalwith model predictions to refine trading decisions. - Calculates daily returns and cumulative returns to assess strategy performance.
- Visualization & Real-Time Snapshot:
- Plots Bollinger Bands, MACD, RSI for a comprehensive technical overview.
- Displays a real-time 1-month snapshot of the ticker’s close price.
FinanceCraft demonstrates the effective integration of multiple trading algorithms and technical indicators with a well-tuned Random Forest Classifier to predict next-day price movements. It also incorporates fundamental backtesting logic, enabling comparison of the strategy’s returns against the broader market.
Note: Real-world performance can vary based on market conditions, data quality, transaction costs, and other factors. Continuous refinement through feature engineering, hyperparameter optimization, or advanced modeling techniques can further enhance prediction reliability.




