A stock analysis platform built with Python and Dash, featuring technical analysis, portfolio management, and strategy backtesting capabilities.
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
- Stock data visualization
- Technical indicators (Moving Averages, RSI, MACD, Bollinger Bands)
- Volume analysis
- Risk metrics calculation
- Trading signals
- Benchmark comparison
- Create and manage multiple portfolios
- Track holdings and allocations
- Real-time portfolio valuation
- Performance analytics
- Risk assessment
- Position management
- Test portfolio strategies
- Historical performance analysis
- Risk-adjusted returns
- Benchmark comparison
- Detailed performance metrics
- Custom date ranges
- 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
- Clone the repository
git clone https://github.com/edtufte/ModularStockAnalysisApp.git
cd modular-stock-analysis
- Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activate
- Install dependencies
pip install -r requirements.txt
- Run the application
python app.py
The application will be available at http://localhost:8050
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
- Enter a stock ticker symbol
- Select analysis timeframe
- Choose benchmark for comparison
- View technical analysis, charts, and recommendations
- Create a new portfolio
- Add holdings with allocations
- Monitor performance
- Adjust positions as needed
- Select a portfolio
- Define date range
- Choose benchmark
- Analyze historical performance
- 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
- Intelligent data caching
- Smart cache management
- Vectorized calculations
- Efficient database operations
- Responsive UI components
- Memory-efficient processing
- Install dependencies
pip install -r requirements.txt
- 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.
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
For production deployment:
gunicorn app:server
Configure your web server (e.g., nginx) to proxy requests to gunicorn.
This project is licensed under the MIT License - see the LICENSE file for details.
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
Similar to Sharpe but only considers downside volatility:
Sortino Ratio = (Rp - Rf) / Οd
Where:
Οd = Standard Deviation of Negative Returns
Largest peak-to-trough decline:
MDD = (Trough Value - Peak Value) / Peak Value
Maximum potential loss at a confidence level (95%):
VaR = ΞΌ - (z * Ο)
Where:
ΞΌ = Mean Return
z = Z-score for confidence level
Ο = Standard Deviation
Moving average with standard deviation bands:
Middle Band = 20-day SMA
Upper Band = Middle Band + (2 Γ Ο)
Lower Band = Middle Band - (2 Γ Ο)
Momentum indicator measuring speed of price changes:
RSI = 100 - (100 / (1 + RS))
RS = Average Gain / Average Loss
Trend-following momentum indicator:
MACD = 12-period EMA - 26-period EMA
Signal Line = 9-period EMA of MACD
Measure of volatility compared to market:
Ξ² = Covariance(Rp, Rm) / Variance(Rm)
Where:
Rp = Portfolio Returns
Rm = Market Returns
Excess return of investment relative to benchmark:
Ξ± = Rp - [Rf + Ξ²(Rm - Rf)]
Risk-adjusted excess returns relative to benchmark:
IR = (Rp - Rb) / Tracking Error
Where:
Rb = Benchmark Return
Tracking Error = Std(Rp - Rb)
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! π