A production-grade hybrid C++/Python application for real-time stock market analysis using advanced data structures, algorithms, and technical indicators.
This project demonstrates enterprise-level software engineering by combining C++17's performance with Python's ecosystem to deliver:
- Technical Indicators: SMA, EMA, RSI calculations
- Optimal Trading Strategy: Find best buy/sell points using dynamic programming
- Top K Analysis: Identify highest daily gains using min-heap
- Real-time Data: Fetch live stock prices from Alpha Vantage API
- Visualization: Generate trading charts with matplotlib
- Data Structures: Min-Heap (Priority Queue), Dynamic Arrays
- Algorithms: Kadane's Algorithm Variant, Wilder's Smoothing (RSI), Exponential Smoothing (EMA)
- Design Patterns: Hybrid Architecture, Separation of Concerns
- Security: Input sanitization, path traversal prevention
- Best Practices: Professional logging, error handling, type hints
Stock Analysis/
├── src/
│ ├── analysis.cpp # C++ algorithm implementations
│ ├── analysis.h # Function declarations and structs
│ └── bindings.cpp # pybind11 Python-C++ interface
├── build/ # CMake build output
├── main.py # Python orchestration and API integration
├── .env # API key configuration
├── CMakeLists.txt # Build configuration
└── system.log # Application logs
## 🚀 Setup & Installation
### Prerequisites
```bash
# Install Python dependencies
pip install requests python-dotenv matplotlib
# Ensure you have CMake and a C++17 compiler installed
- Create a
.envfile in the project root:
ALPHA_VANTAGE_KEY=your_api_key_here- Get a free API key from Alpha Vantage
mkdir build
cd build
cmake ..
cmake --build . --config Debugpython main.pyEnter Stock Symbol (e.g., IBM): IBM
2025-11-23 20:01:22 [INFO] Fetching data for symbol: IBM
2025-11-23 20:01:22 [INFO] Successfully retrieved 100 data points.
========================================
ANALYSIS REPORT: IBM
========================================
Latest Price: $297.44
SMA (5-day): $292.70
EMA (20-day): $298.88
RSI (14-day): 50.20
Top 3 Daily Jumps: ['$22.46', '$13.91', '$7.56']
========================================
Max Profit: $80.21
Buy Day: Index 27 (@ $234.77)
Sell Day: Index 92 (@ $314.98)
- Calculates average of last N closing prices
- Algorithm: Sliding window summation
- Time Complexity: O(n)
- Use Case: Identify trend direction
- Weighted average giving more importance to recent prices
- Algorithm: Exponential smoothing with multiplier 2/(period+1)
- Time Complexity: O(n)
- Use Case: More responsive to recent price changes than SMA
- Momentum oscillator measuring speed and magnitude of price changes
- Algorithm: Wilder's smoothing method
- Range: 0-100 (>70 overbought, <30 oversold)
- Time Complexity: O(n)
- Use Case: Identify overbought/oversold conditions
- Finds maximum profit from single transaction
- Algorithm: Modified Kadane's algorithm with index tracking
- Time Complexity: O(n)
- Space Complexity: O(1)
- Returns: Profit amount, buy day index, sell day index
- Identifies K largest day-over-day price increases
- Data Structure: Min-heap (priority queue)
- Time Complexity: O(n log k)
- Space Complexity: O(k)
- Use Case: Spot high volatility days
TradeResult findMaxProfit(const vector<double>& prices) {
double min_price = prices[0];
int potential_buy_index = 0;
double max_profit = 0.0;
int best_buy_index = -1, best_sell_index = -1;
for (int i = 1; i < prices.size(); ++i) {
double current_profit = prices[i] - min_price;
if (current_profit > max_profit) {
max_profit = current_profit;
best_sell_index = i;
best_buy_index = potential_buy_index;
}
if (prices[i] < min_price) {
min_price = prices[i];
potential_buy_index = i;
}
}
return {max_profit, best_buy_index, best_sell_index};
}vector<double> calculateRSI(const vector<double>& prices, int period) {
// 1. Calculate initial average gain/loss
for (int i = 1; i <= period; ++i) {
double change = prices[i] - prices[i-1];
if (change > 0) avg_gain += change;
else avg_loss += abs(change);
}
avg_gain /= period; avg_loss /= period;
// 2. Apply Wilder's smoothing for subsequent values
for (int i = period + 1; i < prices.size(); ++i) {
avg_gain = ((avg_gain * (period-1)) + current_gain) / period;
avg_loss = ((avg_loss * (period-1)) + current_loss) / period;
double rs = avg_gain / avg_loss;
double rsi = 100.0 - (100.0 / (1.0 + rs));
}
}vector<double> findTopKDailyGains(const vector<double>& prices, int k) {
priority_queue<double, vector<double>, greater<double>> min_heap;
for (int i = 1; i < prices.size(); i++) {
double gain = prices[i] - prices[i-1];
min_heap.push(gain);
if (min_heap.size() > k) min_heap.pop();
}
return extract_heap_to_vector(min_heap);
}| Indicator | Value | Interpretation |
|---|---|---|
| SMA | $292.70 | Average price over last 5 days |
| EMA | $298.88 | Recent prices weighted higher; above SMA suggests upward momentum |
| RSI | 50.20 | Neutral zone (30-70); not overbought or oversold |
| Max Profit | $80.21 | Best single buy/sell opportunity in dataset |
- STL Containers:
vector,priority_queue - Algorithms: Custom implementations of financial indicators
- Memory Management: RAII, smart pointers
- Build System: CMake 3.15+
- API Client:
requestswith timeout and error handling - Visualization:
matplotlibfor chart generation - Configuration:
python-dotenvfor environment variables - Logging: Built-in
loggingmodule with file and console handlers
- pybind11: Zero-overhead C++/Python bindings
- Type Conversion: Automatic
std::vector↔ Python list conversion - Struct Binding:
TradeResultexposed as Python object
import stock_analyzer
# Calculate Simple Moving Average
sma = stock_analyzer.calculate_sma(prices, window=5)
# Returns: float (average of last 5 prices)
# Calculate Exponential Moving Average
ema_values = stock_analyzer.calculate_ema(prices, period=20)
# Returns: list[float] (EMA for each price point)
# Calculate Relative Strength Index
rsi_values = stock_analyzer.calculate_rsi(prices, period=14)
# Returns: list[float] (RSI values 0-100)
# Find optimal buy/sell strategy
trade = stock_analyzer.find_max_profit(prices)
# Returns: TradeResult(max_profit, buy_day_index, sell_day_index)
# Get top K daily gains
top_gains = stock_analyzer.find_top_k_gains(prices, k=3)
# Returns: list[float] (top 3 daily price increases)- Min-Heap: Efficient top-K element tracking (common in "Top K" problems)
- Dynamic Arrays: Vector operations and memory management
- Kadane's Algorithm: Maximum subarray variant for stock profit
- Sliding Window: Moving average calculations
- Exponential Smoothing: Time series analysis technique
- Hybrid Architecture: When to use C++ vs Python
- API Integration: Rate limiting, error handling, retries
- Security: Input validation, path traversal prevention
- Logging: Production-grade observability
- Build Systems: CMake for cross-platform C++ projects
- Foreign Function Interface: pybind11 for language interop
- Configuration Management: Environment variables, .env files
- Error Handling: Graceful degradation, fail-fast principles
Module not found error:
# Rebuild the C++ module
cd build
cmake --build . --config DebugAPI rate limit:
- Free tier: 25 requests/day
- Wait 24 hours or upgrade to premium
Missing API key:
- Ensure
.envfile exists withALPHA_VANTAGE_KEY=your_key
- MACD and Bollinger Bands indicators
- Multi-stock comparison dashboard
- SQLite database for historical data caching
- WebSocket for real-time streaming
- Backtesting framework with performance metrics
- REST API with FastAPI
- Docker containerization
Part of DSA Projects - FAANG Interview Preparation Roadmap
Project #13: Stock Price Analysis (Phase 3: Advanced Intermediate)
Status: ✅ Completed
This project demonstrates production-ready code with enterprise-level practices including security, logging, error handling, and performance optimization.