Python SDK for the MangroveAI trading strategy platform
Project description
MangroveAI Python SDK
Python SDK for the MangroveAI trading strategy platform.
Install
pip install mangroveai
Setup
- Create an account at mangrovedeveloper.ai
- Navigate to Settings > API Keys
- Generate a new API key
- Set it as an environment variable:
export MANGROVE_API_KEY=prod_your_key_here
Quickstart
from mangroveai import MangroveAI
client = MangroveAI() # reads MANGROVE_API_KEY from environment
# List trading signals
signals = client.signals.list(limit=10)
for s in signals.items:
print(f"{s.name} ({s.category}, {s.signal_type})")
# Get market data
btc = client.crypto_assets.get_market_data("BTC")
print(f"BTC: ${btc.data['current_price']:,.2f}")
# Create a strategy
from mangroveai.models import CreateStrategyRequest
strategy = client.strategies.create(CreateStrategyRequest(
name="RSI Momentum",
asset="BTC",
entry=[{"name": "rsi_oversold", "signal_type": "TRIGGER",
"timeframe": "1d", "params": {"window": 14, "threshold": 30}}],
))
# Run a backtest
import json
from mangroveai.models import BacktestRequest
result = client.backtesting.run(BacktestRequest(
asset="BTC",
interval="1d",
strategy_json=json.dumps({"name": "test", "asset": "BTC",
"entry": [{"name": "rsi_oversold", "signal_type": "TRIGGER",
"timeframe": "1d", "params": {"window": 14, "threshold": 30}}],
"exit": []}),
lookback_months=3,
initial_balance=10000,
min_balance_threshold=0.1, min_trade_amount=25,
max_open_positions=3, max_trades_per_day=10,
max_risk_per_trade=0.02, max_units_per_trade=1000000,
max_trade_amount=10000000, volatility_window=24,
target_volatility=0.1,
# Optional: per-timeframe cooldown configuration (preferred over legacy flat fields).
# Keys are the primary timeframe; each value carries max_hold_time_hours,
# short_loss_limit, long_loss_limit, short_window_bars, and long_window_bars.
cooldown_config={
"1d": {
"max_hold_time_hours": 10,
"short_loss_limit": 4,
"long_loss_limit": 6,
"short_window_bars": 20,
"long_window_bars": 60,
}
},
))
print(f"Trades: {result.trade_count}, Sharpe: {result.metrics.get('sharpe_ratio')}")
Services
Layer 1: MangroveAI Core API
| Service | Access | Methods | Description |
|---|---|---|---|
client.auth |
auth.* |
5 | Login, refresh, API key management |
client.strategies |
strategies.* |
8 | Strategy CRUD, status, execution state |
client.backtesting |
backtesting.* |
7 | Sync/async/bulk backtesting |
client.signals |
signals.* |
7 | Signal discovery, evaluation, validation |
client.crypto_assets |
crypto_assets.* |
8 | Assets, exchanges, OHLCV, market data |
client.execution |
execution.* |
8 | Accounts, positions, trades, evaluation |
client.docs |
docs.* |
2 | Documentation listing and content |
Layer 2: Knowledge Base API
| Service | Access | Methods | Description |
|---|---|---|---|
client.kb.documents |
kb.documents.* |
3 | Document listing, content, sections |
client.kb.search |
kb.search.* |
1 | Full-text search with BM25 ranking |
client.kb.tags |
kb.tags.* |
2 | Tag listing and filtering |
client.kb.glossary |
kb.glossary.* |
3 | Glossary terms and backlinks |
client.kb.signals |
kb.signals.* |
2 | Signal metadata from KB |
client.kb.indicators |
kb.indicators.* |
2 | Indicator metadata from KB |
client.kb.compute |
kb.compute.* |
2 | x402 paid signal/indicator computation |
Layer 3: Coming Soon
On-chain analytics (client.on_chain), DeFi data (client.defi), and social signals (client.social) are defined but not yet available. Calling these methods raises NotImplementedLayerError.
Environment Detection
The SDK auto-detects the environment from your API key prefix:
| Prefix | Environment | API Base URL |
|---|---|---|
prod_ |
Production | https://api.mangrovedeveloper.ai/api/v1 |
dev_ |
Development | https://devapi.mangrove.trade/api/v1 |
Override with explicit parameters:
client = MangroveAI(api_key="...", base_url="http://localhost:5001/api/v1")
Error Handling
from mangroveai import MangroveAI, NotFoundError, RateLimitError, APIError
client = MangroveAI()
try:
strategy = client.strategies.get("nonexistent-id")
except NotFoundError as e:
print(f"Not found: {e.message} (correlation_id={e.correlation_id})")
except RateLimitError as e:
print(f"Rate limited, retry after {e.retry_after}s")
except APIError as e:
print(f"[{e.status_code}] {e.code}: {e.message}")
Pagination
Paginated endpoints return PaginatedResponse[T]:
# Single page
page = client.strategies.list(skip=0, limit=10)
print(f"Showing {len(page.items)} of {page.total}")
# Auto-paginate all items
for strategy in client.strategies.list_iter():
print(strategy.name)
Examples
See the examples/ directory for working scripts.
Development
git clone https://github.com/MangroveTechnologies/MangroveAI-SDK.git
cd MangroveAI-SDK
pip install -e ".[dev]"
pytest tests/ --ignore=tests/integration # unit tests
MANGROVE_API_KEY=... pytest tests/integration/ -m integration # live tests
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mangroveai-0.2.0.tar.gz.
File metadata
- Download URL: mangroveai-0.2.0.tar.gz
- Upload date:
- Size: 39.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6217c3323eea60033a741366eda99467fb770b0b9f6dc99c94a6d767382f9022
|
|
| MD5 |
fb15c67844aaa776b94e014cae6caed4
|
|
| BLAKE2b-256 |
5a0a8cb5a2f04356fccf5c5c0d5d6e5fbdc566e40c6ae62d06d8f79967ce55d9
|
File details
Details for the file mangroveai-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mangroveai-0.2.0-py3-none-any.whl
- Upload date:
- Size: 40.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a745f9d46b99ee6cfb7e3f9d632c2e6ffcfb2b5c30ad91e5ba6a35d28800b55d
|
|
| MD5 |
c7bfd095efce905c7aab98328bc468e2
|
|
| BLAKE2b-256 |
4db61bf64cd152f01074267cfb80d561d425a78397a4ebc303ac574c7a50d4dc
|