Skip to content

edtufte/ModularStockAnalysisApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

36 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Modular Stock Analysis Dashboard

A stock analysis platform built with Python and Dash, featuring technical analysis, portfolio management, and strategy backtesting capabilities.

Python Dash License

Key Assumptions

The following assumptions are hard-coded in the calculations:

  • Risk-free rate = 2% (used in Sharpe Ratio, Sortino Ratio, and Alpha calculations)
  • Trading days per year = 252 (used for annualization)
  • RSI period = 14 days
  • Bollinger Bands = 20-day period with 2 standard deviations
  • MACD parameters:
    • Fast EMA = 12 days
    • Slow EMA = 26 days
    • Signal line = 9-day EMA
  • Value at Risk (VaR) confidence level = 95%
  • ATR period = 14 days
  • All calculations use adjusted close prices

Features

πŸ“Š Research Dashboard

  • Stock data visualization
  • Technical indicators (Moving Averages, RSI, MACD, Bollinger Bands)
  • Volume analysis
  • Risk metrics calculation
  • Trading signals
  • Benchmark comparison

πŸ“ˆ Portfolio Management

  • Create and manage multiple portfolios
  • Track holdings and allocations
  • Real-time portfolio valuation
  • Performance analytics
  • Risk assessment
  • Position management

πŸ”„ Strategy Backtesting

  • Test portfolio strategies
  • Historical performance analysis
  • Risk-adjusted returns
  • Benchmark comparison
  • Detailed performance metrics
  • Custom date ranges

Technical Highlights

  • Efficient Data Caching: Prevents duplicate downloads of historical data
  • Advanced Technical Analysis: Comprehensive indicator calculations
  • Robust Error Handling: Graceful degradation with detailed logging
  • Responsive Design: Optimized for all screen sizes
  • Thread-safe Database: SQLite with connection pooling
  • Efficient Data Processing: Vectorized operations with pandas

Installation

  1. Clone the repository
git clone https://github.com/edtufte/ModularStockAnalysisApp.git
cd modular-stock-analysis
  1. Create and activate a virtual environment
python -m venv venv
source venv/bin/activate  # On Windows use: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Run the application
python app.py

The application will be available at http://localhost:8050

Project Structure

modular-stock-analysis/
β”œβ”€β”€ app.py                 # Main application entry point
β”œβ”€β”€ requirements.txt       # Project dependencies
β”œβ”€β”€ services/             # Core services
β”‚   β”œβ”€β”€ stock_data_service.py
β”‚   β”œβ”€β”€ analysis_service.py
β”‚   └── database.py
β”œβ”€β”€ layouts/              # UI layouts
β”‚   β”œβ”€β”€ dashboard_layout.py
β”‚   β”œβ”€β”€ portfolio_layout.py
β”‚   └── backtesting_layout.py
β”œβ”€β”€ callbacks/            # Event handlers
β”‚   β”œβ”€β”€ dashboard_callbacks.py
β”‚   β”œβ”€β”€ portfolio_callbacks.py
β”‚   └── backtesting_callbacks.py
β”œβ”€β”€ components/           # Reusable UI components
β”‚   β”œβ”€β”€ dashboard_components.py
β”‚   └── charts.py
β”œβ”€β”€ models/              # Data models
β”‚   └── portfolio.py
└── static/              # Static assets
    └── styles.css

Usage

Research Dashboard

  1. Enter a stock ticker symbol
  2. Select analysis timeframe
  3. Choose benchmark for comparison
  4. View technical analysis, charts, and recommendations

Portfolio Management

  1. Create a new portfolio
  2. Add holdings with allocations
  3. Monitor performance
  4. Adjust positions as needed

Strategy Backtesting

  1. Select a portfolio
  2. Define date range
  3. Choose benchmark
  4. Analyze historical performance

Technical Analysis Features

  • Moving Averages (SMA, EMA)
  • Bollinger Bands
  • Relative Strength Index (RSI)
  • MACD
  • Average True Range (ATR)
  • On-Balance Volume (OBV)
  • Risk Metrics
    • Sharpe Ratio
    • Sortino Ratio
    • Maximum Drawdown
    • Value at Risk
    • Beta & Alpha
    • Information Ratio

Performance Optimizations

  • Intelligent data caching
  • Smart cache management
  • Vectorized calculations
  • Efficient database operations
  • Responsive UI components
  • Memory-efficient processing

Development

Setting up for Development

  1. Install dependencies
pip install -r requirements.txt
  1. Run the development server
python app.py

The application supports hot-reloading during development. Any changes to the Python files will automatically restart the server.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

Deployment

For production deployment:

gunicorn app:server

Configure your web server (e.g., nginx) to proxy requests to gunicorn.

License

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

Acknowledgments

  • yfinance for stock data
  • Dash for the web framework
  • Plotly for interactive visualizations

Financial Metrics and Calculations

Risk Metrics

Sharpe Ratio

Measures risk-adjusted return relative to risk-free rate:

Sharpe Ratio = (Rp - Rf) / Οƒp

Where:
Rp = Return of Portfolio
Rf = Risk-free Rate
Οƒp = Standard Deviation of Portfolio Returns

Sortino Ratio

Similar to Sharpe but only considers downside volatility:

Sortino Ratio = (Rp - Rf) / Οƒd

Where:
Οƒd = Standard Deviation of Negative Returns

Maximum Drawdown

Largest peak-to-trough decline:

MDD = (Trough Value - Peak Value) / Peak Value

Value at Risk (VaR)

Maximum potential loss at a confidence level (95%):

VaR = ΞΌ - (z * Οƒ)

Where:
ΞΌ = Mean Return
z = Z-score for confidence level
Οƒ = Standard Deviation

Technical Indicators

Bollinger Bands

Moving average with standard deviation bands:

Middle Band = 20-day SMA
Upper Band = Middle Band + (2 Γ— Οƒ)
Lower Band = Middle Band - (2 Γ— Οƒ)

Relative Strength Index (RSI)

Momentum indicator measuring speed of price changes:

RSI = 100 - (100 / (1 + RS))
RS = Average Gain / Average Loss

Moving Average Convergence Divergence (MACD)

Trend-following momentum indicator:

MACD = 12-period EMA - 26-period EMA
Signal Line = 9-period EMA of MACD

Performance Metrics

Beta (Ξ²)

Measure of volatility compared to market:

Ξ² = Covariance(Rp, Rm) / Variance(Rm)

Where:
Rp = Portfolio Returns
Rm = Market Returns

Alpha (Ξ±)

Excess return of investment relative to benchmark:

Ξ± = Rp - [Rf + Ξ²(Rm - Rf)]

Information Ratio

Risk-adjusted excess returns relative to benchmark:

IR = (Rp - Rb) / Tracking Error

Where:
Rb = Benchmark Return
Tracking Error = Std(Rp - Rb)

Average True Range (ATR)

Volatility indicator showing price range:

TR = max[(High - Low), |High - Close_prev|, |Low - Close_prev|]
ATR = 14-period moving average of TR

All metrics are calculated using adjusted close prices to account for corporate actions such as splits and dividends.


πŸ“ˆ Happy Trading! πŸ“Š

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published