# Put Butterfly Delta inside a trading loop

Butterfly Delta belongs after an order is constructed and immediately before the venue submission call. The preflight is paid per order because venue status, visible depth, spread, quote freshness, and estimated slippage can change between successive orders.

## CCXT — Node.js

Install the fail-closed wrapper and its AgentCash payment client directly from Butterfly Delta:

```bash
npm install ccxt https://butterflydelta.com/downloads/butterflydelta-preflight-0.1.0.tgz
```

Replace `exchange.createOrder(...)` with:

```js
import { createOrderWithDelta } from "@butterflydelta/preflight";

const { gate, order } = await createOrderWithDelta(
  exchange,
  "BTC/USD",
  "market",
  "buy",
  0.25,
  undefined,
  {},
  {
    maxEstimatedSlippageBps: 25,
    minDepthCoveragePct: 100,
    maxVenueSpreadBps: 10,
    maxCrossVenueDivergenceBps: 20,
    maxQuoteAgeSeconds: 10,
    minHealthyVenues: 2,
  },
);
```

The wrapper pays at most $0.01 USDC on Base, requires a fresh signed `PASS`, and only then calls CCXT. `BLOCK`, `UNKNOWN`, a malformed result, a timeout, or an expired attestation prevents submission. The supported CCXT exchange IDs are `coinbase`, `coinbaseexchange`, and `gemini`.

The caller remains responsible for authorization, balances, sizing, routing, exchange credentials, and every other risk control. Butterfly Delta never receives exchange keys or wallet credentials and never submits an order itself.

## CCXT — Python

Install AgentCash once, then install the fail-closed Python gate directly from Butterfly Delta:

```bash
npm install -g agentcash@latest
pip install ccxt https://butterflydelta.com/downloads/butterflydelta_preflight-0.1.0-py3-none-any.whl
```

Replace `exchange.create_order(...)` with:

```python
from ccxt_preflight import create_order_with_delta

result = create_order_with_delta(
    exchange,
    "BTC/USD",
    "market",
    "buy",
    0.25,
    policy={
        "maxEstimatedSlippageBps": 25,
        "minDepthCoveragePct": 100,
        "maxVenueSpreadBps": 10,
        "maxCrossVenueDivergenceBps": 20,
        "maxQuoteAgeSeconds": 10,
        "minHealthyVenues": 2,
    },
)
```

The Python gate has the same fail-closed contract and never reads or receives exchange credentials. Exact order fields override policy keys, so a policy object cannot silently change the order that Butterfly Delta checks.

## Freqtrade — final entry callback

Freqtrade documents `confirm_trade_entry()` as its last callback before placing an entry order. Install the Python package above, then put the mixin first in the strategy inheritance order:

```bash
pip install https://butterflydelta.com/downloads/butterflydelta_preflight-0.1.0-py3-none-any.whl
```

```python
from freqtrade.strategy import IStrategy
from freqtrade_preflight import ButterflyDeltaEntryMixin

class MyStrategy(ButterflyDeltaEntryMixin, IStrategy):
    # Keep the rest of the strategy unchanged.
    pass
```

The mixin gates live and dry-run entry orders, skips backtest/hyperopt simulations, and returns `False` on `BLOCK`, `UNKNOWN`, timeout, malformed output, or expiry. It intentionally does not implement `confirm_trade_exit()`, so it cannot block a stop-loss or other protective exit. Freqtrade warns that timing is critical in this callback; use this synchronous integration only when fail-closed entry integrity is more important than minimizing submit latency.

## Hummingbot — Controller entries

Hummingbot's current Strategy V2 controller exposes `buy()` and `sell()` immediately before it creates an executor. Install the package above and put the mixin before your controller base class:

```bash
pip install https://butterflydelta.com/downloads/butterflydelta_preflight-0.1.0-py3-none-any.whl
```

```python
from hummingbot.strategy_v2.controllers.controller_base import ControllerBase
from hummingbot_preflight import ButterflyDeltaControllerMixin

class MyController(ButterflyDeltaControllerMixin, ControllerBase):
    # Keep the rest of the controller unchanged.
    pass
```

The mixin pays for and requires a fresh signed `PASS` before `keep_position=True` orders on Hummingbot's `coinbase_advanced_trade` or `gemini` connectors. It raises before the executor is created on `BLOCK`, `UNKNOWN`, timeout, malformed output, or expiry. Orders with `keep_position=False` bypass Delta deliberately, so a protective close can never be blocked by the gate.

## Agent frameworks

- OpenClaw/Hermes skill: `https://butterflydelta.com/skill.md`
- Remote MCP tools: `https://butterflydelta.com/api/mcp`
- OpenAPI contract: `https://butterflydelta.com/openapi.json`
- Free exact-order coverage preview: `POST https://butterflydelta.com/api/agent/delta-peek`

An Agent402-funded runtime can also resolve and purchase Butterfly Delta as an external seller without adding a second payment client. Call `POST https://agent402.tools/api/route/execute-plus` with `include: "external"`, the task `hummingbot freqtrade ccxt exact-order pre-trade execution integrity gate coinbase gemini`, and the Butterfly Delta request as `params`. Agent402 discloses a $0.05 buyer price, purchases the selected $0.01 preflight on the buyer's behalf, and relays the signed result with provenance. The bot must apply the same fail-closed `PASS` requirement before submission.

FiatDock-compatible agents can search `GET https://fiatdock.com/v1/marketplace/services?q=hummingbot%20freqtrade%20ccxt&include=stats,callhint`, select service `svc_c2cff393-bba7-4b34-b73a-1f62f25d88fb`, and call it through `POST https://fiatdock.com/s/svc_c2cff393-bba7-4b34-b73a-1f62f25d88fb` or FiatDock's `call_service` MCP tool. The gateway returns one $0.01 Base-USDC x402 challenge paying the Ledger-controlled recipient. After settlement it invokes Butterfly Delta's private fulfillment route and returns the signed result; no second payment is required.
