Skip to content

leesh2015/financial-timeseries-python

Repository files navigation

Financial Time Series Analysis with Python

English | ν•œκ΅­μ–΄

License: MIT Python 3.8+ Udemy Course

A comprehensive open-source project for mastering financial time series analysis, algorithmic trading strategies, and production-level automated trading systems using Python. This project combines theoretical knowledge with practical implementation, featuring a live production trading system that is currently operating with real capital.

πŸŽ“ Udemy Course

Mastering Financial Time Series Analysis with Python

πŸ‘‰ Enroll in the Course

  • All Sections: Course materials are fully uploaded and available
  • Section 3: Live production system with real-time trading records

πŸ“Š Live Trading Records

Currently in Live Production

This project includes a production trading system that is actively trading with real capital. Daily trading records are automatically uploaded to:

🌐 Trading History Dashboard

The system uses broker API integration for automated trading, and all trades are transparently displayed on the website.

πŸ“š Project Structure

Section 1: Financial Time Series Analysis

Status: βœ… Course Available

Comprehensive coverage of time series fundamentals and advanced techniques:

  • Chapter 1: Fundamentals of Time Series Data Analysis

    • Stationarity and non-stationarity
    • Differencing and transformation
    • Seasonal decomposition
  • Chapter 2: Advanced Time Series Analysis

    • ADF (Augmented Dickey-Fuller) test
    • AR (Autoregressive) models
    • PACF (Partial Autocorrelation Function) analysis
    • Random walk theory
  • Chapter 3: Univariate Time Series Analysis

    • AR, MA, ARMA models
    • AIC vs BIC model selection
    • Auto-ARIMA
    • Ljung-Box test for residual analysis
  • Chapter 4: Advanced Volatility Modeling and Forecasting

    • ARCH models
    • GARCH models
    • ARIMA-GARCH hybrid models
    • Backtesting strategies
  • Chapter 5: Multivariate Time Series Analysis

    • VAR (Vector Autoregression) models
    • VARMA models
    • Granger causality analysis
  • Chapter 6: Advanced Multivariate Time Series Analysis

    • VECM (Vector Error Correction Model)
    • Johansen cointegration test
    • VAR IRF (Impulse Response Function)
    • VAR FEVD (Forecast Error Variance Decomposition)
    • VECM-APARCH hybrid models

Section 2: Advanced Investment Strategy Design

Status: βœ… Course Available

Practical implementation of trading strategies:

  • Chapter 1: Dynamic Time Series Simulations

    • VECM-EGARCH hybrid model
    • Dynamic re-optimization
    • Long/short position management
  • Chapter 2: Applying Strategies to Bitcoin Trading

    • Bitcoin-specific optimizations
    • Commission fee considerations
    • Volatility-based re-optimization
  • Chapter 3: AI Trading Using Binance

    • Binance API integration
    • Real-time signal generation
    • Automated order execution

Section 3: Production Investment Strategy

Status: βœ… Course Available πŸš€ Live Production System

A production-level trading system currently operating with real capital:

  • Chapter 1: VECM-EGARCH Hybrid Model

    • Vector Error Correction Model for cointegration relationships
    • Exponential GARCH for volatility modeling
    • Dynamic position sizing based on model confidence
    • Key Features:
      • βœ… Information leakage prevention (walking forward validation)
      • βœ… Dynamic re-optimization based on ECT alpha
      • βœ… Confidence-based position sizing (0.2~0.8 fraction range)
      • βœ… Separate forecast horizons for buy (4 days) and sell (7 days)
  • Chapter 2: Reinforcement Learning (RL) Strategy

    • VECM-GARCH Hybrid combined with RL Agent
    • Market Regime Detection (Bull, Bear, Sideways, High Vol)
    • Simple Policy RL agent for dynamic position blending
    • Adaptive confidence thresholds
  • Mathematical Models:

    VECM Model:

    Ξ”Y_t = Ξ±Ξ²'Y_{t-1} + Γ₁ΔY_{t-1} + ... + Ξ“_{p-1}Ξ”Y_{t-p+1} + Ξ΅_t
    
    • Ξ±: adjustment coefficients (speed of adjustment to equilibrium)
    • Ξ²: cointegration vectors (long-run relationships)
    • Ξ“α΅’: short-run dynamics coefficients

    EGARCH Model:

    log(σ²_t) = Ο‰ + Ξ£α΅’β‚Œβ‚α΅– (Ξ±α΅’|z_{t-i}| + Ξ³α΅’z_{t-i}) + Ξ£β±Όβ‚Œβ‚α΅  Ξ²β±Όlog(σ²_{t-j})
    
    • Captures asymmetric volatility effects
    • Ensures positive variance through log transformation

    Hybrid Forecast:

    ΕΆ_{t+1} = VECM_forecast + EGARCH_mean_adjustment
    
  • Trading Strategy:

    • Long Entry: hybrid_yhat_buy > actual_price AND lower_price < lower_bound_mean
    • Long Exit: upper_price > upper_bound_mean
    • Dynamic Re-optimization: When ECT alpha changes from negative to positive

Section 4: Advanced Time Series Models

Status: βœ… Course Available

πŸ“– Detailed Documentation

Modern statistical and ML techniques for challenging temporal structures:

  • Chapter 1: State-Space Models – time-varying beta tracking and error-correction diagnostics
  • Chapter 2: Kalman Filter suite (Custom, FilterPy, PyKalman EM, Particle) – dynamic beta tracking with quantile-based switching
  • Chapter 3: Prophet Model – seasonality-aware forecasting with rolling re-training
  • Chapter 4: Deep Learning (LSTM) – direction classification with imbalance-aware training
  • Chapter 5: Tree-Based ML (XGBoost) – binary classification for direction prediction with rich technical features and ROC-optimized thresholds
  • Chapter 6: Wavelet Transform – multi-resolution feature engineering for volatility regimes
  • Chapter 7: Copula Models – dependence modeling and tail-risk simulation

Section 5: Factor-Based Asset Pricing Models

Status: βœ… Course Available

πŸ“– Detailed Documentation

Theoretical foundations and practical applications of factor-based asset pricing models:

  • Chapter 1: CAPM Limitations and Fama-French Model Origins

    • Empirical testing of CAPM
    • Identifying market anomalies (Size, Value)
    • Visualizing model limitations
  • Chapter 2: Fama-French 3-Factor Model

    • Implementing the 3-factor model
    • Calculating factor exposures (Betas)
    • Comparing multi-factor models vs CAPM
  • Chapter 3: Fama-French 5-Factor and Extended Models

    • Profitability (RMW) and Investment (CMA) factors
    • Momentum factor integration (6-Factor model)
    • Model selection and comparison
  • Chapter 4: Practical Application and Backtesting

    • Factor-based portfolio construction
    • Walk-forward validation
    • Transaction cost analysis
    • Performance evaluation (Sharpe, Alpha, etc.)

Appendix: Financial Mathematics Theory and Practical Examples

Status: βœ… Available

πŸ“– Detailed Documentation

A comprehensive guide to all financial mathematics theory used in quant trading, implemented with easy-to-understand example code:

  • Chapter 1: Linear Algebra

    • Portfolio optimization
    • PCA-based factor analysis
    • Multi-factor regression (Fama-French)
    • Matrix operations in VAR & VECM models
  • Chapter 2: Analysis & Calculus

    • Gradient descent visualization
    • Understanding backpropagation algorithm
    • Calculus principles in GARCH models
    • Wavelet Transform
    • Ito's Lemma
    • Bayesian Optimization
  • Chapter 3: Probability & Time Series Statistics

    • Stationarity testing and understanding
    • Probabilistic foundations of ARIMA models
    • Cointegration and pair trading
    • Dependence analysis using Copula
    • Monte Carlo simulation
  • Chapter 4: Bayesian Statistics & Filtering

    • Bayesian inference examples
    • Understanding Kalman Filter
    • State-space models

πŸš€ Quick Start

Installation

  1. Clone the repository:
git clone https://github.com/leesh2015/financial-timeseries-python.git
cd financial-timeseries-python
  1. Install dependencies:
pip install -r requirements.txt

Running Examples

Section 1 - Time Series Analysis:

cd "Section1.Financial Time Series Analysis/Chapter1.Fundamentals of Time Series Data Analysis"
python stable_data.py

Section 2 - Strategy Design:

cd "Section2.Advanced Investment Strategy Design/Chapter1.Dynamic Time Series Simulations"
python dynamic_simulation.py

Section 3 - Production Simulation:

# Chapter 1: VECM-EGARCH Hybrid
cd "Section3.Production Investment Strategy/Chapter1.VECM-EGARCH Hybrid"
python production_simulation_.py

# Chapter 2: Reinforcement Learning
cd "Section3.Production Investment Strategy/Chapter2.Reinforcement Learning"
python dynamic_simulation_rl.py

Section 4 - Advanced Time Series Models:

cd "Section4.Advanced Time Series Models/Chapter1.State-Space Models"
python state_space_model.py

Section 5 - Factor Models:

cd "Section5.Factor-Based Asset Pricing Models/Chapter4.Practical Application and Backtesting"
python factor_portfolio_backtest.py

Appendix - Financial Mathematics:

# Install dependencies from project root (skip if already installed)
pip install -r requirements.txt

cd Appendix

# Chapter 1: Linear Algebra
python Chapter1_Linear_Algebra/portfolio_optimization.py

# Chapter 2: Calculus
python Chapter2_Calculus/gradient_descent_demo.py

# Chapter 3: Probability & Statistics
python Chapter3_Probability_Statistics/stationarity_analysis.py

# Chapter 4: Bayesian
python Chapter4_Bayesian_Filtering/kalman_filter_demo.py

Results will be saved in the results/ folder within each section.

πŸ“¦ Dependencies

Core dependencies (see requirements.txt for full list):

  • Data Science: numpy, pandas, scipy
  • Time Series: statsmodels, arch, pmdarima
  • Data Collection: yfinance
  • Visualization: matplotlib, seaborn
  • Machine Learning: scikit-learn
  • Cryptocurrency: ccxt (for Binance integration)
  • Excel Support: openpyxl

🎯 Key Features

Information Leakage Prevention

  • Uses data up to time t-1 to predict price at time t
  • Models are re-trained at each step using only historical data (walking forward)
  • No future data is used in any prediction or optimization step

Dynamic Model Adaptation

  • Automatic re-optimization when market conditions change
  • ECT alpha monitoring for cointegration relationship health
  • Volatility-based model adjustments

Confidence-Based Position Sizing

  • Dynamic position sizing based on VECM model confidence
  • Fraction range: 0.2 (low confidence) to 0.8 (high confidence)
  • Adaptive threshold calculation using rolling window

Production-Ready

  • Real-time broker API integration
  • Automated trade execution
  • Daily performance tracking and reporting
  • Transparent trade history on web dashboard

πŸ“ˆ Performance Metrics

The production system tracks comprehensive performance metrics:

  • Total P&L
  • Win Rate
  • Sharpe Ratio
  • Maximum Drawdown
  • Annualized Returns
  • Buy-and-Hold Comparison

View live metrics at: Trading History Dashboard

πŸ”¬ Research & Methodology

This project implements state-of-the-art financial econometrics techniques:

  • Cointegration Analysis: Identifying long-run equilibrium relationships
  • Error Correction Models: Capturing short-term deviations from equilibrium
  • GARCH Family Models: Modeling volatility clustering and asymmetry
  • Hybrid Forecasting: Combining multiple models for improved accuracy
  • Dynamic Optimization: Adapting to changing market regimes

πŸ“– Course-Code Mapping

Course Section Repository Section Status
Section 1: Time Series Fundamentals Section1.Financial Time Series Analysis/ βœ… Available
Section 2: Strategy Design Section2.Advanced Investment Strategy Design/ βœ… Available
Section 3: Production System Section3.Production Investment Strategy/ βœ… Available πŸš€
Section 4: Advanced Time Series Models Section4.Advanced Time Series Models/ βœ… Available
Section 5: Factor Models Section5.Factor-Based Asset Pricing Models/ βœ… Available
Appendix: Financial Mathematics Appendix/ βœ… Available

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Join the Discussion: Have questions, ideas, or want to share your results? Join our GitHub Discussions to connect with the community!

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

⚠️ Disclaimer

Important: This project is for educational and research purposes. The production trading system is provided as a demonstration of the concepts taught in the course.

  • Past performance does not guarantee future results
  • Trading involves risk of financial loss
  • Always conduct thorough backtesting before deploying any trading strategy
  • The authors are not responsible for any financial losses incurred from using this code

πŸ”— Links

πŸ“§ Contact

For questions, suggestions, or collaboration opportunities, please open an issue on GitHub or contact through the Udemy course platform.


Made with ❀️ for the algorithmic trading community

This project demonstrates the complete journey from theoretical time series analysis to production-level automated trading systems.