# maxnardone/scalable-cloud-api-for-trading-analysis architecture

> Architecture diagram of the GitHub repository maxnardone/scalable-cloud-api-for-trading-analysis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/maxnardone/scalable-cloud-api-for-trading-analysis
- Repository: https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis

## Overview

##### Project Type and Purpose
- Cloud-native, multi-cloud Flask API for trading-strategy risk/profit analysis
- Uses Monte Carlo simulations for risk, integrates AWS Lambda (serverless), EC2 (heavy compute), and Google App Engine (Flask hosting + static/chart storage)

##### File Structure
- EC2 instance code/monte_carlo.py → compute logic deployed on EC2
- Lambda functions/
- MonteCarloSimulation1.py – warm-up parallel simulation
- create_audit.py, read_audit.py – audit log operations
- create_ec2_instance.py, terminate_instances.py – EC2 lifecycle management
- Root
- index.py – Flask app (API endpoints)
- app.yaml – GAE deployment config
- requirements.txt – Python dependencies
- static/ (generated_risk_chart.html, styles.css)
- templates/ (risk_chart.html)

##### README for Insights
- 15 REST endpoints for orchestration (/warmup, /analyse, /get_*, /terminate, etc.)
- Charts stored temporarily on GAE
- Auditing, cost/time reporting

##### Main Components
a. Client Layer
- Browser or HTTP client consuming the REST API
b. API Layer (Google App Engine)
- Flask (index.py)
- Static assets & templates for risk chart
c. Serverless Orchestration (AWS Lambda)
- MonteCarloSimulation1 – warm-up & invocation
- create_ec2_instance, terminate_instances – EC2 management
- create_audit, read_audit – audit database operations
d. Compute Layer (EC2 Instances)
- Run monte_carlo.py for heavy Monte Carlo simulations
e. Data & Storage
- Audit log store (e.g. DynamoDB or GAE Datastore)
- Generated chart files in GAE static storage
f. External Services
- AWS API Gateway (or HTTPS) fronting Lambdas
- IAM roles networking

##### Relationships & Interactions
- Client → HTTP → GAE Flask API
- /warmup → Flask → AWS API Gateway → MonteCarloSimulation1 Lambda
- MonteCarloSimulation1 may ➔ create_ec2_instance Lambda ➔ EC2 spin-up
- EC2 runs monte_carlo.py ➔ returns results to Lambda ➔ create_audit Lambda logs results
- Flask /analyse and /get_* endpoints call relevant Lambda (or read DB) for data
- /get_chart_url returns the GAE static URL of generated risk_chart.html
- /terminate → Flask → terminate_instances Lambda ➔ EC2 scale-down

##### Architectural Patterns
- Microservices / serverless functions for orchestration
- Serverful EC2 for long-running compute
- Clear separation: API, orchestration, compute, storage

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer" 
        direction TB
        Client["HTTP Client / Browser"]:::client
    end

    %% GCP Layer
    subgraph "GCP (Google App Engine)" 
        direction TB
        API["Flask API\n(index.py)"]:::gcp
        Config["GAE Config\n(app.yaml)"]:::gcp
        Req["Dependencies\n(requirements.txt)"]:::gcp
        Static["Static Storage\n(generated_risk_chart.html\nstyles.css)"]:::gcp
        Templates["Templates\n(risk_chart.html)"]:::gcp
    end

    %% AWS Lambda Orchestration
    subgraph "AWS Lambda Orchestration" 
        direction TB
        MC1["MonteCarloSimulation1"]:::lambda
        CreateEC2["create_ec2_instance"]:::lambda
        TerminateEC2["terminate_instances"]:::lambda
        CreateAudit["create_audit"]:::lambda
        ReadAudit["read_audit"]:::lambda
    end

    %% AWS API Gateway
    APIGW["AWS API Gateway"]:::external

    %% EC2 Compute Layer
    subgraph "Compute Layer (EC2)" 
        direction TB
        EC2["EC2 Instance\nmonte_carlo.py"]:::ec2
    end

    %% Data & Storage
    subgraph "Data & Storage" 
        direction TB
        AuditDB["Audit Log Store (DynamoDB)"]:::storage
    end

    %% Connections
    Client -->|"HTTP Request\n(e.g. /warmup,\n/analyse,/terminate)"| API
    API -->|"Invoke via HTTPS"| APIGW
    APIGW -->|"Invoke MonteCarloSimulation1"| MC1
    MC1 -->|"StartInstance"| CreateEC2
    CreateEC2 -->|"AWS SDK: RunInstances"| EC2
    EC2 -->|"Return Simulation Results"| MC1
    MC1 -->|"Invoke create_audit"| CreateAudit
    CreateAudit -->|"Write Log"| AuditDB
    API -->|"GET /get_* endpoints"| ReadAudit
    ReadAudit -->|"Read Log"| AuditDB
    API -->|"GET /get_chart_url"| Static
    API -->|"POST /terminate"| APIGW
    APIGW -->|"Invoke terminate_instances"| TerminateEC2
    TerminateEC2 -->|"AWS SDK: TerminateInstances"| EC2

    %% Click Events
    click API "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/blob/main/index.py"
    click Config "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/blob/main/app.yaml"
    click Req "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/blob/main/requirements.txt"
    click Static "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/tree/main/static/"
    click Templates "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/tree/main/templates/"
    click EC2 "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/blob/main/Ec2 instance code/monte_carlo.py"
    click MC1 "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/blob/main/Lambda functions/MonteCarloSimulation1.py"
    click CreateEC2 "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/blob/main/Lambda functions/create_ec2_instance.py"
    click TerminateEC2 "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/blob/main/Lambda functions/terminate_instances.py"
    click CreateAudit "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/blob/main/Lambda functions/create_audit.py"
    click ReadAudit "https://github.com/maxnardone/scalable-cloud-api-for-trading-analysis/blob/main/Lambda functions/read_audit.py"

    %% Styles
    classDef client fill:#f8d7da,stroke:#d6a4a4
    classDef gcp fill:#cce5ff,stroke:#4b8bbe
    classDef lambda fill:#d4edda,stroke:#74b06f
    classDef ec2 fill:#ffe5cc,stroke:#e0914f
    classDef storage fill:#e2e3e5,stroke:#a0a0a0
    classDef external fill:#f0f0f0,stroke:#888888,stroke-dasharray: 5 5
```
