[Python] how to get Bloomberg intraday bar data outside of regular nyse trading hours in bython

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I was able to find code (sorry for no reference) on Stack Overflow that allows me to get bar data from Bloomberg. Unfortunately it only gives me data for regular trading hours. How can I get extended hours (AM and PM) data as well? Below is the code that I'm currently using.

    from datetime import datetime,time

    #An intraday bar function which accepts multiple days
    def bdibMultiDay(ticker,start_date,end_date,typ,tz,interval=1,session_open=None, session_close=None):
    from xbbg.core import conn,process
    from pandas import DataFrame
    import pytz

    #If no explicit session times given, impute from input datetimes
    if(session_open is None): session_open = start_date.time()
    if(session_close is None): session_close = end_date.time()

    time_fmt = '%Y-%m-%dT%H:%M:%S'
    local = pytz.timezone(tz)

    req = process.create_request(service='//blp/refdata',request='IntradayBarRequest')

    req.set('security',ticker)
    req.set('eventType',typ)
    req.set('interval',interval)
    req.set('startDateTime',start_date.astimezone(pytz.utc).strftime(time_fmt))
    req.set('endDateTime',end_date.astimezone(pytz.utc).strftime(time_fmt))

    conn.send_request(request=req)

    data = ( DataFrame(process.rec_events(func=process.process_bar))
    .set_index('time')
    .rename_axis(index=None)
    .rename(columns={'numEvents': 'num_trds'})
    .tz_localize('UTC')
    .tz_convert(tz) )

    def isInSession(dt) -> bool:
    t = dt.time()
    return (t >= session_open) and (t <= session_close)

    return data.loc[map(isInSession,data.index)]

    def testbdlMultiDay():
    ticker = 'QQQ US Equity'
    start_date = datetime(2025,4,3,7,00)
    end_date = datetime(2025,4,3,18,30)
    typ = 'TRADE'
    tz='America/New_York'
    interval=5

    df = bdibMultiDay(ticker,start_date,end_date,typ,tz,interval)
    return df


    df=testbdlMultiDay()



    The code above only gives regular hours, not extended hours. I would like both.

    Continue reading...

Compartilhe esta Página