跳到主要内容

Betfair

Founded in 2000, Betfair operates the world’s largest online betting exchange, with its headquarters in London and satellite offices across the globe.

NautilusTrader provides an adapter for integrating with the Betfair REST API and Exchange Streaming API.

安装

Install NautilusTrader with Betfair 支持 via pip:

pip install --upgrade "nautilus_trader[betfair]"

To build from source with Betfair extras:

uv sync --all-extras

示例

You can find live example scripts here.

Betfair 文档

For API details and troubleshooting, see the official Betfair Developer Documentation.

Application Keys

Betfair requires an Application Key to authenticate API requests. After registering and funding your account, obtain your key using the API-NG Developer AppKeys Tool.

API credentials

Supply your Betfair credentials via 环境 variables or 客户端 配置:

export BETFAIR_USERNAME=<your_username>
export BETFAIR_PASSWORD=<your_password>
export BETFAIR_APP_KEY=<your_app_key>
export BETFAIR_CERTS_DIR=<path_to_certificate_dir>
提示

We recommend using 环境 variables to manage your credentials.

概览

The Betfair adapter provides three primary components:

  • BetfairInstrumentProvider: loads Betfair markets and converts them into Nautilus instruments.
  • BetfairDataClient: streams real-time market data from the Exchange Streaming API.
  • BetfairExecutionClient: submits orders (bets) and tracks execution status via the REST API.

Capability Matrix

Betfair operates as a betting exchange with unique characteristics compared to traditional financial exchanges:

Order Types

Order TypeSupportedNotes
MARKET-Not applicable to betting exchange.
LIMIT订单 placed at specific odds.
STOP_MARKET-Not supported.
STOP_LIMIT-Not supported.
MARKET_IF_TOUCHED-Not supported.
LIMIT_IF_TOUCHED-Not supported.
TRAILING_STOP_MARKET-Not supported.

执行 Instructions

InstructionSupportedNotes
post_only-Not applicable to betting exchange.
reduce_only-Not applicable to betting exchange.

Time in force options

Time in forceSupportedNotes
GTC-Betting exchange uses different model.
GTD-Betting exchange uses different model.
FOK-Betting exchange uses different model.
IOC-Betting exchange uses different model.

Advanced Order Features

功能SupportedNotes
Order ModificationLimited to non-exposure changing fields.
Bracket/OCO 订单-Not supported.
Iceberg 订单-Not supported.

Batch operations

OperationSupportedNotes
Batch Submit-Not supported.
Batch Modify-Not supported.
Batch Cancel-Not supported.

Position management

功能SupportedNotes
Query 头寸-Betting exchange model differs.
Position mode-Not applicable to betting exchange.
Leverage control-No leverage in betting exchange.
Margin mode-No margin in betting exchange.

Order querying

功能SupportedNotes
Query open 订单List all active bets.
Query order historyHistorical betting 数据.
Order status updates实时 bet state changes.
Trade historyBet matching and settlement 报告.

Contingent 订单

功能SupportedNotes
Order lists-Not supported.
OCO 订单-Not supported.
Bracket 订单-Not supported.
Conditional 订单-Basic bet conditions only.

配置 options

The following 执行 客户端 配置 options affect order behavior:

OptionDefaultDescription
calculate_account_stateTrueIf True, calculates account state from events.
request_account_state_secs300Interval for account state checks in seconds (0 disables).
reconcile_market_ids_onlyFalseIf True, only reconciles 订单 for configured market IDs.
ignore_external_ordersFalseIf True, silently ignores 订单 not found in 缓存.

配置

Here is a minimal example showing how to configure a live TradingNode with Betfair clients:

from nautilus_trader.adapters.betfair import BETFAIR
from nautilus_trader.adapters.betfair import BetfairLiveDataClientFactory
from nautilus_trader.adapters.betfair import BetfairLiveExecClientFactory
from nautilus_trader.config import TradingNodeConfig
from nautilus_trader.live.node import TradingNode

# Configure Betfair data and execution clients (using AUD account currency)
config = TradingNodeConfig(
data_clients={BETFAIR: {"account_currency": "AUD"}},
exec_clients={BETFAIR: {"account_currency": "AUD"}},
)

# Build the TradingNode with Betfair adapter factories
node = TradingNode(config)
node.add_data_client_factory(BETFAIR, BetfairLiveDataClientFactory)
node.add_exec_client_factory(BETFAIR, BetfairLiveExecClientFactory)
node.build()