# nagarajugunda/pyalgomate architecture

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

- Interactive diagram: https://gitdiagram.com/nagarajugunda/pyalgomate
- Repository: https://github.com/nagarajugunda/pyalgomate

## Overview

##### Main Components
- Core Engine and Modules: At the heart is the pyalgomate package. Inside, you have:
- Core functionalities in the “pyalgomate/core” module (e.g., backtesting routines, event dispatching, strategy execution, order management).
- Analyzers (“pyalgomate/analyzers”) and technical modules (“pyalgomate/technical”) for indicators and analysis.
- Broker Integrations: Under “pyalgomate/brokers” you have broker-specific submodules for Finvasia, Kotak, and Zerodha. These handle communication with broker APIs, data feeds, order execution, and even external websocket clients.
- Strategy Implementations: The “pyalgomate/strategies” directory contains various reference strategies that implement different trading logics (e.g., StraddleIntraday, SuperTrend, etc.). This is a key layer that interacts with the core.
- Data Handling and Feeds: The “pyalgomate/backtesting” and “pyalgomate/barfeed” modules manage the historical and live data feeds, including custom CSV and DataFrame feeds.
- Command-Line Interface (CLI): Files like “pyalgomate/cli.py” provide the mechanism to run backtests and trading sessions.
- User Interfaces:
- A Flet-based UI is evident in the “pyalgomate/ui/flet” and “flet” directories, which are used for real-time view of strategies and performance.
- A Streamlit-based UI is set up in the “streamlit” directory, offering additional visualization and analysis capabilities (e.g., backtest analyzer).
- Auxiliary Components: Scripts like “LiveTest.py”, “Runner.py”, and various service and configuration files (e.g., pyalgomate.service, Straddle.yaml) support deployment and orchestration.

##### Interactions and Data Flow
- Core Processing: Illustrate how the incoming data flows into the “pyalgomate/core” where events are dispatched, orders are managed, and strategies are executed.
- Broker Communication: Show the interaction between the core engine and the broker-specific modules (for Finvasia, Zerodha, Kotak) which send orders and receive updates.
- Interfaces:
- The CLI path where users run backtests or live trading sessions through Python command-line commands.
- The UI path with two separate flows: one for Flet-based UI (for trading views and strategy control) and one for Streamlit (for backtest result analysis and visualizations).

##### Architectural Patterns and Design Principles
- Event-Driven Architecture: Emphasize the event-based system that triggers strategy execution, order dispatching, and data updates.
- Modular Design: The clear separation among core, broker interfaces, strategies, and UI components indicates a modular design – each component can be developed, tested, and maintained relatively independently.
- Extensibility: Consider showing extension points (i.e., plug-and-play capabilities for new strategies or broker integrations).
- Asynchronous Operations: The README mentions asynchronous operations in recent updates, which should be highlighted if applicable, especially in data feed and UI updates.

##### Specific Recommendations for PyAlgoMate
a. Main Workflow: Start with market data input (historical or live), then pass through the core engine’s event dispatcher (interaction with “pyalgomate/core/backtesting.py” and “pyalgomate/core/broker.py”), followed by strategy evaluation (from “pyalgomate/strategies”) and finally broker order execution (from “pyalgomate/brokers”).
b. UI Options: Clearly separate the UI branch into Flet (for interactive strategy management) and Streamlit (for analyzing backtest results), indicating how both connect back to the core engine.
c. Dual Operation Modes: Note both CLI and UI modes, highlighting that they serve the same core trading functionalities but differ in the presentation layer.
d. External Dependencies: Optionally, add a note representing auxiliary services or scripts like “LiveTest.py”, “Runner.py”, and local configuration files that facilitate deployment and running of services (e.g., pyalgomate.service).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Data Source
    MD("Market Data"):::data

    %% Data Feeds Layer
    subgraph "Data Feeds"
        HD("Historical Data Feed (CSV/DataFrame)"):::data
        LD("Live Data Feed (Bar Aggregation)"):::data
    end

    %% Core Processing Layer
    subgraph "Core Modules"
        CE("Core Engine (Event Dispatcher & Order Mgmt)"):::core
        AN("Analyzers"):::core
        TM("Technical Modules"):::core
    end

    %% Strategy Layer
    ST("Strategy Implementations"):::strategy
    SU("Strategy Utilities"):::strategy

    %% Broker Integration Layer
    subgraph "Broker Integrations"
        FV("Finvasia Broker"):::broker
        KT("Kotak Broker"):::broker
        ZD("Zerodha Broker"):::broker
    end

    %% CLI Layer
    CLI("CLI Interface"):::cli

    %% UI Layer
    subgraph "User Interfaces"
        FI("Flet UI (Internal)"):::ui
        FS("Flet UI (Standalone)"):::ui
        STUI("Streamlit UI"):::ui
    end

    %% Auxiliary Components
    AUX("Deployment & Config Scripts"):::aux

    %% Connections - Data Flow
    MD --> HD
    MD --> LD
    HD --> CE
    LD --> CE

    %% Core Module Interactions
    CE --> AN
    CE --> TM

    %% Core to Strategies and then Broker Execution
    CE --> ST
    ST --> SU
    ST --> FV
    ST --> KT
    ST --> ZD

    %% Broker Feedback to Core (Order updates)
    FV --> CE
    KT --> CE
    ZD --> CE

    %% Core Engine serving Interfaces
    CE --> CLI
    CE --> FI
    CE --> FS
    CE --> STUI

    %% Auxiliary scripts support Core operations
    AUX -.-> CE

    %% Click Events
    click HD "https://github.com/nagarajugunda/pyalgomate/tree/develop/pyalgomate/backtesting"
    click LD "https://github.com/nagarajugunda/pyalgomate/tree/develop/pyalgomate/barfeed"
    click CE "https://github.com/nagarajugunda/pyalgomate/blob/develop/pyalgomate/core/backtesting.py"
    click AN "https://github.com/nagarajugunda/pyalgomate/tree/develop/pyalgomate/analyzers"
    click TM "https://github.com/nagarajugunda/pyalgomate/tree/develop/pyalgomate/technical"
    click ST "https://github.com/nagarajugunda/pyalgomate/tree/develop/pyalgomate/strategies"
    click SU "https://github.com/nagarajugunda/pyalgomate/blob/develop/pyalgomate/strategy/position.py"
    click FV "https://github.com/nagarajugunda/pyalgomate/tree/develop/pyalgomate/brokers/finvasia"
    click KT "https://github.com/nagarajugunda/pyalgomate/tree/develop/pyalgomate/brokers/kotak"
    click ZD "https://github.com/nagarajugunda/pyalgomate/tree/develop/pyalgomate/brokers/zerodha"
    click CLI "https://github.com/nagarajugunda/pyalgomate/blob/develop/pyalgomate/cli.py"
    click FI "https://github.com/nagarajugunda/pyalgomate/tree/develop/pyalgomate/ui/flet"
    click FS "https://github.com/nagarajugunda/pyalgomate/tree/develop/flet"
    click STUI "https://github.com/nagarajugunda/pyalgomate/tree/develop/streamlit"
    click AUX "https://github.com/nagarajugunda/pyalgomate/blob/develop/LiveTest.py"

    %% Style Classes
    classDef data fill:#ccf,stroke:#333,stroke-width:2px;
    classDef core fill:#cfc,stroke:#333,stroke-width:2px;
    classDef strategy fill:#ff9,stroke:#333,stroke-width:2px;
    classDef broker fill:#fdd,stroke:#333,stroke-width:2px;
    classDef cli fill:#fc9,stroke:#333,stroke-width:2px;
    classDef ui fill:#f9c,stroke:#333,stroke-width:2px;
    classDef aux fill:#ddd,stroke:#333,stroke-width:2px;
```
