# jononon/algo-trading-schwab architecture

> Architecture diagram of the GitHub repository jononon/algo-trading-schwab, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jononon/algo-trading-schwab
- Repository: https://github.com/jononon/algo-trading-schwab

## Overview

##### Main Components

a) AWS Cloud Environment (us-west-1):
- AWS services used

b) External Components:
- Schwab API (in Phoenix, AZ)
- Market Data Sources (for ETF price data)

##### Detailed Component Breakdown

a) AWS Components:
- Lambda Function (Central component)
- Main.py as the entry point
- To supporting modules (auth.py, schwab.py, dynamodb.py, ssm.py)

- CloudWatch
- Both logging and alarm flows

- DynamoDB
- Trade information storage

- Systems Manager (SSM)
- Parameter store for Schwab credentials

b) Trading Logic Flow:
- ETF evaluation process

**[Recommended Layout]**

**┌─────────────────────────────── AWS Region (us-west-1) ──────────────────────────────┐**

- ┌─────────────┐    ┌─────────────┐                                                │
- CloudWatch  │◄───┤   Lambda    │                                                │
- (Monitoring)│    │  Function   │◄──────────────┐                                │
- ┘    │ (main.py)   │              │                                │
- ┬───────┘              │                                │

- ┌──────▼───────┐    ┌────────┴───────┐                        │
- ┌─────────────┐   │   Trading    │    │      SSM       │                        │
- DynamoDB   │◄──┤    Logic     │    │ (Credentials)  │                        │
- (Storage)  │   │ (schwab.py)  │    └────────────────┘                        │
- ┘   └──────────────┘                                              │

- ┼──────────────────────────────────────────────────────┘

- ┌───────▼──────┐
- Schwab API  │
- (Phoenix, AZ)│
- ┘

##### Key Flows to Highlight

- Trigger Flow: Show the daily market open trigger from CloudWatch Events
- Authentication Flow: Demonstrate the credential retrieval from SSM to Schwab API
- Trading Flow:
- Market data retrieval
- Strategy evaluation
- Trade execution
- Trade recording in DynamoDB

##### Additional Elements

- Timing indicators for the daily execution
- Error handling paths
- Monitoring and alerting flows

##### Important Notes

- Serverless nature of the architecture
- Clear separation between AWS and external services
- Security boundary between SSM and the Schwab API
- Latency considerations due to geographic location

##### Technical Details

- Python runtime version
- Serverless framework deployment flow
- DynamoDB table structure indicators
- CloudWatch alarm thresholds
- API rate limits and constraints

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Define styles
    classDef aws fill:#FF9900,stroke:#232F3E,color:white
    classDef external fill:#1E88E5,stroke:#0D47A1,color:white
    classDef code fill:#4CAF50,stroke:#1B5E20,color:white
    classDef data fill:#E91E63,stroke:#880E4F,color:white
    classDef config fill:#9C27B0,stroke:#4A148C,color:white

    subgraph AWS["AWS Region (us-west-1)"]
        CW[CloudWatch]:::aws
        Lambda["Lambda Function\n(main.py)"]:::aws
        SSM["Systems Manager\n(Parameter Store)"]:::aws
        DDB[DynamoDB]:::aws
        
        subgraph TradingLogic["Trading Logic"]
            Auth["Authentication\n(auth.py)"]:::code
            SchwabInt["Schwab Interface\n(schwab.py)"]:::code
            DDBInt["DynamoDB Interface\n(dynamodb.py)"]:::code
            SSMInt["SSM Interface\n(ssm.py)"]:::code
        end
        
        Config["serverless.yml"]:::config
        CICD[".github/workflows"]:::config
        Deps["requirements.txt"]:::config
        Docs["readme.md"]:::config
    end
    
    External["Schwab API\n(Phoenix, AZ)"]:::external
    MarketData["Market Data Sources"]:::external
    
    %% Define relationships
    CW --> Lambda
    Lambda --> Auth
    Auth --> SSM
    Auth --> SchwabInt
    SchwabInt --> External
    Lambda --> DDBInt
    DDBInt --> DDB
    SSM --> SSMInt
    MarketData --> SchwabInt
    
    %% Click events for component mapping
    click Lambda "https://github.com/jononon/algo-trading-schwab/blob/master/main.py"
    click Auth "https://github.com/jononon/algo-trading-schwab/blob/master/auth.py"
    click SchwabInt "https://github.com/jononon/algo-trading-schwab/blob/master/schwab.py"
    click DDBInt "https://github.com/jononon/algo-trading-schwab/blob/master/dynamodb.py"
    click SSMInt "https://github.com/jononon/algo-trading-schwab/blob/master/ssm.py"
    click Config "https://github.com/jononon/algo-trading-schwab/blob/master/serverless.yml"
    click CICD "https://github.com/jononon/algo-trading-schwab/blob/master/.github/workflows/serverless-deploy.yml"
    click Deps "https://github.com/jononon/algo-trading-schwab/blob/master/requirements.txt"
    click Docs "https://github.com/jononon/algo-trading-schwab/blob/master/readme.md"
    
    %% Legend
    subgraph Legend
        AWS_Service["AWS Service"]:::aws
        External_Service["External Service"]:::external
        Code_Module["Code Module"]:::code
        Data_Store["Data Store"]:::data
        Configuration["Configuration"]:::config
    end
```
