Skip to main content

Betfairstreamer

What this library provides

  • Run single or multiple streams simultaneously (single threaded)
  • Market cache and order cache, these provide abstractions over the betfairstream
  • Using numpy arrays to slicing markets selections.
  • Async streaming (Optional)
  • Parse historical data (Example below)

Installation

pip install betfairstreamer

Demo

Tutorial

Credentials

Create a credential file credentials.env with following content.

USERNAME=BETFAIR_USERNAME
PASSWORD=BETFAIR_PASSWORD
APP_KEY=BETFAIR_APP_KEY
LOCALE=IFANY
CERT_PATH=PATH_TO_BETFAIR_CERTIFICATES

Virtual environment

Create a virtual environment, needs python >= 3.8.0

python3.8 -m venv venv
source venv/bin/activate
pip install betfairstreamer==.5.3

Create Application

main.py

import logging
import time

import numpy as np
import orjson

from betfairstreamer.betfair_api import (
    BetfairMarketFilter,
    BetfairMarketDataFilter,
)
from betfairstreamer.cache import MarketCache, OrderCache
from betfairstreamer.stream import create_connection_pool
from betfairstreamer.utils import create_market_subscription, create_order_subscription

np.set_printoptions(precision=3)

logging.basicConfig(level=logging.INFO)

horses_subscription_message = create_market_subscription(
    # Asian Handicap is not yet supported if using EX_ALL_OFFER.
    market_filter=BetfairMarketFilter(eventTypeIds=["7"], marketTypes=["WIN"]),
    market_data_filter=BetfairMarketDataFilter(
        ladderLevels=3,  # Ladder levels are fixed to 3 at the moment.
        fields=[
            "EX_MARKET_DEF",
            "EX_BEST_OFFERS",
            "EX_LTP",
            "EX_BEST_OFFERS_DISP",
            "EX_ALL_OFFERS",
        ],
    ),
    conflate_ms=1000 # Remove this if you want full speed ...
)

order_subscription_message = create_order_subscription()

market_cache = MarketCache()
order_cache = OrderCache()

connection_pool = create_connection_pool([horses_subscription_message, order_subscription_message])

for update in connection_pool.read():
    update = orjson.loads(update)

    market_updates = market_cache(update)
    order_updates = order_cache(update)

    for market_book in market_updates:
        # From full price ladder take sortPriority one and two and
        # only ladder levels having a size greater or equal to 2.0.
        maskB = market_book.full_price_ladder[0, 0, :, 1] >= 2.0
        maskL = market_book.full_price_ladder[0, 1, :, 1] >= 2.0
        # Marketbook price size index: [SortPriority, LAY/BACK, LadderLevel, Price/Size]
        print()
        print(f"market ID: {market_book.market_id}\n"
              f"virtualized:\n"
              f"  BACK: {str(market_book.best_display[0, 0, :, 0])}\n"
              f"  LAY:  {str(market_book.best_display[0, 1, :, 0])}\n"
              f"best offer:\n"
              f"  BACK: {str(market_book.best_offers[0, 0, :, 0])}\n"
              f"  LAY:  {str(market_book.best_offers[0, 1, :, 0])}\n"
              f"full (capped at 8):\n"
              f"  BACK: {str(market_book.full_price_ladder[0, 0, maskB, 0][-8:])}\n"
              f"  LAY:  {str(market_book.full_price_ladder[0, 1, maskL, 0][:8])}\n"
              )

    for order in order_updates:
        print(order)

Run

env $(cat credentials.env) python main.py

Read historical data

import bz2 # If you download data from betfair

market_cache = MarketCache()

# market_updates = open("market_file", "r").readlines() # If files are not compressed
# market_updates = bz2.open("market_file.bz2", "r").readlines()  # If files are compressed

for market_update in market_updates:
    market_books = market_cache(orjson.loads(market_update))    

    for market_book in market_books: 
        # Do something with market_book...

Benchmark

Setup: Two processes, one sending betfair stream messages , one receiving.

Hardware: I7 8550U, 16GB ram

Results: 
 * Using a market cache it can read around 25k messages/second
 * Without market cache >> 100 Mb/s

Release files for betfairstreamer 0.5.3

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

Source distribution (sdist)

Source distribution for betfairstreamer 0.5.3
File Size Uploaded
betfairstreamer-0.5.3.tar.gz 16.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for betfairstreamer 0.5.3
File Interpreter ABI Platform
betfairstreamer-0.5.3-py3-none-any.whl Python 3 none any Details

Total release size:29.7 kB

Release files / betfairstreamer-0.5.3.tar.gz

Download URL betfairstreamer-0.5.3.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
54b5151718f63e588d43ef8a0aa9d5f49bdd0037a511bd7628bc6fb90155716b
BLAKE2b-256 checksum
How to use checksums
3d05284594a25839085dd48257fcc61ef7b3542d66416ccb0786a2382933d051
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

Release files / betfairstreamer-0.5.3-py3-none-any.whl

Download URL betfairstreamer-0.5.3-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6cd53ca9a002c50c909dd1b85d68726cc3dd021929f1c0cb41438246f996df62
BLAKE2b-256 checksum
How to use checksums
ec7f8218f4acd454164ad6a77c8ff4cf0b07fb38e3593cf4727a9387c9e04b3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

Release history Release notifications | RSS feed

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

This release

0.5.3 This release

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release 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