BotTrade Python SDK
Backtest any Python trading agent on a versioned historical-market benchmark.
Quick start
python -m pip install 'bottrade==0.2.0'
export BOTTRADE_API_KEY="bt_your_key_here"
Create my_agent.py:
import bottrade
def decide(observation: bottrade.Observation):
symbol = observation.scenario.benchmark_symbol or observation.scenario.universe[0]
bars = observation.bars[symbol]
if observation.position(symbol):
return bottrade.hold("Position is open")
if len(bars) >= 2 and bars[-1].close > bars[-2].close:
return bottrade.buy(symbol, quantity=10, reasoning="Positive one-bar momentum")
return bottrade.hold("Waiting for momentum")
result = bottrade.backtest(
decide,
scenario="sandbox-nov-2024",
agent_info=bottrade.AgentInfo(
name="My momentum agent",
framework="python",
version="1.0",
),
)
print(result.run_id)
print(result.return_pct)
print(result.sharpe)
print(result.max_drawdown)
Run it:
python my_agent.py
backtest() calls the agent, submits its orders, advances the scenario, computes final metrics,
and returns a typed BacktestResult.
Get an API key at bot-trade.org/account.
Agent decisions
An agent receives one Observation and returns an order, a list of orders, or hold().
return bottrade.buy("AAPL", quantity=10, reasoning="Breakout")
return bottrade.sell("AAPL", quantity=5, reasoning="Reduce exposure")
return bottrade.short("TSLA", quantity=2, reasoning="Bearish signal")
return bottrade.cover("TSLA", quantity=2, reasoning="Close short")
return bottrade.hold("No signal")
Multiple orders:
return [
bottrade.buy("AAPL", quantity=10),
bottrade.buy("MSFT", quantity=5),
]
Each order owns its symbol, side, quantity, and reasoning.
Observation reference
observation.scenario # Scenario metadata and universe
observation.sim_time # Current simulated timestamp
observation.cash # Available cash
observation.positions # Current positions
observation.bars # Visible OHLCV bars by symbol
observation.step_number # Current runner step
observation.position("SPY")
Bars are typed objects:
latest = observation.bars["SPY"][-1]
print(latest.open, latest.high, latest.low, latest.close, latest.volume)
Stateful agents
import bottrade
class MovingAverageAgent:
def decide(self, observation: bottrade.Observation):
symbol = "SPY"
closes = [bar.close for bar in observation.bars[symbol]]
average = sum(closes) / len(closes)
if closes[-1] > average and observation.position(symbol) is None:
return bottrade.buy(symbol, quantity=10)
return bottrade.hold()
result = bottrade.backtest(
MovingAverageAgent(),
scenario="sandbox-nov-2024",
lookback=20,
)
Async agents
import asyncio
import bottrade
async def decide(observation: bottrade.Observation):
signal = await get_model_signal(observation)
if signal == "buy":
return bottrade.buy("SPY", quantity=10)
return bottrade.hold()
async def main():
result = await bottrade.backtest_async(decide, scenario="sandbox-nov-2024")
print(result.return_pct)
asyncio.run(main())
Agent provenance
Attach reproducible identity to every run:
info = bottrade.AgentInfo(
name="AI Hedge Fund technical",
framework="ai-hedge-fund",
model="gpt-4.1",
version="2026.7.10",
source_url="https://github.com/virattt/ai-hedge-fund",
source_revision="09dd33167bd6b4ea63ae32e7246e70e80632cc81",
config={"analysts": ["technical_analyst"], "lookback": 180},
)
result = bottrade.backtest(agent, scenario="tech-2024-q2", agent_info=info)
Published run pages display this identity with the benchmark evidence.
Runner options
result = bottrade.backtest(
agent,
scenario="tech-2024-q2",
lookback=50,
decide_every=1,
max_steps=10_000,
resume_run_id=None,
publish=False,
)
| Option | Meaning |
|---|---|
scenario |
Ready scenario slug |
lookback |
Visible bars per symbol at each decision |
decide_every |
Call the agent every N bars |
max_steps |
Maximum simulator steps for this invocation |
resume_run_id |
Continue an existing active run |
publish |
Publish the completed run and trades |
Command line
Export a function or agent object from a module:
bottrade backtest my_agent:decide --scenario sandbox-nov-2024
python -m bottrade backtest my_agent:decide --scenario sandbox-nov-2024
Add provenance:
bottrade backtest my_agent:decide \
--scenario tech-2024-q2 \
--name "My momentum agent" \
--framework python \
--agent-version 1.0 \
--source-revision abc123
Run bottrade backtest --help for the complete command reference.
Explicit reference strategy
import bottrade
from bottrade.strategies import buy_and_hold
result = bottrade.backtest(
buy_and_hold(quantity=10, symbol="SPY"),
scenario="sandbox-nov-2024",
)
Here, quantity configures the selected buy-and-hold agent.
Integrations
| Integration | Example |
|---|---|
| Plain Python | Custom momentum agent |
| OpenAI Agents SDK | Streamable HTTP MCP agent |
| LangChain / LangGraph | MultiServerMCPClient agent |
| OpenAI, Gemini, Grok | Multi-provider agent |
| AI Hedge Fund | AI Hedge Fund adapter |
Result object
result.run_id
result.agent_info
result.scenario
result.return_pct
result.final_equity
result.sharpe
result.sortino
result.max_drawdown
result.trade_count
result.published
Publish a result with publish=True, then embed its evidence badge:
[](https://bot-trade.org/run/RUN_ID)
Low-level client
Use session() for explicit observation, submission, and stepping:
import bottrade
info = bottrade.AgentInfo(name="My manual agent", framework="python")
with bottrade.session("sandbox-nov-2024", agent_info=info) as run:
while run.active:
observation = run.observe()
run.submit(decide(observation))
run.step()
results = run.results()
BotTradeClient exposes scenario discovery, run creation, observations, orders, stepping, results,
publication, public runs, URLs, and badges. AsyncBotTradeClient provides the same operations with
async methods.
import bottrade
with bottrade.BotTradeClient.from_env() as client:
scenarios = client.list_scenarios()
print([scenario.slug for scenario in scenarios])
Development
python -m pip install -e '.[dev]'
ruff check .
mypy
pytest
python -m build
twine check dist/*
BotTrade is designed for software evaluation, education, and research.
Metadata
Release files for bottrade 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bottrade-0.2.0.tar.gz | 85.6 kB | With provenance | Details for bottrade-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bottrade-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for bottrade-0.2.0-py3-none-any.whl |
Total release size: 105.2 kB
Release files / bottrade-0.2.0.tar.gz
| Download URL | bottrade-0.2.0.tar.gz |
|---|---|
| Size | 85.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a0c52bfffbde10ed40703796a676948c5e6de2c39043950270162f15d71e341e
|
|
BLAKE2b-256 checksum How to use checksums |
ac0f8240f6cfd4102b08f8f00c87a80cc3b034ffb71fa443a786f11122330470
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 13, 2026.
Transparency logRelease files / bottrade-0.2.0-py3-none-any.whl
| Download URL | bottrade-0.2.0-py3-none-any.whl |
|---|---|
| Size | 19.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c2fd1a3c056489c242a54b0000dee9e6ec6ee478dc8521cf9f3400b80388c422
|
|
BLAKE2b-256 checksum How to use checksums |
16f24925d512f7d445bde9dd2ce528ff5b6925afa0b2882d6148ba4ef5223b3e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 13, 2026.
Transparency logSecurity for bottrade 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for bottrade-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 13, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.