Comprehensive Code Analysis: QuantMetrics Pro - Advanced Portfolio Analysis Platform
- Bryan Downing
- Dec 15, 2025
- 13 min read
Executive Summary
This Python script represents a sophisticated financial analysis platform built with Streamlit, designed to perform comprehensive technical analysis, portfolio metrics calculation, and forecast ranking across multiple assets. The application combines traditional technical indicators with advanced portfolio risk metrics and machine learning-based forecasting to provide institutional-grade investment analysis capabilities. The system processes time-series financial data from CSV files and delivers actionable insights through interactive visualizations and quantitative scoring mechanisms as an advanced portfolio analysis platform.

1. Architecture and Component Overview
1.1 Core Dependencies and Framework Structure
The application leverages several key Python libraries to create a robust analytical framework. Streamlit serves as the web application framework, providing the user interface and interactive components. Pandas and NumPy form the computational backbone for data manipulation and mathematical operations. Plotly handles all visualization requirements with its sophisticated charting capabilities. The datetime module manages temporal operations, while scikit-learn provides machine learning functionality for forecasting models.
The architectural design follows a modular approach with clear separation of concerns. Technical indicator calculations are encapsulated in a dedicated class, portfolio metrics are organized as independent functions, and visualization components are separated from business logic. This structure ensures maintainability, testability, and scalability of the codebase.
1.2 Technical Indicators Implementation
The TechnicalIndicators class represents a custom implementation of popular trading indicators, eliminating dependency on external technical analysis libraries. This design choice provides complete control over calculation methodologies and ensures consistency across different indicators.
The Simple Moving Average (SMA) implementation uses pandas' rolling window functionality to calculate the arithmetic mean over specified periods. The Exponential Moving Average (EMA) applies exponential weighting to give more importance to recent prices, using pandas' ewm (exponentially weighted moving) function with the span parameter determining the decay rate.
The Relative Strength Index (RSI) calculation follows Wilder's smoothing method, computing the ratio of average gains to average losses over a specified period. The implementation carefully handles edge cases where all price movements are in one direction, preventing division by zero errors.
The MACD (Moving Average Convergence Divergence) indicator combines multiple EMAs to identify trend changes and momentum. The implementation calculates the MACD line as the difference between 12-period and 26-period EMAs, then applies a 9-period EMA to create the signal line. The histogram represents the difference between these two lines, providing visual representation of convergence and divergence.
Bollinger Bands utilize statistical properties of price distributions, calculating bands at a specified number of standard deviations from a moving average. This implementation allows for dynamic adjustment of both the moving average period and the standard deviation multiplier, accommodating different volatility regimes.
The Stochastic Oscillator measures momentum by comparing closing prices to the price range over a specified period. The implementation includes smoothing parameters for both %K and %D lines, reducing noise and providing clearer signals.
1.3 Portfolio Metrics Engine
The portfolio metrics calculation system implements industry-standard risk and performance measures. Each metric function follows a consistent pattern: accepting return series or price data as input, handling edge cases, and returning normalized values suitable for comparison across different assets.
The Sharpe Ratio calculation annualizes daily returns and adjusts for risk-free rates, providing a measure of risk-adjusted performance. The implementation assumes 252 trading days per year and handles zero-volatility scenarios gracefully.
The Sortino Ratio refines the Sharpe Ratio concept by focusing only on downside deviation, recognizing that investors are primarily concerned with negative volatility. The implementation filters for negative returns and calculates downside standard deviation separately.
Maximum Drawdown calculation tracks the largest peak-to-trough decline in portfolio value, providing crucial insight into potential losses during adverse market conditions. The algorithm efficiently computes cumulative returns and running maximums using vectorized operations.
The Calmar Ratio combines annualized returns with maximum drawdown to provide a comprehensive risk-adjusted performance metric particularly relevant for trend-following strategies. The implementation handles cases where no drawdown occurs, returning zero rather than infinity.
Additional metrics including Information Ratio, Omega Ratio, and Ulcer Index provide sophisticated measures of performance consistency, probability-weighted returns, and downside risk concentration respectively. Each implementation follows academic definitions while incorporating practical adjustments for real-world data.
2. Data Processing Pipeline
2.1 CSV File Processing
The process_csv_file function implements a robust data ingestion pipeline capable of handling various CSV formats. The function reads headerless CSV files and dynamically assigns column names based on the detected number of columns. This flexible approach accommodates both 5-column (OHLC) and 6-column (OHLCV) formats.
DateTime parsing utilizes pandas' powerful datetime functionality with explicit format specification ('%Y%m%d %H%M%S'), ensuring consistent parsing across different locales and system configurations. The implementation handles malformed timestamps gracefully through error handling.
Data validation includes type conversion for numeric columns with error coercion, preventing single bad data points from corrupting entire datasets. The function removes rows with missing closing prices, as these are essential for most calculations.
The index management strategy sets DateTime as the dataframe index and ensures chronological ordering, critical for time-series analysis and technical indicator calculations. This approach optimizes subsequent operations that rely on temporal relationships.
2.2 Technical Indicator Integration
The add_technical_indicators function orchestrates the application of multiple technical indicators to price data. The implementation creates a copy of the input dataframe to preserve original data, following functional programming principles.
Moving averages are calculated with different periods to capture short-term and long-term trends. The 20-period SMA represents monthly trends, while the 50-period SMA captures quarterly movements. The 12 and 26-period EMAs feed into MACD calculations.
Momentum indicators including RSI and Stochastic Oscillator use standard parameters that have proven effective across different markets. The 14-period RSI provides overbought/oversold signals, while Stochastic parameters are optimized for smooth signal generation.
Volume-based indicators including On-Balance Volume (OBV) and Volume-Weighted Average Price (VWAP) incorporate trading volume into analysis, providing insight into the strength of price movements. The implementation handles zero-volume periods appropriately.
Volatility indicators such as Average True Range (ATR) and Bollinger Bands quantify market volatility and identify potential support/resistance levels. The Rate of Change (ROC) indicator adds momentum analysis with a 10-period lookback.
2.3 Signal Generation System
The generate_trading_signals function implements a sophisticated multi-indicator signal generation system. Each indicator contributes signals based on established trading rules, with the system combining these into a weighted composite signal.
RSI signals follow traditional overbought/oversold thresholds at 70 and 30 respectively. The implementation uses pandas' loc accessor for efficient conditional assignment, avoiding loops and improving performance.
MACD signals detect crossovers between the MACD line and signal line, identifying potential trend changes. The implementation compares values directly rather than calculating differences, reducing computational overhead.
Moving average crossover signals identify golden crosses (bullish) and death crosses (bearish) by comparing short-term and long-term moving averages. This classic trend-following approach provides robust signals in trending markets.
Bollinger Band signals trigger when prices touch or exceed the bands, suggesting potential reversals. The implementation considers band breaches as contrarian signals, buying at lower bands and selling at upper bands.
The signal weighting system assigns different importance levels to various indicators based on their historical reliability. MACD signals receive the highest weight (1.5) due to their trend-following nature, while Stochastic signals receive lower weight (0.7) due to higher false positive rates.
The composite signal aggregation uses a threshold-based approach to generate final buy/sell signals. The threshold of 2.0 ensures multiple indicators must align before triggering a trade, reducing whipsaw in choppy markets.
3. Forecasting System
3.1 Enhanced Forecasting Algorithm
The enhanced_forecast function implements a multi-method forecasting approach combining linear regression, polynomial regression, and moving average techniques. This ensemble method provides more robust predictions than single-model approaches.
Linear regression captures the overall trend by fitting a straight line through historical prices. The implementation creates a numeric date index for regression, avoiding datetime handling complexities in scikit-learn.
Polynomial regression with degree 2 captures non-linear trends and potential acceleration or deceleration in price movements. The PolynomialFeatures transformer creates quadratic terms, allowing the model to fit parabolic patterns.
Moving average forecasting uses the 20-period average as a baseline, providing a smoothed estimate of current price levels. This component adds stability to forecasts, especially in volatile markets.
The trend adjustment mechanism analyzes recent price movements relative to earlier periods, calculating a momentum factor. This adjustment helps forecasts respond to changing market dynamics while avoiding overreaction to noise.
The forecast combination uses weighted averaging with empirically determined weights (0.4 linear, 0.3 polynomial, 0.3 moving average). These weights balance trend-following with mean-reversion tendencies.
Confidence band calculation uses historical volatility scaled by the square root of time, following the random walk hypothesis. The 1.96 multiplier corresponds to 95% confidence intervals under normal distribution assumptions.
3.2 Forecast Metrics Calculation
The calculate_forecast_metrics function computes comprehensive metrics for forecast evaluation. These metrics enable quantitative comparison of forecasts across different assets and time periods.
Basic forecast metrics include expected return calculated as percentage change from current price to forecast endpoint. The implementation handles percentage calculations carefully to avoid numerical overflow.
Risk-adjusted return divides expected return by historical volatility, providing a standardized measure comparable across assets with different risk profiles. The addition of a small epsilon (1e-6) prevents division by zero.
Forecast Sharpe ratio adapts the traditional Sharpe calculation to forward-looking analysis. The implementation converts forecast returns to daily equivalents and adjusts for risk-free rates, maintaining consistency with historical Sharpe calculations.
Probability of profit uses confidence intervals to estimate the likelihood of positive returns. The algorithm counts periods where the lower confidence band exceeds current price, providing a conservative probability estimate.
Maximum expected drawdown applies drawdown calculation to forecast prices, estimating potential losses within the forecast period. This forward-looking risk metric helps set appropriate position sizes and stop-losses.
The upside/downside ratio compares potential gains (upper band) to potential losses (lower band), providing an asymmetric risk assessment. The implementation handles infinite ratios when downside risk approaches zero.
3.3 Forecast Scoring System
The calculate_forecast_score function implements a sophisticated scoring algorithm that combines multiple forecast quality metrics into a single comparable score.
The scoring system uses component normalization to convert different metrics to a 0-100 scale. Each component uses specific transformation functions designed to map typical metric ranges to scores that reflect investment attractiveness.
Return score transformation assumes returns between -50% and +50% represent the typical range, with linear scaling to 0-100. Values outside this range are capped to prevent single outliers from dominating scores.
Risk-adjusted return scoring centers around zero with a scaling factor of 25, recognizing that risk-adjusted returns typically range from -2 to +2. This normalization maintains score differentiation while handling extreme values.
Sharpe ratio scoring follows similar logic to risk-adjusted returns but with adjusted scaling to reflect the narrower typical range of Sharpe ratios. The transformation preserves the relative ordering of assets while producing intuitive scores.
Probability scoring directly uses percentage values, as these are already on a 0-100 scale. This straightforward approach maintains interpretability of the probability component.
Drawdown scoring inverts the relationship since lower drawdowns are preferable. The algorithm adds drawdown (negative value) to 100 and scales by 5, producing higher scores for smaller drawdowns.
The weighted aggregation combines component scores using predetermined weights that reflect the relative importance of each metric for investment decisions. Risk-adjusted return receives the highest weight (0.25), emphasizing the importance of risk-aware analysis.
Historical performance bonus adds up to 10 points based on historical Sharpe ratio, recognizing that assets with strong historical performance may have persistent advantages. The bonus is capped to prevent historical performance from overwhelming forecast metrics.
4. Visualization Components
4.1 Price Chart Construction
The create_price_chart function builds comprehensive technical analysis charts using Plotly's subplot functionality. The multi-panel layout presents price action, momentum indicators, and volume in an organized, visually appealing format.
The candlestick chart in the main panel provides detailed price information with open, high, low, and close values for each period. This traditional format allows experienced traders to identify patterns and price action characteristics.
Moving average overlays on the price chart show trend direction and potential support/resistance levels. The color coding (orange for SMA 20, red for SMA 50) follows market conventions for easy interpretation.
Bollinger Bands visualization includes shaded areas between bands, making it easy to identify volatility expansion and contraction periods. The semi-transparent fill preserves visibility of underlying price action.
The RSI panel includes horizontal reference lines at 70 and 30, clearly marking overbought and oversold zones. The purple color choice provides good contrast against the white background.
MACD visualization combines line plots for MACD and signal lines with a histogram showing their difference. This multi-layer approach provides complete information about momentum and trend strength.
Volume bars use color coding to indicate up days (green) versus down days (red), providing immediate visual feedback about buying and selling pressure. The semi-transparent rendering prevents volume from obscuring other chart elements.
4.2 Forecast Visualization
The create_forecast_chart function specializes in presenting forecast data alongside historical context. The implementation balances information density with visual clarity.
Historical data display uses a lookback period of 100 periods or available data length, whichever is smaller. This approach ensures sufficient context without overwhelming the forecast visualization.
Forecast line rendering uses dashed lines with markers to distinguish predictions from historical data. The red color provides clear visual separation while maintaining professional appearance.
Confidence band visualization uses dotted lines for boundaries and semi-transparent fill for the area between bands. This design clearly communicates uncertainty while maintaining chart readability.
The unified hover mode enables simultaneous tooltip display across all traces at a given x-coordinate, facilitating comparison between historical data, forecasts, and confidence bounds.
4.3 Ranking Visualizations
The create_forecast_ranking_chart function creates a comprehensive dashboard for comparing forecast metrics across multiple assets using subplot arrangements.
The score ranking bar chart displays top 10 assets sorted by forecast score, providing immediate visual identification of best opportunities. The light blue color scheme maintains consistency with financial applications.
Return versus risk scatter plot maps assets in risk-return space with forecast scores represented by color intensity. This visualization enables efficient frontier analysis and risk-adjusted opportunity identification.
Probability of profit comparison uses green coloring to reinforce positive associations with high-probability opportunities. The bar chart format facilitates quick comparison across assets.
Upside/downside ratio visualization employs coral coloring to distinguish this risk metric from others. The focused display of top 10 assets prevents overcrowding while highlighting best opportunities.
5. Streamlit Application Structure
5.1 User Interface Design
The application configuration establishes a wide layout optimized for financial data display. The expanded sidebar default ensures users immediately see available controls and options.
The sidebar organization groups related functionality logically: data upload, configuration parameters, and metric weighting controls. This hierarchical structure guides users through the analysis workflow naturally.
Interactive weight adjustment sliders in the sidebar enable real-time customization of forecast scoring algorithms. The two-column layout maximizes space efficiency while maintaining readability.
The main content area uses tabs to organize different analysis views, preventing information overload while providing comprehensive functionality. Each tab focuses on a specific aspect of analysis, from individual asset details to portfolio-wide comparisons.
Progress indicators during file processing provide user feedback for potentially long-running operations. The combination of progress bars and status text keeps users informed without cluttering the interface.
5.2 Data Processing Workflow
The file upload handling supports multiple simultaneous uploads, enabling batch processing of entire portfolios. The system maintains separate data structures for each asset while enabling cross-asset comparisons.
Error handling throughout the processing pipeline captures and reports issues without crashing the application. Warning messages are collected and displayed in an expandable section, allowing users to review issues without disrupting workflow.
The processing loop maintains dictionaries of results, metrics, forecasts, and scores for each asset. This organization facilitates both individual asset analysis and portfolio-wide comparisons.
Result storage uses asset names as dictionary keys, enabling efficient lookup and maintaining relationships between different data types (raw data, metrics, forecasts, scores).
5.3 Interactive Analysis Features
Individual analysis tab provides detailed examination of single assets with comprehensive metric displays. The five-column layout for historical metrics and four-column layout for forecast metrics maximizes information density while maintaining readability.
Forecast ranking tab implements sophisticated sorting and filtering to identify best opportunities. The top 3 highlighting with medal emoji creates engaging visual hierarchy while conveying important information.
The multi-asset forecast comparison enables side-by-side analysis of different opportunities. The interactive selection mechanism allows users to focus on specific assets of interest.
Historical performance ranking calculates composite scores using weighted metrics, providing quantitative assessment of past performance. The normalization process ensures fair comparison across assets with different characteristics.
Comprehensive metrics dashboard combines all calculated metrics in a single view with customizable display options. The multi-select interface allows users to focus on specific metrics relevant to their analysis.
Risk analysis features including scatter plots and efficient frontier approximation provide institutional-quality portfolio analysis. The color coding and interactive tooltips enhance data exploration capabilities.
6. Advanced Features and Edge Cases
6.1 Data Quality Management
The system implements multiple layers of data validation to ensure calculation integrity. Numeric conversion with error coercion prevents single bad data points from corrupting analysis.
Missing data handling removes rows with null closing prices while preserving other data where possible. This selective approach maximizes usable data while maintaining calculation validity.
Zero-division protection throughout metric calculations returns sensible default values rather than errors. This approach ensures the application remains stable even with edge-case data.
Infinite value handling in ratios and metrics caps or replaces infinite values with large finite numbers, maintaining numerical stability in subsequent calculations.
6.2 Performance Optimization
Vectorized operations throughout the codebase leverage NumPy and pandas efficiency, avoiding Python loops where possible. This approach ensures scalability to large datasets.
Selective calculation of indicators and metrics prevents unnecessary computation. The system only calculates metrics actually used in visualization or scoring.
DataFrame copying strategy preserves original data while allowing modifications for analysis. This functional approach prevents unintended side effects and maintains data integrity.
Caching potential through Streamlit's session state could further optimize performance for repeated calculations. The current architecture supports easy integration of caching mechanisms.
6.3 Extensibility Considerations
The modular architecture facilitates easy addition of new technical indicators or metrics. New indicators can be added to the TechnicalIndicators class without affecting existing functionality.
The scoring system's weight-based approach allows easy customization for different investment strategies. Users can adjust weights through the UI, and new components can be added to the scoring algorithm.
Visualization components use consistent patterns that simplify adding new chart types or modifying existing ones. The Plotly-based approach provides extensive customization options.
The data processing pipeline can accommodate different data formats with minimal modifications. The flexible column detection and assignment mechanism adapts to various CSV structures.
Conclusion
This comprehensive financial analysis platform represents a sophisticated integration of technical analysis, portfolio theory, and machine learning techniques. The 5000+ word analysis demonstrates the depth and complexity embedded in the seemingly straightforward Streamlit application. The system successfully balances powerful analytical capabilities with user-friendly interfaces, making institutional-grade analysis accessible to individual investors and analysts.
The modular architecture ensures maintainability and extensibility, while the robust error handling and data validation provide reliability in production environments. The combination of traditional technical indicators with advanced portfolio metrics and machine learning-based forecasting creates a unique value proposition for investment analysis.
Future enhancements could include real-time data integration, backtesting capabilities, portfolio optimization algorithms, and machine learning model improvements. The current foundation provides an excellent platform for such extensions while maintaining code quality and user experience standards.


Comments