# rhettre/coinbase-advancedtrade-python architecture

> Architecture diagram of the GitHub repository rhettre/coinbase-advancedtrade-python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rhettre/coinbase-advancedtrade-python
- Repository: https://github.com/rhettre/coinbase-advancedtrade-python

## Overview

##### Project Type and Purpose
- This is a Python client library (wrapper) for the Coinbase Advanced Trade API with built-in trading strategies.
- It is not a full-stack application; rather, it’s an SDK extension that sits between a user’s code and two external HTTP APIs (Coinbase and AlphaSquared), plus one data source (Fear & Greed Index).

##### File Structure
- Top-Level Package: `coinbase_advanced_trader/`
– `enhanced_rest_client.py`: Core HTTP client subclass that talks to Coinbase.
– `alphasquared_trader.py`: Orchestrator combining Coinbase client + AlphaSquared client.
– `services/`: Business-logic modules (account, price, order, funds, trading strategies).
– `models/`: Domain objects (Order, Product).
– `trading_config.py` & `config.py`: Configuration management (API keys, default strategy settings).
– `utils/helpers.py` & `logger.py`: Common utilities and logging.
- Tests mirror each module under `tests/`.
- Build/config: `setup.py`, `requirements.txt`, `.github/workflows` (CI/CD for publishing).

##### README for Technical Details
- EnhancedRESTClient extends official Coinbase SDK’s `RESTClient`.
- External Services:
– Coinbase Advanced Trade HTTP API
– Fear-and-Greed-Crypto package (for market sentiment)
– AlphaSquared API (for risk analysis strategies)
- Caching is used in account-balance services to minimize repeated API calls.
- AWS Lambda compatibility note (Python 3.12, cryptography layer).

##### Main Components
a. Core Library Components
– Configuration Manager (`config.py`, `trading_config.py`)
– Logger (centralized logging utility)
– EnhancedRESTClient (inherits from Coinbase SDK)
– AlphaSquaredTrader (combines two clients)
– Services Layer:
- AccountService
- PriceService
- OrderService
- FundsService
- TradingStrategyService & FearAndGreedStrategy
– Models Layer: Order, Product, etc.
– Utils Layer: helpers, error handling
b. External Components
– Official Coinbase Advanced Trade API
– AlphaSquared Risk Analysis API
– Fear-and-Greed-Crypto data source
– AWS Lambda environment (optional deployment target)

##### Relationships & Data Flows
– The user’s code → instantiates EnhancedRESTClient and/or AlphaSquaredTrader
– EnhancedRESTClient ↔ Coinbase Advanced Trade API (HTTP requests/responses)
– Services call methods on EnhancedRESTClient, process results into Models
– AlphaSquaredTrader:
- Pulls data from EnhancedRESTClient (e.g., balances)
- Pulls risk analysis from AlphaSquared API
- Calls trading services (via EnhancedRESTClient) to execute orders
– FearAndGreedStrategy: obtains index from fear-and-greed-crypto → influences order volume via TradingStrategyService

##### Architectural Patterns & Principles
– Layered Architecture:
- Presentation/API (public client methods)
- Service / Business Logic Layer
- Infrastructure Layer (REST client)
- Models Layer
– Dependency Injection: client objects passed into services/trader
– Caching Decorator or TTL caching in account-related services
– Single Responsibility: each service handles one domain concept

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration & Utilities
    subgraph "Configuration & Utilities"
        Config["Configuration Manager"]:::config_utils
        TradeConfig["Trading Configuration"]:::config_utils
        Logger["Logger"]:::config_utils
        Helpers["Utilities / Helpers"]:::config_utils
        Constants["Constants"]:::config_utils
    end

    %% Models Layer
    subgraph "Models Layer"
        OrderModel["Order Model"]:::model
        ProductModel["Product Model"]:::model
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        ERC["EnhancedRESTClient"]:::core
    end

    %% Services Layer
    subgraph "Services Layer"
        AccountSvc["AccountService\n(cache TTL=1hr)"]:::core
        PriceSvc["PriceService"]:::core
        OrderSvc["OrderService"]:::core
        FundsSvc["FundsService"]:::core
        TradingStratSvc["TradingStrategyService"]:::core
        FearGreed["FearAndGreedStrategy"]:::core
    end

    %% Core Trader
    AlphaTrader["AlphaSquaredTrader"]:::core

    %% External APIs and Deployment
    subgraph "External & Deployment"
        CoinbaseAPI["Coinbase Advanced Trade API"]:::external
        AlphaAPI["AlphaSquared Risk Analysis API"]:::external
        FearGreedSrc["Fear & Greed Index"]:::external
        AWSLambda["AWS Lambda (Deployment)"]:::external
    end

    %% Entry Point
    User["User Code"]:::external

    %% Data Flows
    User -->|instantiates| ERC
    User -->|instantiates| AlphaTrader

    AccountSvc -->|"get_account() → AccountModel"| ERC
    PriceSvc -->|"get_price() → ProductModel"| ERC
    OrderSvc -->|"create_order() → OrderModel"| ERC
    FundsSvc -->|"get_funds() → FundsData"| ERC
    TradingStratSvc -->|"execute_strategy() → HTTP POST /orders"| ERC

    ERC -->|"HTTP requests/responses"| CoinbaseAPI

    FearGreed -->|"fetch_index() → index_data"| FearGreedSrc
    FearGreed -->|"provides sentiment"| TradingStratSvc

    AlphaTrader -->|"pull balances"| AccountSvc
    AlphaTrader -->|"pull risk analysis"| AlphaAPI
    AlphaTrader -->|"execute orders"| TradingStratSvc

    %% Deployment arrow
    AWSLambda -->|"deploys library"| ERC

    %% Click Events
    click Config "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/config.py"
    click TradeConfig "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/trading_config.py"
    click Logger "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/logger.py"
    click Helpers "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/utils/helpers.py"
    click Constants "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/constants.py"
    click OrderModel "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/models/order.py"
    click ProductModel "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/models/product.py"
    click ERC "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/enhanced_rest_client.py"
    click AlphaTrader "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/alphasquared_trader.py"
    click AccountSvc "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/services/account_service.py"
    click PriceSvc "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/services/price_service.py"
    click OrderSvc "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/services/order_service.py"
    click FundsSvc "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/services/funds_service.py"
    click TradingStratSvc "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/services/trading_strategy_service.py"
    click FearGreed "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/coinbase_advanced_trader/services/fear_and_greed_strategy.py"
    click AWSLambda "https://github.com/rhettre/coinbase-advancedtrade-python/blob/main/.github/workflows/publish-and-release.yml"

    %% Styles
    classDef config_utils fill:#cccccc,stroke:#333,stroke-width:1px
    classDef core fill:#add8e6,stroke:#333,stroke-width:1px
    classDef model fill:#90ee90,stroke:#333,stroke-width:1px
    classDef external fill:#ffa500,stroke:#333,stroke-width:1px
```
