How rhettre/coinbase-advancedtrade-python works
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