Skip to content

Lightweight python wrapper for Betfair API-NG (with streaming)

License

Notifications You must be signed in to change notification settings

dan4ever/betfair

 
 

Repository files navigation

betfairlightweight

Build Status Coverage Status PyPI version

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

Documentation

Join slack group

Currently tested on Python 2.7, 3.4, 3.5, 3.6 and 3.7.

installation

$ pip install betfairlightweight

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 go for a delayed or a 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 if '/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 (no certs)

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

In development so breaking changes likely.

>>> from betfairlightweight.filters import (
        streaming_market_filter,
        streaming_market_data_filter,
    )

>>> betfair_socket = trading.streaming.create_stream(
        unique_id=0,
        description='Test Market Socket',
    )

>>> 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(async_=False)

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

In development so breaking changes likely.

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

>>> stream.start(async_=False)

or use the stream generator:

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

>>> g = stream.get_generator()

>>> for i in g:
>>>     print(i)

About

Lightweight python wrapper for Betfair API-NG (with streaming)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%