Skip to main content

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.9, 3.10, 3.11, 3.12, 3.13 and 3.14.

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)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

betfairlightweight-2.24.0.tar.gz (221.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

betfairlightweight-2.24.0-py3-none-any.whl (70.0 kB view details)

Uploaded Python 3

File details

Details for the file betfairlightweight-2.24.0.tar.gz.

File metadata

  • Download URL: betfairlightweight-2.24.0.tar.gz
  • Upload date:
  • Size: 221.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.16

File hashes

Hashes for betfairlightweight-2.24.0.tar.gz
Algorithm Hash digest
SHA256 ce4238d25628db34380cc45598c0b5091efe4e29084875803d9489d4879d00df
MD5 b86b3edcc092b324970cdd4f2cf23a6d
BLAKE2b-256 9afb56819027833bb50ff94d5b00b9a8f2592956deb66d901eb5b68fba1d3e77

See more details on using hashes here.

File details

Details for the file betfairlightweight-2.24.0-py3-none-any.whl.

File metadata

File hashes

Hashes for betfairlightweight-2.24.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04f6c5d07d193d72670bfc7433f5efe4ca2e6b20565e5fa19105145c5b7987a4
MD5 28a34c59afb0ebd2e060412bc2a02a0a
BLAKE2b-256 70384f1dbe769e1d3b4518da3793407e5b7e3b1d87f123d49128992343a8f6a2

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.24.0 This release

2 files

2.23.2

2 files

2.23.1

2 files

2.23.0

2 files

2.22.1

2 files

2.22.0

2 files

2.21.2

2 files

2.21.1

2 files

2.21.0

2 files

2.20.4

2 files

2.20.3

2 files

2.20.2

2 files

2.20.1

2 files

2.20.0

2 files

2.19.1

2 files

2.19.0

2 files

2.18.0

2 files

2.17.3

2 files

2.17.2

2 files

2.17.1

2 files

2.17.0

2 files

2.16.7

2 files

2.16.6

2 files

2.16.5

2 files

2.16.4

2 files

2.16.3

2 files

2.16.2

2 files

2.16.1

2 files

2.16.0

2 files

2.15.4

2 files

2.15.3

2 files

2.15.2

2 files

2.15.1

2 files

2.15.0

2 files

2.14.1

2 files

2.14.0

2 files

2.13.2

2 files

2.13.1

2 files

2.13.0

2 files

2.12.3

2 files

2.12.2

2 files

2.12.1

2 files

2.12.0

2 files

2.11.2

2 files

2.11.1

2 files

2.11.0

2 files

2.10.2

2 files

2.10.1

2 files

2.10.0

2 files

2.9.2

2 files

2.9.1

2 files

2.9.0

2 files

2.8.0

2 files

2.7.2

2 files

2.7.1

2 files

2.7.0

2 files

2.6.0

2 files

2.5.0

2 files

2.4.0

2 files

2.3.1

2 files

2.3.0

2 files

2.2.0

2 files

2.1.0

2 files

2.0.1

2 files

1.10.4

2 files

1.10.3

2 files

1.10.2

2 files

1.10.1

2 files

1.10.0

2 files

1.9.1

2 files

1.9.0

2 files

1.8.3

2 files

1.8.2

2 files

1.8.1

2 files

1.8.0

2 files

1.7.2

2 files

1.7.1

2 files

1.7.0

2 files

1.6.4

2 files

1.6.3

2 files

1.6.2

2 files

1.6.1

2 files

1.6.0

2 files

1.5.13

2 files

1.5.12

2 files

1.5.11

2 files

1.5.10

2 files

1.5.9

2 files

1.5.8

2 files

1.5.7

2 files

1.5.6

2 files

1.5.5

2 files

1.5.4

2 files

1.5.3

2 files

1.5.2

2 files

1.5.1

2 files

1.5.0

2 files

1.4.8

2 files

1.4.7

2 files

1.4.6

2 files

1.4.3

2 files

1.4.2

2 files

1.4.1

2 files

1.4.0

2 files

1.3.2

2 files

1.3.1

2 files

1.3.0

2 files

1.2.3

2 files

1.2.2

2 files

1.2.0

2 files

1.1.9

2 files

1.1.8

2 files

1.1.7

2 files

1.1.6

2 files

1.1.5

2 files

1.1.4

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.0.1

2 files

1.0.0

2 files

0.9.10

2 files

0.9.9

2 files

0.9.7

2 files

0.9.5

2 files

0.9.4

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

0.8.5

2 files

0.8.4

2 files

0.8.3

2 files

0.8.2

2 files

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