Agentic-Trading: Multi-Agent Simulator with A2A Protocol and ADK

7月17日 Published inStock Market Tools

Agentic-Trading is a simulated trading environment designed around the interaction of autonomous agents. Within this framework, AlphaBot generates potential trade signals while RiskGuard evaluates them for safety and compliance. These agents communicate via the Agent-to-Agent (A2A) protocol, ensuring a standardized exchange of information. A browser-based interface allows users to configure market scenarios, execute simulations, and monitor portfolio performance in real time.

Modular Agent Architecture. The system decomposes complex trading operations into discrete services. AlphaBot implements a simple moving average (SMA) crossover strategy to identify market entries and exits. RiskGuard acts as a gatekeeper, vetting proposed trades against pre-defined risk parameters. Both agents are built using Google’s Agent Development Kit (ADK). The Simulator UI—a FastAPI application—orchestrates the entire process, allowing users to define market conditions and strategy variables before running step-by-step simulations visualized through Plotly.

A2A Communication. Inter-agent messaging is handled by the open A2A specification. This protocol ensures that communication remains structured and interoperable. By following this standard, agents built on differing frameworks can discover one another's capabilities and exchange data securely without custom integration logic.

Configurable Parameters. Users can fine-tune market dynamics, including initial pricing, volatility levels, and directional trends. Strategy settings, such as SMA windows and trade sizing, are also fully adjustable, as are the underlying risk management rules.

Portfolio Tracking. The simulator maintains an accurate ledger of cash reserves, share holdings, and total equity, updating these values immediately after every transaction.

Visual Results. The system provides interactive charts that map price action alongside SMA lines, portfolio equity curves, and specific trade execution markers.

Flexible Deployment. The project includes scripts for both local development and Google Cloud Run deployment. Each service is containerized via its own Dockerfile, and Google Cloud Build is used to streamline the packaging process.

System Architecture

The architecture consists of three primary services communicating over HTTP using the A2A protocol.

Simulator UI (FastAPI)

  • Provides the web-based front end for inputting simulation parameters.
  • Orchestrates the market simulation loop. During each step, it transmits the current market data and portfolio status to AlphaBot through the A2A tasks/send endpoint.
  • Captures AlphaBot’s final trade decisions—whether approved, rejected, or updated—and adjusts the portfolio accordingly.
  • Renders the resulting data for user analysis.

AlphaBot (ADK / Python)

  • Processes A2A requests from the Simulator. It analyzes market trends by calculating SMAs to identify potential buy or sell signals.
  • Once a trade signal is detected, it contacts RiskGuard’s A2A tasks/send endpoint with a trade proposal and the current portfolio state.
  • Receives RiskGuard’s assessment and, if approved, updates its internal state to reflect the new position.
  • Relays the final decision, including the risk assessment details, back to the Simulator within the A2A response.

RiskGuard (ADK / Python)

  • Monitors for A2A requests originating from AlphaBot, which contain specific trade proposals and portfolio snapshots.
  • Validates the proposed trade against the risk rules defined in rules.py.
  • Returns a verdict—either approved or rejected—accompanied by a rationale, typically transmitted as an A2A artifact.

A2A Protocol in Action

This project utilizes the A2A protocol to bridge the gap between execution logic and risk oversight.

  • A2A Servers: Both AlphaBot and RiskGuard host HTTP endpoints compliant with the A2A specification.
  • A2A Clients: The Simulator UI functions as a client when assigning tasks to AlphaBot. Similarly, AlphaBot acts as a client when delegating subtasks to RiskGuard.
  • Task: The fundamental unit of work. The Simulator issues tasks containing market and portfolio data, while AlphaBot issues tasks regarding trade specifics.
  • Message / Part: Data such as market states, portfolio snapshots, and trade proposals are transmitted within A2A message parts, utilizing DataPart for structured JSON payloads.
  • Artifact: RiskGuard delivers its final assessment as an artifact in its response. AlphaBot can also return structured data to the Simulator using this method.
  • Agent Card: While local setups often use static discovery, a production deployment allows AlphaBot to dynamically fetch RiskGuard’s agent.json to identify its capabilities and connection endpoints. The ADK simplifies much of this protocol-level complexity.

Getting Started

Requirements: Python 3.11 or higher is recommended. The Google Cloud SDK is required for Cloud Run deployments.

Installation

  1. Clone the repository:
git clone https://github.com/kweinmeister/agentic-trading
cd agentic-trading
  1. Create a Python virtual environment and install the necessary dependencies:
python -m venv venv
source venv/bin/activate   # On Windows use: venv\Scripts\activate
pip install -r requirements.txt

Run Locally

To launch all components simultaneously, use the provided deployment script:

./deploy_local.sh

This script initializes RiskGuard (port 8080 by default), AlphaBot (port 8081), and the Simulator UI (port 8000). Once the services are active, navigate to http://127.0.0.1:8000 in your web browser.

To shut down the services, use the kill command provided in the script output or press Ctrl+C if the processes were started manually.

Run Tests

Install the development dependencies and execute the test suite via pytest:

pip install -r requirements-dev.txt
pytest tests/

Deployment

Cloud Run (Public Access)

The deploy_cloud_run.sh script automates the process of containerizing and deploying all three services to Google Cloud Run.

Before running:

  • Export your PROJECT_ID or edit the script to include your Google Cloud project ID.
  • Authenticate using gcloud auth login and select your project with gcloud config set project YOUR_PROJECT_ID.
  • Verify that you have the necessary permissions to enable the Cloud Run, Cloud Build, and Artifact Registry APIs.

Execute the script:

./deploy_cloud_run.sh

The script will establish an Artifact Registry repository if one does not exist. It then uses Cloud Build to create images for RiskGuard, AlphaBot, and the Simulator. Each service is deployed to Cloud Run with the required environment variables—enabling AlphaBot to locate RiskGuard and the Simulator to locate both agents. The final public URL for the Simulator UI will be displayed upon completion.

Security Note: Public deployment renders these services accessible over the open internet. For production environments, it is highly recommended to implement Google Identity-Aware Proxy (IAP) or a similar authentication layer.

Configuration

Defaults: SMA periods, risk thresholds, simulation parameters, and local service addresses are centrally managed in common/config.py.

Service URLs (Cloud Run): The deployment script automatically configures RISKGUARD_SERVICE_URL and ALPHABOT_SERVICE_URL as environment variables for the relevant services.

PORT Variable: The Dockerfiles and Cloud Run environment utilize the standard PORT environment variable to define the listening port for each service.