Skip to content

mp-c0de/StockPortfolioMonitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StockPortfolioMonitor

A real-time stock market tracking application with Alpha Vantage API integration, featuring mock data fallback and elegant portfolio monitoring interface.

Overview

StockPortfolioMonitor is a financial tracking app that monitors stock prices in real-time using the Alpha Vantage API. The app demonstrates professional API integration, error handling with graceful fallback to mock data, and modern SwiftUI design patterns for displaying financial information.

Features

  • Real-Time Stock Prices: Live price updates via Alpha Vantage API
  • Mock Data Fallback: 20 pre-configured popular stocks for offline/demo mode
  • Pull-to-Refresh: Manual price updates
  • Change Indicators: Visual up/down price change display
  • Add/Remove Stocks: Portfolio customization by stock symbol
  • Stock Limit Tracking: Monitor API usage limits
  • Error Handling: Graceful degradation when API is unavailable

Technical Stack

  • Framework: SwiftUI
  • Networking: URLSession with async/await
  • API: Alpha Vantage Financial Data API
  • iOS Version: iOS 15+
  • Architecture: MVVM with StockService and StockListViewModel
  • Concurrency: Swift async/await patterns

Key Learning Concepts

This project demonstrates:

  • RESTful API integration
  • Async/await networking patterns
  • JSON decoding with Codable
  • MVVM architecture
  • Error handling strategies
  • Mock data for testing/fallback
  • Pull-to-refresh implementation

Project Structure

StockPortfolioMonitor/
├── Models/
│   └── Stock.swift                # Stock data model
├── Services/
│   └── StockService.swift         # API integration
├── ViewModels/
│   └── StockListViewModel.swift   # Business logic
├── Views/
│   ├── ContentView.swift          # Main stock list
│   └── StockRowView.swift         # Individual stock display
└── StockPortfolioMonitorApp.swift

API Integration

Alpha Vantage API

// Endpoint: https://www.alphavantage.co/query
// Function: GLOBAL_QUOTE
// Symbol: Stock ticker (e.g., AAPL, GOOGL)

Mock Data

Pre-configured stocks for demo/offline mode:

  • Tech: AAPL, GOOGL, MSFT, AMZN, META, TSLA
  • Finance: JPM, BAC, GS
  • Retail: WMT, HD
  • Entertainment: DIS, NFLX
  • Others: JNJ, PG, KO, PEP, NKE, NVDA, AMD

Code Highlights

Stock Service

class StockService {
    private let apiKey = "YOUR_API_KEY"
    private let baseURL = "https://www.alphavantage.co/query"

    func fetchStockPrice(symbol: String) async throws -> Stock {
        // API call implementation
        // JSON parsing
        // Error handling
    }

    func getMockStock(symbol: String) -> Stock {
        // Fallback mock data
    }
}

MVVM Pattern

ViewModel handles business logic, API calls, and state management, keeping views clean and testable.

Async/Await

Modern Swift concurrency for clean asynchronous code without callback hell.

Skills Demonstrated

  • RESTful API consumption
  • Swift Concurrency (async/await)
  • Codable protocol for JSON parsing
  • MVVM architectural pattern
  • Error handling and recovery
  • Network state management
  • Pull-to-refresh UI pattern
  • Mock data strategies

Data Model

Stock

struct Stock: Identifiable, Codable {
    let id: UUID
    let symbol: String
    let price: Double
    let change: Double
    let changePercent: Double
}

API Response Handling

  1. Success: Display real-time data
  2. API Limit: Fallback to mock data
  3. Network Error: Show cached/mock data
  4. Invalid Symbol: Error message

Use Cases

  • Personal stock portfolio tracking
  • Financial app prototypes
  • API integration learning
  • Real-time data display patterns
  • Investment monitoring

API Limitations

  • Free Tier: 5 API calls per minute, 500 per day
  • Rate Limiting: Implemented with graceful fallback
  • Mock Mode: Allows unlimited testing

Future Enhancements

  • Historical price charts with Charts framework
  • Portfolio value calculation
  • News integration for stock symbols
  • Price alerts and notifications
  • Multiple currency support
  • Favorites/watchlist organization
  • Detailed stock information view
  • Performance analytics dashboard

Setup Instructions

  1. Get free API key from Alpha Vantage
  2. Replace YOUR_API_KEY in StockService.swift
  3. Build and run

Note: App works in mock mode without API key for demonstration purposes.


Author: Martynas Prascevicius Contact: mpcode@icloud.com Purpose: Learning project demonstrating API integration and financial data handling

About

Real-time stock tracking with Alpha Vantage API - SwiftUI, Async/Await, URLSession

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages