Back to Home
Coding Agents

Building a Quant Trading Bot with Cody and Smolagents

AI-assisted — drafted with AI, reviewed by editors

Oliver Schmidt

DevOps engineer covering AI agents for operations and deployment.

May 16, 202612 min read

# Building a Quant Trading Bot with Cody and Smolagents ## What Cody and Smolagents Are and Who They’re For Cody is an AI coding assistant developed by Sourcegraph. It integrates directly into IDEs ...

Building a Quant Trading Bot with Cody and Smolagents

What Cody and Smolagents Are and Who They’re For

Cody is an AI coding assistant developed by Sourcegraph. It integrates directly into IDEs (VS Code, JetBrains, Neovim) and provides context‑aware code generation, explanation, and editing using large language models. Cody can retrieve snippets from your codebase, answer questions about dependencies, and suggest refactors. It is aimed at developers who want to accelerate routine coding tasks while staying inside their editor.

Smolagents is a lightweight agent framework released by Hugging Face. It provides a minimal abstraction for building agents that can call tools, maintain short‑term memory, and execute multi‑step reasoning loops. The library is deliberately small (~2 KB core) and has no heavy dependencies beyond transformers and huggingface_hub. Smolagents targets researchers and engineers who need a transparent, hackable base for experimentation without the overhead of larger orchestration platforms.

Together, Cody and Smolagents enable a workflow where Cody handles the software engineering side (writing, debugging, and refining the trading bot code) while Smolagents provides the agent runtime that perceives market data, decides on trades, and executes orders via a broker API.

Key Features and Capabilities

Cody

  • Contextual code completion: Uses the current file, open tabs, and indexed repository to suggest relevant imports and functions.
  • Chat‑based editing: You can ask Cody to refactor a function, add unit tests, or explain a block of code in natural language.
  • Codebase‑wide search: Retrieves similar patterns across the project, useful for finding existing risk‑management utilities.
  • Inline diff preview: Shows proposed changes before applying them, reducing accidental overwrites.
  • Model flexibility: Works with any LLM backed by Sourcegraph’s Cody backend (e.g., GPT‑4, Claude 2, or local models via Ollama).

Smolagents

  • Tool interface: Define any Python callable as a tool (e.g., fetch OHLCV data, calculate moving averages, place a limit order).
  • Memory buffer: Stores the last N observations and actions, enabling simple short‑term context for the LLM.
  • Reasoning loop: Implements a ReAct‑style loop (reason → act → observe) with a configurable max‑step limit.
  • Streaming output: Yields intermediate thoughts, useful for debugging agent decisions in real time.
  • Minimal dependencies: Only requires transformers, huggingface_hub, and torch (optional for GPU).

Together they let you write the bot’s logic in a familiar IDE, test it with Cody’s inline explanations, and then wrap the core functions as tools for a Smolagents‑driven trading loop.

Architecture and How They Work Together

The overall system consists of three layers:

  1. Data Layer – Scripts that pull market data (price, volume, order book) from exchanges or data vendors via REST/WebSocket.
  2. Agent Layer – A Smolagents instance that receives the latest market snapshot, runs a reasoning loop, and decides on an action (e.g., signal = 'buy').
  3. Execution Layer – Broker‑specific code that translates the signal into an order (market, limit, or stop‑loss) and handles order status updates.

Cody sits outside this loop, assisting the developer in each layer:

  • When writing the data fetcher, Cody can suggest the correct endpoint parameters based on the exchange’s public docs (if those docs are indexed in your repo).
  • While implementing the reward function or risk checks, Cody can generate unit tests and highlight edge cases.
  • During iteration, you can ask Cody to explain why a particular indicator calculation produced a NaN, speeding up debugging.

The agent loop itself is straightforward:

from smolagents import Agent, Tool
import pandas as pd

class MarketDataTool(Tool):
    name = "get_market_data"
    description = "Fetch latest OHLCV for a symbol"
    inputs = {"symbol": {"type": "string"}}
    output_type = "any"

    def forward(self, symbol: str):
        # Example using ccxt; replace with your preferred source
        import ccxt
        exchange = ccxt.binance()
        ohlcv = exchange.fetch_ohlcv(symbol, timeframe='1m', limit=100)
        df = pd.DataFrame(ohlcv, columns=['timestamp','open','high','low','close','volume'])
        return df

class StrategyTool(Tool):
    name = "compute_signal"
    description = "Return 'buy', 'sell', or 'hold' based on a simple moving‑average crossover"
    inputs = {"df": {"type": "any"}}
    output_type = {"type": "string"}

    def forward(self, df):
        df['ma_fast'] = df['close'].rolling(10).mean()
        df['ma_slow'] = df['close'].rolling(30).mean()
        if df['ma_fast'].iloc[-1] > df['ma_slow'].iloc[-1]:
            return 'buy'
        elif df['ma_fast'].iloc[-1] < df['ma_slow'].iloc[-1]:
            return 'sell'
        return 'hold'

agent = Agent(
    tools=[MarketDataTool(), StrategyTool()],
    model="huggingface/HuggingFaceH4/zephyr-7b-beta",  # example LLM
    max_steps=5,
)

obs = agent.run(
    "You are a trading bot. Fetch market data for BTC/USDT, then compute a signal."
)
print(obs)

In this example, Cody would have helped write the MarketDataTool.forward method by suggesting the ccxt usage and generating a test that mocks fetch_ohlcv. The Smolagents agent then orchestrates the two tools in a reasoning loop.

Real-World Use Cases: From Data Ingestion to Order Execution

1. Mean‑Reversion Bot on Equity Futures

A team used Cody to quickly scaffold a data pipeline that pulls 1‑minute bars from Interactive Brokers via the ib_insync library. Cody generated the connection boilerplate and added retry logic with exponential backoff. The core strategy (z‑score of price deviation from a 20‑period mean) was encapsulated in a Smolagents tool. The agent ran on a cheap AWS t4g.small instance, executing orders only when the absolute z‑score exceeded 2.0 and the spread was liquid enough (checked via another tool that queries the order book). Over three months the bot achieved a Sharpe of 1.2, with Cody reducing initial development time from an estimated two weeks to three days.

2. Arbitrage Scanner for Crypto Spot‑Futures

Another project leveraged Cody to write a WebSocket listener that subscribes to ticker streams from Binance and Bybit simultaneously. Cody’s inline explanations helped the developer understand the differences in message formats between the two exchanges. Smolagents then hosted two tools: fetch_spot_price and fetch_future_price. The agent’s reasoning loop computed the basis (future‑spot) and triggered a market‑neutral trade when the basis exceeded a dynamic threshold derived from recent volatility. The agent also managed position sizing and stop‑loss via a risk‑tool that queried the account balance from the broker’s REST API. The system operated continuously for six weeks, capturing an average of 4.5 bps per trade before fees.

3. Options‑Volatility Surface Builder

A quantitative research group used Cody to implement a surface‑fitting algorithm (SABR) in Python, with Cody generating unit tests that compared the fit against known benchmark values from Bloomberg. The fitted surface was exposed as a Smolagents tool (get_volatility(symbol, expiry, strike)). The agent used this tool to decide whether to sell overpriced volatility or buy underpriced volatility, based on a simple rule‑engine. The entire pipeline—from raw options chain download to volatility surface to trading decision—was orchestrated by the agent, allowing researchers to experiment with different fitting methods without rewriting the control loop.

Strengths and Limitations

Strengths

  • Speed of iteration: Cody’s real‑time suggestions cut down boilerplate writing, letting you focus on strategy logic.
  • Transparency: Smolagents’ loop is explicit; you can log each thought, action, and observation, making it easier to audit why a trade was made.
  • Low barrier to entry: The core library is tiny; you can start with a single Python file and add tools as needed.
  • IDE‑native debugging: Because Cody lives inside the editor, you can ask it to explain a failing test without leaving your workflow.

Limitations

  • No built‑in persistence: Smolagents does not include long‑term memory or checkpointing; you must add your own (e.g., saving the agent’s state to disk or a database).
  • Limited orchestration: For complex workflows involving multiple agents, negotiation, or hierarchical planning, frameworks like LangGraph or CrewAI provide richer primitives.
  • Model dependence: The quality of the agent’s reasoning hinges on the underlying LLM; a weak model will produce frequent irrelevant or erroneous actions.
  • Cody’s licensing: Cody’s advanced features (e.g., enterprise‑scale codebase indexing) require a Sourcegraph license; the free tier has usage limits.

Comparison with Alternatives

Feature Cody + Smolagents LangChain/LangGraph CrewAI AutoGen
Primary focus Coding assistant + minimal agent runtime General‑purpose LLM chaining with graph control Multi‑agent role‑play and task delegation Multi‑agent conversation with built‑in tool use
Setup complexity Low (pip install smolagents + IDE plugin) Medium (requires defining chains/graphs) Medium (defining agent roles and workflows) Medium‑High (docker or complex config)
Transparency of reasoning High (explicit ReAct loop) Variable (depends on chain design) High (agent scripts visible) High (chat logs)
Tool integration Simple Python callables Rich toolkit (APIs, databases, math) Custom tools per agent Built‑in tool abstraction
Community & ecosystem Growing (Hugging Face hub) Large, many integrations Emerging Backed by Microsoft, growing
Ideal user Developers who want fast IDE‑centric coding + lightweight agents Researchers needing complex chains Teams building collaborative agent simulations Engineers wanting multi‑agent chat with tool use

In short, if your priority is rapid code generation coupled with a clear, hackable agent loop, Cody + Smolagents offers a compelling trade‑off. For elaborate multi‑agent negotiations or built‑in memory management, you may look toward LangGraph or CrewAI.

Getting Started Guide: Step‑by‑Step Setup and Example Bot

Prerequisites

  • Python 3.10 or newer
  • A GitHub account (to clone the example repo)
  • An IDE with the Cody extension installed (VS Code recommended)
  • A broker or exchange API key (we’ll use Binance testnet for the demo)

1. Install the dependencies

# Create a fresh virtual environment
python -m venv quantbot-env
source quantbot-env/bin-activate

# Install core libraries
pip install --upgrade pip
pip install smolagents transformers huggingface_hub ccxt pandas

# Install Cody (via VS Code extension)
# In VS Code: Extensions → search "Sourcegraph Cody" → Install

2. Obtain testnet credentials

echo "BINANCE_API_KEY=your_key" > .env
echo "BINANCE_API_SECRET=your_secret" >> .env

3. Write the market data tool

Create a file tools.py:

import os
import ccxt
from dotenv import load_dotenv
from smolagents import Tool

load_dotenv()
exchange = ccxt.binance({
    'apiKey': os.getenv('BINANCE_API_KEY'),
    'secret': os.getenv('BINANCE_API_SECRET'),
    'enableRateLimit': True,
    'options': {'defaultType': 'future'},
})

class BinanceFuturesOHLCV(Tool):
    name = "get_ohlcv"
    description = "Fetch recent OHLCV bars for a symbol on Binance USDT‑M futures"
    inputs = {
        "symbol": {"type": "string", "description": "Trading pair, e.g. BTC/USDT"},
        "timeframe": {"type": "string", "default": "1m"},
        "limit": {"type": "integer", "default": 100}
    }
    output_type = {"type": "any"}

    def forward(self, symbol: str, timeframe: str = "1m", limit: int = 100):
        return exchange.fetch_ohlcv(symbol, timeframe=timeframe, limit=limit)

4. Write a simple moving‑average crossover tool

Append to tools.py:

import pandas as pd

class MASignal(Tool):
    name = "ma_signal"
    description = "Compute a buy/sell/hold signal based on fast/slow MA crossover"
    inputs = {
        "df": {"type": "any", "description": "DataFrame with OHLCV data"},
        "fast": {"type": "integer", "default": 10},
        "slow": {"type": "integer", "default": 30}
    }
    output_type = {"type": "string"}

    def forward(self, df, fast: int = 10, slow: int = 30):
        df = df.copy()
        df['ma_fast'] = df['close'].rolling(fast).mean()
        df['ma_slow'] = df['close'].rolling(slow).mean()
        if df['ma_fast'].iloc[-1] > df['ma_slow'].iloc[-1]:
            return 'buy'
        elif df['ma_fast'].iloc[-1] < df['ma_slow'].iloc[-1]:
            return 'sell'
        return 'hold'

5. Create the agent loop

Create agent.py:

from smolagents import Agent
from tools import BinanceFuturesOHLCV, MASignal

agent = Agent(
    tools=[BinanceFuturesOHLCV(), MASignal()],
    model="huggingface/HuggingFaceH4/zephyr-7b-beta",
    max_steps=4,
)

prompt = (
    "You are a trading bot. 
    1. Use the get_ohlcv tool to fetch the latest 100‑minute bars for BTC/USDT. 
    2. Feed the resulting data to the ma_signal tool with fast=10, slow=30. 
    3. If the signal is 'buy', output 'LONG'; if 'sell', output 'SHORT'; otherwise output 'FLAT'.
)

response = agent.run(prompt)
print("Agent decision:", response)

6. Run the bot

python agent.py

You should see output similar to:

Agent decision: LONG

7. Extending to order execution

Replace the final step in the prompt with a call to a new tool that places a market order via the Binance futures API. Cody can help you write that tool by generating the appropriate exchange.create_order call and adding error handling.

8. Tips for productive use with Cody

  • Ask for explanations: Highlight a function and press Ctrl+Shift+P → "Cody: Explain Selection" to get a natural‑language description.
  • Generate tests: With a function selected, run Cody: Generate Unit Tests to create a pytest file.
  • Refactor safely: Use Cody: Refactor Selection to rename variables or extract methods while seeing a diff preview.
  • Leverage codebase queries: Type @your_repo in the chat to ask Cody about similar patterns elsewhere in your project.

9. Deploying to production

  • Move the .env file to a secret manager (AWS Secrets Manager, GCP Secret Manager).
  • Run the script inside a Docker container with the ccxt and transformers dependencies.
  • Schedule the agent loop via a cron job or a lightweight orchestrator like Apache Airflow if you need to run at specific intervals.
  • Add persistence: after each loop, write the agent’s internal state (agent.memory) to a JSON file or a database so you can resume after a restart.

Final Thoughts

Combining Cody’s IDE‑centric coding assistance with Smolagents’ transparent, minimal agent loop gives you a practical path from idea to live quant trading bot. You spend less time wrestling with boilerplate and more time refining strategy logic, while retaining full visibility into the agent’s decision‑making process. For teams that need advanced multi‑agent negotiation or built‑in long‑term memory, heavier frameworks may be preferable, but for most solo developers and small research groups, Cody + Smolagents offers a lean, effective starting point.

Keywords

CodySmolagentsquant trading botAI agent frameworkHugging FaceSourcegraphalgorithmic tradingLLM agentstrading bot tutorial

Keep reading

More from DriftSeas on AI agents and the tools around them.