Lightweight python wrapper for Betfair API-NG
Project description
betfairlightweight
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.
Currently tested on Python 3.6, 3.7, 3.8, 3.9 and 3.10.
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:
-
trading.login
-
trading.login_interactive
-
trading.keep_alive
-
trading.logout
-
trading.betting
-
trading.account
-
trading.navigation
-
trading.scores
-
trading.streaming
-
trading.historical
-
trading.in_play_service
-
trading.race_card
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.
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:
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).
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
Built Distribution
Hashes for betfairlightweight-2.16.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e20c5abdce4beeee608a6c7acd6cdf5bf09ecc7bf3b4ea9144281f9e374e0a8 |
|
MD5 | 40b45f106554cb780efe401c3e3ce255 |
|
BLAKE2b-256 | a78061bb552331ea3a60ab9ac23e00abb464c4c9d8ec5a7eee692afdd6356dea |
Hashes for betfairlightweight-2.16.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79026b4a7a2b9b7e3ff6650d3111a0222df2a31354a76dc100dc065c648e63ab |
|
MD5 | 00190ead3b54b8a7da87a8a7660a4ce6 |
|
BLAKE2b-256 | 4e82a3ab093bcb55ebbb1f6145989360ca09a2d1d2facde7f651423b8ebc2736 |