Server for Exchange Stream API
Project description
Usage
Subscribe and read from single market stream.
market_stream = BetfairConnection.create_connection(
subscription_message, session_token=session_token, app_key=app_key
)
while True:
update: = market_stream.read() # List[bytes]
print(update)
Subscribe to multiple streams (order, market), using network polling
order_connection = BetfairConnection.create_connection(
order_subscription_message, session_token=session_token, app_key=app_key
)
market_connection = BetfairConnection.create_connection(
market_subscription_message, session_token=session_token, app_key=app_key
)
connection_pool = BetfairConnectionPool()
connection_pool.add(order_connection)
connection_pool.add(market_connection)
while True:
for update in connection_pool.read(): # Generator[List[bytes], None, None]
print(update)
Create Subscriptions
Id field is submitted back to you by betfair on every change message. Used to differentiate streams or group together. Betfair do not check for uniqueness.
market_subscription_message = MarketSubscriptionMessage(
id=1,
market_filter=MarketFilter(
country_codes=["GB"],
event_type_ids=["1"],
market_types=["MATCH_ODDS"]
),
market_data_filter=MarketDataFilter(
ladder_levels=3, # WARNING! Ladder levels are fixed to 3 atm !!
fields=[
Field.EX_MARKET_DEF,
Field.EX_BEST_OFFERS_DISP
]
),
)
order_subscription = OrderSubscriptionMessage(id=2)
Session Token
from betfairlightweight import APIClient
trading: APIClient = APIClient(username, password, app_key, certs=cert_path)
trading.login()
session_token = trading.session_token
Imports
from betfairlightweight import APIClient
from betfairstreamer.betfair.models import (
OrderSubscriptionMessage,
MarketFilter,
MarketDataFilter,
MarketSubscriptionMessage,
)
from betfairstreamer.cache.market_cache import MarketCache
from betfairstreamer.server import BetfairConnection, BetfairConnectionPool
Using market cache.
Assuming you have received a list of stream updates into a list ( List[bytes] )
import orjson
updates: List[bytes] = ...
market_cache = MarketCache()
for update in updates:
decoded_update = orjson.loads(update)
market_books: List[MarketBook] = market_cache(decoded_update)
Marketbook
Each marketbook is backed by numpy arrays. This makes it easy to take slices and do vectorized calculations.
The array contains four axes. [SORT_PRIORITY, SIDE (Back/Lay), LADDER_LEVEL, PRICE/SIZE]
market_book: MarketBook
## best_offers is populated if you are using Field.EX_BEST_OFFERS
### Access price on first selection on side back and on first ladder level
market_book.runner_book.best_offers[0, 0, 0, 0] # scalar value
### Access price and size on second selection side lay, first ladder level
market_book.runner_book.best_offers[1, 1, 0, :] # np.shape(...) == (2,)
## using Field.Field.EX_BEST_OFFERS_DISP, populate best_display (virtualised prices/ what betfair homepage display)
market_book.runner_book.best_display[0, 0, 0, 0] # scalar value
market_book.runner_book.best_display[1, 1, 0, :] # np.shape(...) == (2,)
For more information checkout numpy slicing.
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
betfairstreamer-0.3.1.tar.gz
(15.7 kB
view hashes)
Built Distribution
Close
Hashes for betfairstreamer-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d809adac3d32fcb02672bf7e098466e487f4c060e341089b5741702206a7a901 |
|
MD5 | b8db0d87312b47779879fb796d3475c1 |
|
BLAKE2b-256 | 69756d864e96fee3b67008baae55dcab3788e7515732fc79c8190f64fdfe54dd |