I Connected Claude AI to Freqtrade in 5 Lines of Code published: true.
Source: Dev.to
Source: Dev.to – I connected Claude AI to freqtrade in 5 lines of code
The problem with AI‑driven trading bots
You want to use Claude or GPT to analyze the market and trade automatically.
The usual approach looks like this:
- Call the AI API.
- Parse the response (and hope the format is consistent).
- Map the result to Binance/Bybit API calls.
- Handle authentication, signatures, and rate limits.
- Write all of this from scratch for every exchange.
You end up with hundreds of lines of glue code before writing a single line of actual trading logic.
There’s a better way.
PULSE Protocol
PULSE is an open‑source semantic‑messaging standard for AI‑to‑AI communication.
Think of it as a universal language – every agent (Claude, GPT, Freqtrade, Binance, etc.) can speak PULSE.
What a PULSE message looks like
from pulse.message import PulseMessage
msg = PulseMessage(
action="ACT.RECOMMEND.ACTION",
parameters={
"pair": "BTC/USDT",
"direction": "long",
"confidence": 0.92,
"reason": (
"RSI 0.80: "
"dataframe['enter_long'] = 1\n"
"# Fallback: your normal RSI logic\n"
"dataframe.loc[dataframe['rsi'] > 70, 'exit_long'] = 1"
),
"agent_id": "my-trading-bot",
},
)action– a namespaced identifier that tells the receiver what to do.parameters– a JSON‑serialisable dictionary that carries the payload.
Step 3 – Write the Claude agent
Run the snippet below on any machine (laptop, server, cloud instance, …).
It asks Claude to analyse a market snapshot, extracts the JSON answer, and forwards the recommendation to a local Freqtrade instance via PULSE.
import json
import requests
from pulse_anthropic import AnthropicAdapter
from pulse.message import PulseMessage
# ----------------------------------------------------------------------
# 1️⃣ Initialise the Anthropic (Claude) adapter
# ----------------------------------------------------------------------
adapter = AnthropicAdapter(api_key="sk-ant-...") # <-- replace with your key
# ----------------------------------------------------------------------
# 2️⃣ Ask Claude to analyse the market
# ----------------------------------------------------------------------
analysis = adapter.send(
PulseMessage(
action="ACT.ANALYZE.SENTIMENT",
parameters={
"text": """
BTC/USDT 1h chart:
- RSI: 23 (oversold)
- Price: $83,200, below 200‑SMA
- Last 3 candles: bullish divergence
- Volume: 40 % above average
Should I open a long position?
Answer with JSON:
{"direction": "long/short/neutral", "confidence": 0‑1, "reason": "..."}
"""
},
)
)
# ----------------------------------------------------------------------
# 3️⃣ Parse Claude's JSON response
# ----------------------------------------------------------------------
# The exact path to the JSON payload may differ depending on the adapter
# version – adjust if necessary.
result = json.loads(
analysis.content["parameters"]["result"]["content"]
)
# ----------------------------------------------------------------------
# 4️⃣ Forward the recommendation to Freqtrade via PULSE
# ----------------------------------------------------------------------
requests.post(
"http://localhost:9999/", # Freqtrade REST endpoint
json={
"action": "ACT.RECOMMEND.ACTION",
"parameters": {
"pair": "BTC/USDT",
"direction": result["direction"],
"confidence": result["confidence"],
"reason": result["reason"],
"agent_id": "claude-sonnet",
},
},
)
# ----------------------------------------------------------------------
# 5️⃣ Log what happened
# ----------------------------------------------------------------------
print(
f"Signal sent: {result['direction']} "
f"({result['confidence']:.0%} confidence)"
)
# → Signal sent: long (92% confidence)Tip:
If you receive a parsing error, inspectanalysis.contentto locate the exact field that holds Claude’s JSON string.
Step 4 – Start Freqtrade normally
freqtrade trade \
--config config.json \
--strategy ClaudeStrategy \
--enable-rest-apiNow the workflow is:
- Claude analyses the market and returns a JSON recommendation.
- The recommendation is wrapped in a PULSE message and posted to Freqtrade.
- Freqtrade receives the PULSE signal and executes the trade.
That’s all you need to connect Claude (or any other LLM) to your automated trading stack with PULSE. 🚀
Why not just call the Freqtrade REST API directly?
You could, but then you would have to:
- Write custom HTTP code for every integration
- Parse different response formats from different exchanges
- Rewrite everything if you switch from Claude to GPT (or vice‑versa)
- Handle authentication separately for each system
With PULSE, every component speaks the same language:
# Today: Claude drives Freqtrade on Binance
from pulse_anthropic import AnthropicAdapter
from pulse_freqtrade import FreqtradeAdapter, PulseMessage
from pulse_binance import BinanceAdapter
# … (same flow as above) …Enjoy a unified, plug‑and‑play architecture for AI‑driven crypto trading!
# Imports for tomorrow: GPT drives Freqtrade on Bybit
from pulse_openai import OpenAIAdapter
from pulse_bybit import BybitAdapterYour strategy code: unchanged
Bonus: Monitor Multiple Bots
Running a trend bot and a mean‑reversion bot simultaneously? Monitor both in one place:
from pulse_freqtrade import FreqtradeAdapter
from pulse.message import PulseMessage
bots = {
"trend-bot": FreqtradeAdapter(url="http://localhost:8080", ...),
"reversal-bot": FreqtradeAdapter(url="http://localhost:8081", ...),
}
for name, adapter in bots.items():
adapter.connect()
response = adapter.send(
PulseMessage(action="ACT.QUERY.STATUS", parameters={})
)
trades = response.content["parameters"]["result"]
profit = adapter.get_profit()
print(
f"{name}: {len(trades)} open trades | "
f"profit: {profit['profit_all_percent']:.2f}%"
)
# trend-bot: 3 open trades | profit: +4.21%
# reversal-bot: 1 open trade | profit: +1.87%Supported PULSE actions
| PULSE Action | What it does in Freqtrade |
|---|---|
ACT.QUERY.STATUS | Get open trades |
ACT.QUERY.BALANCE | Get portfolio balance |
ACT.QUERY.LIST | Get trade history |
ACT.QUERY.DATA | Get OHLCV candles for a pair |
ACT.QUERY.PROFIT | Get profit/loss summary |
ACT.TRANSACT.REQUEST | Force open a trade |
ACT.CANCEL | Force close a trade |
ACT.START | Start the bot |
ACT.STOP | Stop the bot |
ACT.RELOAD | Reload config |
Install
pip install pulse-freqtradeGitHub: [https://github.com/your-repo/pulse-freqtrade]
PyPI: [https://pypi.org/project/pulse-freqtrade]
License: Apache 2.0
The Bigger Picture
pulse-freqtrade is part of the PULSE Protocol ecosystem – 11 packages that let any AI system talk to any other AI system using a common semantic language:
pulse-protocol — core protocol
pulse-openai — GPT adapter
pulse-anthropic — Claude adapter
pulse-gemini — Gemini adapter
pulse-ollama — local models (Llama, Mistral)
pulse-binance — Binance exchange
pulse-bybit — Bybit exchange
pulse-kraken — Kraken exchange
pulse-okx — OKX exchange
pulse-gateway — secure API gateway
pulse-freqtrade — Freqtrade bot ← newAll packages share the same message format, so you can mix and match them freely.
Questions? Drop them in the comments—happy to help with setup or strategy ideas.
The code is open‑source, Apache 2.0, free forever.