This tutorial walks through building an autonomous crypto trading system using the Swarms framework and the Gemini exchange API.
What is Gemini Agentic Trading?
Gemini’s Agentic Trading is the first agentic trading capability offered by a regulated US exchange. It provides modular “Trading Skills” — pre-built functions that AI agents can call to:
- Query real-time market data — prices, order book depth, bid-ask spreads
- Access historical data — OHLCV candles for backtesting and trend analysis
- Execute trades autonomously — place, modify, and cancel orders
- Monitor positions — track balances, open orders, and P&L
Gemini exposes these capabilities via the Model Context Protocol (MCP), an open standard that lets AI agents interact with external tools. In this tutorial, we build equivalent tools as Python functions that Swarms agents call directly via function calling.
What We Build
We cover two patterns:
- Single agent — monitors price conditions and executes trades
- Multi-agent swarm — signal generation, risk management, and execution as separate agents in a sequential pipeline
Trading involves real financial risk. Always start with Gemini’s sandbox environment before using real funds. All examples default to sandbox mode with dry-run enabled.
Install
Environment Setup
Create a .env file or export these in your shell:
For production trading, create API keys at exchange.gemini.com/settings/api with Trading permissions enabled.
Agents interact with Gemini through tool functions. Each tool must have type hints and a docstring — Swarms automatically converts them into the OpenAI function-calling schema that the LLM uses to decide when to call them.
Authentication Helper
All private Gemini endpoints use HMAC-SHA384 signature authentication. The JSON payload is base64-encoded and sent as a header (not as a POST body).
These are public endpoints — no authentication required.
These require authentication via the helper above.
Part 2: Single Agent
A single agent that checks market conditions and places trades when it finds setups.
Part 3: Multi-Agent Swarm
For more disciplined trading, split responsibilities across three agents in a sequential pipeline. Each agent focuses on one job and passes its output to the next.
The pipeline flows: Signal (market data → signals) → Risk (signals → approved orders) → Execution (orders → confirmations).
Guardrails & Best Practices
Sandbox vs Production
Dry-Run Mode
DRY_RUN = True prevents all real orders. The agent sees realistic responses but nothing is actually submitted to the exchange. Always start here.
Hard-Coded Safety Limits
Even with agent-level risk management, add hard limits in your order tool:
Trade Logging
Log every decision for audit:
Gemini Rate Limits
- Public endpoints: 120 requests/minute
- Private endpoints: 600 requests/minute
If running agents in a loop, add a loop_interval to the Agent constructor to avoid hitting limits.