Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

betfairlightweight

Build Status Coverage Status PyPI version Downloads

Lightweight, super fast (uses C and Rust libraries) pythonic wrapper for Betfair API-NG allowing all betting operations (including market and order streaming) and account operations, see examples.

docs

join betcode slack group

Currently tested on Python 3.8, 3.9, 3.10 and 3.11.

installation

$ pip install betfairlightweight

To use C/Rust libraries install with

$ pip install betfairlightweight[speed]

setup

In order to connect to the Betfair API you will need an App Key, SSL Certificates and a username/password.

App Key

Follow these instructions to get your app key, you can either use a delayed or live key.

SSL certificates

Follow these instructions to set up your SSL certificates. Save your .ctr and .key files to a local directory. The default directory where the library is looking for the keys is '/certs' but you can specify any other directory.

Using the library

The library can then be used as follows:

import betfairlightweight

trading = betfairlightweight.APIClient('username', 'password', app_key='app_key', certs='/certs')

trading.login()

or the following for interactive login with no certs (not as secure)

import betfairlightweight

trading = betfairlightweight.APIClient('username', 'password', app_key='app_key')

trading.login_interactive()
event_types = trading.betting.list_event_types()

[<EventTypeResult>, <EventTypeResult>, ..]

Following endpoints are available:

streaming

Currently two listeners available, below will run the base listener which prints anything it receives. Stream listener is able to hold an order stream or a market stream (one per listener). The listener can hold a cache and push market_books/order_books out via a queue.

Exchange Stream API

from betfairlightweight.filters import (
    streaming_market_filter,
    streaming_market_data_filter,
)

betfair_socket = trading.streaming.create_stream()

market_filter = streaming_market_filter(
    event_type_ids=['7'],
    country_codes=['IE'],
    market_types=['WIN'],
)
market_data_filter = streaming_market_data_filter(
    fields=['EX_ALL_OFFERS', 'EX_MARKET_DEF'],
    ladder_levels=3
)

betfair_socket.subscribe_to_markets(
    market_filter=market_filter,
    market_data_filter=market_data_filter,
)

betfair_socket.start()  # blocking

historic data

The historic endpoint provides some basic abstraction for the historicdata api:

Historic Data API

trading.historic.get_my_data()

[{'plan': 'Basic Plan', 'purchaseItemId': 1343, 'sport': 'Cricket', 'forDate': '2017-06-01T00:00:00'}]

Taking advantage of the streaming code lightweight can parse/output historical data in the same way it process streaming data allowing backtesting or with a custom listener, csv creation (see examples).

Historic Data

stream = trading.streaming.create_historical_stream(
    file_path='horse-racing-pro-sample',
)

stream.start()

or use the stream generator:

stream = trading.streaming.create_historical_generator_stream(
    file_path='horse-racing-pro-sample',
)

g = stream.get_generator()

for market_books in g():
    print(market_books)

Metadata

Release files for betfairlightweight 2.19.0b0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for betfairlightweight 2.19.0b0
File Size Uploaded Provenance Actions
betfairlightweight-2.19.0b0.tar.gz 226.3 kB Without provenance Details for betfairlightweight-2.19.0b0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for betfairlightweight 2.19.0b0
File Interpreter ABI Platform Actions
betfairlightweight-2.19.0b0-py3-none-any.whl Python 3 none any Details for betfairlightweight-2.19.0b0-py3-none-any.whl

Total release size: 294.8 kB

Release files / betfairlightweight-2.19.0b0.tar.gz

Download URL betfairlightweight-2.19.0b0.tar.gz
Size 226.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2b12d48460dc5ecc8d48c10e58047dbb8025bad0d46c1ba2fa3b6cc6b7bb9c2b
BLAKE2b-256 checksum
How to use checksums
a01feeb0b8d4a2e8b2300d9f1819503d0f3b692984dcc7c3dc6ae31dd3d9be2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.31.0 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

Release files / betfairlightweight-2.19.0b0-py3-none-any.whl

Download URL betfairlightweight-2.19.0b0-py3-none-any.whl
Size 68.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
110171809fe8e92da5440eb1c797b133ad304ddb39dae3d488ee84b183c3b3ff
BLAKE2b-256 checksum
How to use checksums
8dbc16f8d74ca2179817eebc1b21fe664fca14f96c1858b343b3e9f2f2617619
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.31.0 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

Security for betfairlightweight 2.19.0b0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page