Markov Chain-Based Financial Prediction System
Github Link: github.com/MetinUnlu/markovchain_finance
This project implements a comprehensive financial prediction system using Markov chains to model stock market behavior and make trading decisions. The system analyzes historical stock data, creates state-based models, and generates predictions for future price movements with threshold-based decision making.
Project Overview
The system utilizes Markov chains to model transition probabilities between different market states, calculate the expected value of each state, and predict outcomes. If the predicted value exceeds a predefined threshold, trading orders are executed. The project demonstrates both the potential and limitations of Markov chain applications in financial markets.
Mathematical Foundation
The core of this system is based on mathematical modeling of state transitions:
- State Transition Probability: Pij = (Number of transitions from i to j) / (Total transitions from i)
- Expected Value Calculation: E(si) = Σ Pij × Vj
- Threshold-Based Decision Making: Orders are placed when O(si) > θ
Technical Implementation
The system processes Google stock data spanning from 2004 to 2024, implementing several key components:
Technical Indicators
- Short-term EMA (14-day): Tracks recent price movements
- Long-term EMA (50-day): Reflects established price trends
- RSI (14-day): Identifies overbought/oversold conditions
- Close Gap: Percentage change in closing prices
State Creation and Pattern Recognition
The system creates complex state patterns by combining:
- Short and Long EMA gap classifications (Low/Medium/High)
- RSI signals (Overbought/Oversold/Normal)
- Binary state determination based on price movement direction
Dataset and Preprocessing
The project uses comprehensive Google stock data with the following features:
- Date, Open, High, Low, Close, Volume
- Calculated technical indicators (EMA, RSI)
- State patterns generated from indicator combinations
- Transition matrices for state-to-state probability calculations
Results and Performance Analysis
Key Findings
The system was tested on the last 10% of the dataset (validation period), revealing important insights:
- Strategy Performance: Achieved 16% profit using threshold-based decisions
- Comparison with Buy-and-Hold: Simple buy-and-hold strategy yielded 58% profit over the same period
- Accuracy: The prediction model achieved 53% accuracy
- Threshold Impact: Without threshold (threshold=0), the system resulted in 92% loss
Honest Assessment
While the Markov chain approach shows promise, this implementation demonstrates several limitations:
- The strategy underperformed compared to simple buy-and-hold
- The model favored 'buy' predictions due to Google's general upward trend
- False positives in trading decisions significantly impacted returns
- The threshold mechanism improved performance but couldn't overcome fundamental limitations
Technologies and Tools
- Python: Primary programming language
- Pandas & NumPy: Data manipulation and numerical computations
- Matplotlib: Data visualization and result plotting
- Jupyter Notebook: Interactive development and analysis
- Technical Analysis Libraries: For calculating financial indicators
Key Insights and Lessons Learned
This project provided valuable insights into both the potential and limitations of Markov chains in financial prediction:
- State Design is Critical: The success of Markov chain methods heavily depends on creating meaningful states that capture market behavior
- Threshold Implementation: Risk management through thresholds is essential for practical trading applications
- Market Complexity: Simple technical indicators may not capture the full complexity of market dynamics
- Benchmark Comparison: Always compare against simple baseline strategies
Future Improvements
The project identifies several areas for enhancement:
- Reinforcement Learning Integration: Implement RL to create adaptive expected states
- Enhanced Indicators: Incorporate more sophisticated technical analysis tools
- Multi-Asset Analysis: Extend the approach to multiple securities
- Risk Management: Implement proper position sizing and stop-loss mechanisms
Conclusion
This Markov chain-based financial prediction system serves as an educational exploration of quantitative finance methods. While the current implementation shows limitations compared to simple strategies, it demonstrates the importance of rigorous testing and honest evaluation in financial modeling. The project highlights that successful algorithmic trading requires sophisticated state modeling, robust risk management, and realistic performance expectations.
The methodology is repeatable with any stock and time range, making it a valuable learning tool for understanding both the potential and pitfalls of mathematical approaches to financial prediction.