Skip to main content

Lightweight python wrapper for Betfair API-NG

Project description

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)

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

betfairlightweight-2.23.1.tar.gz (219.8 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.23.1-py3-none-any.whl (69.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: betfairlightweight-2.23.1.tar.gz
  • Upload date:
  • Size: 219.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for betfairlightweight-2.23.1.tar.gz
Algorithm Hash digest
SHA256 df5a24879013353a353ce4ad6e2ebb08bd61e7c5d24eb3479cb541920906f5b8
MD5 2c8971a9e0f545dd3a55a7811e5e5444
BLAKE2b-256 f30daf82b6860d3cb64df26a2b567265892a2d0ad7a779304bc07c56d75ea058

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for betfairlightweight-2.23.1-py3-none-any.whl
Algorithm Hash digest
SHA256 648cf7b5927b07d4291289dfa6d853e6e399e47b10a1a83f1f8c0a0d3a7d21e4
MD5 82cc297222a48545e624bbcd0ceccb66
BLAKE2b-256 4a680fbed5f445c5237d14e48bfaaf57e6d6a805a09f9db18f2b65c3b966b4a2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page