# steveman1123/multistonk architecture

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

- Interactive diagram: https://gitdiagram.com/steveman1123/multistonk
- Repository: https://github.com/steveman1123/multistonk

## Overview

##### Project Type and Purpose
- The project is a full-stack automated stock trading program that uses multiple algorithms/strategies.
- It is designed to interact with the Alpaca API for trading actions. It processes configuration files to set up trading parameters and uses multiple algorithm modules (located in ./algos) as well as specialized utilities for Alpaca and Nasdaq API interactions.

##### File Structure
- Key files and directories:
- The top-level Python scripts (e.g., multistonk.py, dryrun.py) contain the main running loops and support functions.
- The ./algos directory holds individual algorithm modules (each implementing a set of required functions such as init, getList, goodSells, sellDn, sellUp, and sellUpDn).
- The ./configs directory stores user-modifiable settings that drive the execution and behavior of the algorithms and the trading process.
- The modules (alpacafxns.py and ndaqfxns.py) encapsulate API-specific functions, making clear separations between general stock data processing and API-dependent operations.
- Other components include stockStuff (holding API key files and historical data) and miscellaneous testing or debug scripts (e.g., debugTesting.py).

a. Main Components:
- Main Application Driver:
- multistonk.py: Orchestrates the trading program lifecycle (running until the portfolio value drops a certain percentage), reading config files, and calling respective algorithm modules.
- Algorithm Modules:
- Directory: ./algos. Each file represents a unique trading strategy. They must implement key functions to determine buying and selling decisions.
- Configuration Management:
- Directory: ./configs. Contains various config files (e.g., multi.config) which define user settings, triggering live or paper trading, algorithm-specific thresholds, etc.
- API Integration Layers:
- alpacafxns.py: Handles direct communication with Alpaca's markets API for executing trades.
- ndaqfxns.py: Responsible for functions that compute or request data from the Nasdaq API without requiring API keys.
- Stock Data and API Key Management:
- Directory: ./stockStuff holds API keys and historical trading data which the system uses to inform trading decisions.
- Utilities and Support Functions:
- Additional helper scripts (e.g., tradeData.py, getPos.py, otherfxns.py) are likely used for data handling, position tracking, and executing various helper tasks.

b. Relationships and Interactions:
- The Main Application (multistonk.py) drives the overall process, reading config files from ./configs, then sequentially or concurrently invoking different algorithm modules from ./algos.
- Each algorithm module uses its initialization and decision functions to interact with the main driver, suggesting to the driver which stocks to buy or sell.
- API function modules (alpacafxns.py and ndaqfxns.py) are called by the main application and/or algorithms to perform data retrieval and transaction execution.
- The stockStuff directory (after being relocated as instructed) provides essential data (e.g., API keys or historical stock data) needed by these API and trading modules.
- The program also includes dry-run testing utilities (e.g., dryrun.py, algoAnalysis/ scripts) to simulate trading scenarios without risk.

c. Architectural Patterns and Design Principles:
- Modularity: Each algorithm exists as its own module, making it straightforward to add, modify, or remove strategies.
- Separation of Concerns: Distinct modules deal with API integration (alpacafxns.py, ndaqfxns.py), configuration (configs/), and core trading logic (multistonk.py and related scripts).
- Extensibility: The architecture allows for new configuration files or new algorithms to be added without major refactoring.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration Management
    subgraph "Configuration Management"
        CFG["Configuration Files"]:::config
    end

    %% Main Trading Engine
    subgraph "Main Trading Engine"
        MTE["Main Trading Engine"]:::main
    end

    %% Algorithm Modules
    subgraph "Algorithm Modules"
        DIP["DipBot"]:::main
        EMA["EMA Strategy"]:::main
        OTH["Other Algorithms"]:::main
    end

    %% API Integration Layers
    subgraph "API Integration Layers"
        ALPACA["Alpaca API"]:::api
        NASDAQ["Nasdaq API"]:::api
    end

    %% Stock Data & API Keys
    subgraph "Stock Data & API Keys"
        STOCK["Stock Data & API Keys"]:::data
        KEYS["API Keys"]:::data
    end

    %% Utilities & Support Functions
    subgraph "Utilities & Support"
        DRY["Dry Run"]:::util
        GETPOS["Position Tracker"]:::util
        TRADEDATA["Trade Data Handler"]:::util
        OTHERFXNS["Miscellaneous Functions"]:::util
        DEBUG["Debug Testing"]:::util
    end

    %% Data Flow Connections
    CFG -->|"provides"| MTE
    MTE -->|"triggers"| DIP
    MTE -->|"triggers"| EMA
    MTE -->|"triggers"| OTH

    %% API Invocations
    MTE -->|"invokes"| ALPACA
    MTE -->|"invokes"| NASDAQ
    DIP -->|"calls"| ALPACA
    EMA -->|"calls"| NASDAQ

    %% External Data Dependency
    ALPACA -->|"fetches"| STOCK
    NASDAQ -->|"fetches"| STOCK
    STOCK -->|"includes"| KEYS

    %% Utilities Connections
    MTE -->|"executes"| DRY
    MTE -->|"tracks"| GETPOS
    MTE -->|"handles"| TRADEDATA
    MTE -->|"assists"| OTHERFXNS
    MTE -->|"tests"| DEBUG

    %% Click Events
    click MTE "https://github.com/steveman1123/multistonk/blob/main/multiStonk.py"
    click DIP "https://github.com/steveman1123/multistonk/blob/main/algos/dipbot.py"
    click EMA "https://github.com/steveman1123/multistonk/blob/main/algos/ema.py"
    click CFG "https://github.com/steveman1123/multistonk/tree/main/configs/"
    click ALPACA "https://github.com/steveman1123/multistonk/blob/main/alpacafxns.py"
    click NASDAQ "https://github.com/steveman1123/multistonk/blob/main/ndaqfxns.py"
    click STOCK "https://github.com/steveman1123/multistonk/tree/main/stockStuff/"
    click KEYS "https://github.com/steveman1123/multistonk/blob/main/stockStuff/apikeys/keys.txt"
    click DRY "https://github.com/steveman1123/multistonk/blob/main/dryrun.py"
    click GETPOS "https://github.com/steveman1123/multistonk/blob/main/getPos.py"
    click TRADEDATA "https://github.com/steveman1123/multistonk/blob/main/tradeData.py"
    click OTHERFXNS "https://github.com/steveman1123/multistonk/blob/main/otherfxns.py"
    click DEBUG "https://github.com/steveman1123/multistonk/blob/main/debugTesting.py"

    %% Styles
    classDef main fill:#6BA5D7,stroke:#1B4F72,color:#ffffff,stroke-width:2px;
    classDef config fill:#82E0AA,stroke:#196F3D,color:#ffffff,stroke-width:2px;
    classDef api fill:#F5B041,stroke:#BA4A00,color:#ffffff,stroke-width:2px;
    classDef data fill:#AF7AC5,stroke:#6C3483,color:#ffffff,stroke-width:2px;
    classDef util fill:#F39C12,stroke:#B9770E,color:#ffffff,stroke-width:2px;
```
