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

Download files

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

Source Distribution

betfairstreamer-0.5.3.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

betfairstreamer-0.5.3-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file betfairstreamer-0.5.3.tar.gz.

File metadata

  • Download URL: betfairstreamer-0.5.3.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for betfairstreamer-0.5.3.tar.gz
Algorithm Hash digest
SHA256 54b5151718f63e588d43ef8a0aa9d5f49bdd0037a511bd7628bc6fb90155716b
MD5 ef36534871488e312ba5cac01c6bf4a6
BLAKE2b-256 3d05284594a25839085dd48257fcc61ef7b3542d66416ccb0786a2382933d051

See more details on using hashes here.

File details

Details for the file betfairstreamer-0.5.3-py3-none-any.whl.

File metadata

  • Download URL: betfairstreamer-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for betfairstreamer-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6cd53ca9a002c50c909dd1b85d68726cc3dd021929f1c0cb41438246f996df62
MD5 8adba17b6ec29d32b9b621c002aa6a12
BLAKE2b-256 ec7f8218f4acd454164ad6a77c8ff4cf0b07fb38e3593cf4727a9387c9e04b3e

See more details on using hashes here.

Release history Release notifications | RSS feed

0.8.0

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

This release

0.5.3 This release

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.1

2 files

0.3.0

2 files

Supported by

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