Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get data from bloomberg api with python via excel

I've written some code which successfully pulls data from the bloomberg api by using python to write a query in excel, it then launches excel gets the data via the bloomberg plugin, transfers the data into a csv (for easier use in python and other downstream needs) and returns.

The only issue is that for the data to be pulled into excel I have to set time.sleep(40) before I save the file as the connection through excel needs enough time to pull the data. I am wondering if there is a way to automatically detect when the data has been pulled? - The plan is to scale this up to do many queries in a loop or across multiple threads and for this to be efficient I need to detect when the data has been pulled to start the next step.

Any ideas would be really helpful? See the function run_VBA Here is my code so far:

import xlsxwriter
import pandas as pd
import xlwings as xl
import glob
import openpyxl
import time
import os
import win32com.client

def write_bloomberg_query_in_excel():
    """main function, make an excel workbook containing an api query, open the file and allow the data to 
        be pulled from the api, save and close, then transfer the data into a pandas df and csv"""

    WB = 'C:/python_workspace/bloomberg_api_data_pull/excel_queries/daily_wind_temp_precip.xlsx'
    location = "EGLL"

    make_workbook(WB, location)

    run_VWA(WB, location)

    df = df_from_excel(WB, location)  # sheetname is optional
    df.to_csv(WB.split('.')[0]+'.csv', index=False)

    return

def run_VWA(WB, location):
    """open the excel file, allow enough time to pull the data, then close and save"""

    bb = 'C:/blp/API/Office Tools/BloombergUI.xla'
    xl=win32com.client.DispatchEx("Excel.Application")  
    xl.Workbooks.Open(bb)
    xl.AddIns("Bloomberg Excel Tools").Installed = True
    wb = xl.Workbooks.Open(Filename=WB) #opens workbook in readonly mode.

    xl.Visible = False
    # need help here!! this time works for this query but I need to scale
    # the api calls and need a way to select time or detect when 
    # the download has happened
    time.sleep(40)         

    wb.Close(SaveChanges=1)

    xl.Quit()
    #Cleanup the com reference. 
    del xl   

    return

def make_workbook(WB, location):
    """write a bloomberg api query into an an excel workbook """

    # Create a workbook and add a worksheet.
    workbook = xlsxwriter.Workbook(WB)
    worksheet = workbook.add_worksheet(location)

    # Some data we want to write to the worksheet.
    W = """=BSRCH("comdty:weather","provider=wsi","location={}",
        "model=ACTUALS","frequency=DAILY","target_start_date=2018-08-01",
        "target_end_date=2018-12-31", 
        "fields=WIND_SPEED|TEMPERATURE|PRECIPITATION_24HR")""".format(location)

    # write to worksheet using formula
    worksheet.write(0, 0, W)
    # close
    workbook.close()
    return

def df_from_excel(path, SN):
    """read the contents of an excel file into a pandas dataframe"""
    app = xl.App(visible=False)
    book = app.books.open(path)
    sheet = book.sheets(SN)
    book.save()
    df = pd.read_excel(path, sheet_name=SN)
    app.kill()
    return df


if __name__=="__main__":
    write_bloomberg_query_in_excel()

I know it would make more sense to just get this data from the python api, however there is no support for the 'bsrch' type of query which I am doing here.

However I'm open to better approaches? Preferably python, or possibly R

like image 857
user3062260 Avatar asked Nov 06 '22 21:11

user3062260


1 Answers

I would suggest using the Rblpapi package in R, as it has a bsrch function.

like image 168
Titus Buckworth Avatar answered Nov 14 '22 22:11

Titus Buckworth