Betfair Streaming API: No Packets Received with Valid Market ID and Fields.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TigerRoy
    Junior Member
    • Jul 2025
    • 3

    #1

    Betfair Streaming API: No Packets Received with Valid Market ID and Fields.

    Hi all,

    Hope someone can help me, I've been trying to set up a basic price monitoring/bet submisson automation script and I'm having difficulty getting price streaming to work. Here's all the info, please advise if anything else would be useful.

    Betfair Streaming API using betfairlightweight (v2.20.4, Python 3.11.0, Windows 11). I can get a market catalogue and select a valid GB/IE win market. But when I start a market stream (with marketId auto-selected), the stream starts but no packets are ever received. The queue is always empty, and the main loop never gets data.

    Environment:
    • betfairlightweight==2.20.4
    • python==3.11.0
    • Windows 11
    • API credentials loaded from .env (not shown)

    Code:
    Code:
    import os
    import queue
    from datetime import datetime, timezone
    from dotenv import load_dotenv
    from betfairlightweight import APIClient
    from betfairlightweight.streaming.listener import StreamListener
    
    # --- Load credentials ---
    load_dotenv()
    USERNAME = os.getenv('BF_USERNAME')
    APP_KEY = os.getenv('BF_APP_KEY')
    SESSION_TOKEN = os.getenv('BF_SESSION_TOKEN')
    
    # --- Betfair API client ---
    client = APIClient(username=USERNAME, password='', app_key=APP_KEY)
    client.session_token = SESSION_TOKEN
    
    # --- Get next UK/IE market ---
    bf_filter = {
        "eventTypeIds": ["7"],
        "marketTypeCodes": ["WIN"],
        "inPlayOnly": False,
        "eventCountries": ["GB", "IE"]
    }
    catalogue = client.betting.list_market_catalogue(
        filter=bf_filter,
        max_results=100,
        market_projection=["MARKET_START_TIME", "EVENT"],
        sort="FIRST_TO_START"
    )
    
    def as_utc(dt):
        if dt is None: return None
        if dt.tzinfo is None: return dt.replace(tzinfo=timezone.utc)
        else: return dt.astimezone(timezone.utc)
    
    now = datetime.now(timezone.utc)
    racing_catalogue = []
    for m in catalogue:
        mst = as_utc(getattr(m, 'market_start_time', None))
        country = getattr(getattr(m, 'event', None), 'country_code', None)
        if mst and mst > now and country in ('GB', 'IE'):
            racing_catalogue.append(m)
    if not racing_catalogue:
        print("No GB/IE racing markets found!")
        exit(1)
    
    market = min(racing_catalogue, key=lambda m: m.market_start_time)
    MARKET_ID = market.market_id
    venue = getattr(market.event, "venue", "Unknown")
    mst = market.market_start_time.strftime('%Y-%m-%d %H:%M:%S')
    print(f"Auto-selected market: {venue} {market.market_name} @ {mst} (marketId={MARKET_ID})")
    
    # --- Streaming setup ---
    output_q = queue.Queue()
    listener = StreamListener()
    listener.output_queue = output_q
    stream = client.streaming.create_stream(unique_id=6, listener=listener, timeout=60)
    
    market_filter_obj = {'marketIds': [MARKET_ID]}
    market_data_filter = {
        'fields': ['EX_LTP', 'EX_MARKET_DEF']
    }
    stream.subscribe_to_markets(market_filter_obj, market_data_filter)
    
    print("About to start stream...")
    stream.start()
    print("Stream started! Entering main loop...")
    
    while True:
        print("Waiting for packet (10s timeout)...")
        try:
            packet = output_q.get(timeout=10)
            print(f"GOT PACKET: type={type(packet)}, first 200 chars: {str(packet)[:200]}")
        except queue.Empty:
            print("No packet in 10 seconds.")
        except Exception as e:
            print("Exception in main loop:", e)
            break
    Symptoms:
    • Market is auto-selected and shown (marketId, name, etc.)
    • stream.start() does not crash, but nothing ever comes through the listener queue (output_q.get() always times out)
    • When run with betfairlightweight logging set to DEBUG, I do see lots of stream data in the logs
    • Changing/adding/removing fields like EX_LTP/EX_MARKET_DEF/etc. does not affect the outcome
    • Credentials and token are valid (market list loads fine)

    Sample console output:
    (personal info redacted, this is after starting the script)

    Auto-selected market: Kempton 2m Hcap @ 2025-08-06 19:55:00 (marketId=1.246317759) About to start stream... Stream started! Entering main loop... Waiting for packet (10s timeout)... No packet in 10 seconds. Waiting for packet (10s timeout)... No packet in 10 seconds. ... (repeats forever)

    What I’ve tried:
    • Changing fields in market_data_filter
    • Manually specifying different marketIds
    • Running with logging at DEBUG and INFO
    • Upgrading/downgrading betfairlightweight
    • Verifying API credentials and session token are valid and live

    When running with DEBUG logging (logging.basicConfig(level=logging.DEBUG)), I see that Betfair is sending runner updates and data does appear in logs, but not through my queue/listener code.
    I have also verified the listener.output_queue matches the output_q object (checked via id()), so it’s not a wiring/reference bug.

    Is there something wrong in my subscribe or stream setup? Do I need to pass any additional parameters, or is there a new way I should be requesting these fields in recent API versions?

    Any tips or known pitfalls with betfairlightweight streaming (v2.20.4) and Python 3.11 would be greatly appreciated!

    I'm thinking it's a problem with how the field parameters are being processed, not sure if I've got the format wrong or have missed an essential parameter off the request? Or got my code incorrect somewhere.


    Please help and put me out of my misery!

    Thanks in advance.

    Tiger R.
  • emrah
    Junior Member
    • Sep 2025
    • 1

    #2
    It is a bit late, but I got the same issue. I called stream.start() async for a solution. Otherwise it was blocking the rest of the script.

    Comment

    Working...
    X