This project focuses on detecting anomalies in financial time-series data, specifically stock price trends. The primary goal is to identify unusual behavior or outliers in stock movement that could signal major events, fraud, or trading opportunities.
We use a combination of statistical techniques and machine learning algorithms to analyze historical stock price data and flag anomalies in trends, volatility, and price fluctuations.
- Load and preprocess historical stock market data
- Calculate key financial indicators (moving averages, volatility, returns)
- Apply anomaly detection algorithms (Isolation Forest, DBSCAN)
- Forecast trends using LSTM or Prophet
- Visualize anomalies and prediction uncertainty
- Source: Yahoo Finance API
- Sample Ticker: AAPL, MSFT, TSLA, etc.
- Time Period: Adjustable (e.g., last 5 years)
- Features Used:
Open,High,Low,Close,Volume- Technical indicators (Moving Average, RSI, Volatility, etc.)
You can change the ticker and time range to analyze different stocks.
- Python
pandas,numpy– data manipulationmatplotlib,seaborn,plotly– visualizationscikit-learn– Isolation Forest, DBSCANtensorflow/keras– LSTM (forecasting)prophet– Facebook Prophet time-series model
- yfinance – fetch real-time and historical stock data
-
Data Collection
- Fetch historical stock data using
yfinance
- Fetch historical stock data using
-
Preprocessing
- Handle missing values, compute log returns
- Calculate rolling averages and volatility
-
Anomaly Detection
- Apply
Isolation Forestand/orDBSCANon engineered features - Label points as anomalous or normal
- Apply
-
Forecasting
- Use LSTM or Prophet to model and forecast future stock trends
- Evaluate using RMSE, MAE
-
Visualization
- Plot stock prices with anomalies highlighted
- Forecast with uncertainty intervals
- Successfully detected price spikes/drops using Isolation Forest
- DBSCAN detected density-based outliers in low-volume periods
- Prophet forecasted trends with seasonality and trend changepoints
- LSTM showed potential for multi-step forecasting but required tuning
Anomalies can help in building alert systems for financial risk or trading signals.
- Clone the repository:
git clone https://github.com/yourusername/financial-anomaly-detection.git cd financial-anomaly-detection