# pikvik/stocksentimentanalysis architecture

> Architecture diagram of the GitHub repository pikvik/stocksentimentanalysis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pikvik/stocksentimentanalysis
- Repository: https://github.com/pikvik/stocksentimentanalysis

## Overview

##### Core project type
- This is a web-based stock analysis application built with Streamlit.
- The purpose is to provide real-time financial insights by fetching stock data and performing sentiment analysis using FinBERT.

##### Break down the file structure to highlight main components
- Frontend/UI: The Streamlit interface is driven by main.py and configured via .streamlit/config.toml along with custom styling from styles/custom.css.
- Data Fetching & Processing: In the utils directory, data_fetcher.py grabs real-time data from Yahoo Finance, chart_helper.py prepares interactive charts (likely using Plotly for candlestick and volume visualizations), and sentiment_analyzer.py performs news sentiment analysis using FinBERT and PyTorch.
- Configuration/Deployment: Files such as .replit, replit.nix, and uv.lock hint at the development and deployment setup but are not core to the runtime architecture.

##### Components and interactions
a. Main Components:
- User Interface (UI): Represented by the Streamlit web frontend (main.py, .streamlit/config.toml, styles).
- Data Module: Includes data_fetcher (for Yahoo Finance stock data) and chart_helper (for generating interactive price charts).
- Sentiment Analysis Module: Contains sentiment_analyzer, which integrates FinBERT through PyTorch.
- External Services: Show an external component for Yahoo Finance (via yfinance) and any external news data source if applicable.

b. Relationships and Data Flows:
- Flow from the UI (main.py) to the Data Module (data_fetcher) to fetch stock data from Yahoo Finance.
- Flow from main.py to the Chart Helper, which uses the fetched data to generate candlestick and volume charts.
- Similarly, flow from main.py to the Sentiment Analysis Module, which processes news sentiment.
- Finally, data flows back to the UI for rendering interactive charts and sentiment results.

c. Architectural Patterns and Design Principles:
- Separation of Concerns: Distinct modules for data fetching, chart generation, and sentiment analysis.
- Modular Design: Each util is responsible for a specific functional area, making the system easier to extend or update.
- UI-Logic Separation: The main.py acts as an orchestrator while delegating work to separate functions/modules.

d. Technologies, Frameworks, and Libraries:
- Streamlit: For the web-based user interface.
- yfinance: For interfacing with Yahoo Finance to retrieve real-time stock data.
- Plotly: Likely used in chart_helper for generating interactive charts.
- Transformers (FinBERT) & PyTorch: For implementing sentiment analysis on financial news.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Browser["User / Browser"]:::external

    subgraph "UI - Streamlit Frontend"
        UI_Main["main.py"]:::ui
        UI_Config[".streamlit/config.toml"]:::ui
        UI_Styles["styles/custom.css"]:::ui
    end

    DataFetcher["Data Fetcher Module"]:::module
    ChartHelper["Chart Helper Module"]:::module
    SentimentAnalysis["Sentiment Analysis Module"]:::module

    Yahoo["Yahoo Finance API"]:::external
    FinBERT["FinBERT Model"]:::external

    %% User flow to UI
    Browser -->|"input"| UI_Main

    %% UI interactions for data fetching and sentiment analysis
    UI_Main -->|"fetch_stock_data"| DataFetcher
    UI_Main -->|"analyze_news"| SentimentAnalysis

    %% Data Flow: Data Fetcher to External API and Chart Helper
    DataFetcher -->|"stock_data"| Yahoo
    DataFetcher -->|"processed_data"| ChartHelper
    ChartHelper -->|"interactive_charts"| UI_Main

    %% Sentiment Flow: Sentiment Analysis interacts with FinBERT and returns results to UI
    SentimentAnalysis -->|"process_with_finbert"| FinBERT
    SentimentAnalysis -->|"sentiment_results"| UI_Main

    %% Click Events
    click UI_Main "https://github.com/pikvik/stocksentimentanalysis/blob/main/main.py"
    click UI_Config "https://github.com/pikvik/stocksentimentanalysis/blob/main/.streamlit/config.toml"
    click UI_Styles "https://github.com/pikvik/stocksentimentanalysis/blob/main/styles/custom.css"
    click DataFetcher "https://github.com/pikvik/stocksentimentanalysis/blob/main/utils/data_fetcher.py"
    click ChartHelper "https://github.com/pikvik/stocksentimentanalysis/blob/main/utils/chart_helper.py"
    click SentimentAnalysis "https://github.com/pikvik/stocksentimentanalysis/blob/main/utils/sentiment_analyzer.py"

    %% Styles
    classDef ui fill:#B3E5FC,stroke:#0D47A1,stroke-width:2px;
    classDef module fill:#FFECB3,stroke:#FF6F00,stroke-width:2px;
    classDef external fill:#C8E6C9,stroke:#1B5E20,stroke-width:2px;
```
