簡體   English   中英

使用python通過excel從bloomberg api獲取數據

[英]get data from bloomberg api with python via excel

我編寫了一些代碼,通過使用 python 在 excel 中編寫查詢成功地從bloomberg api 中提取數據,然后啟動 excel 通過bloomberg 插件獲取數據,將數據傳輸到 csv(以便在 python 和其他中更容易使用)下游需求)和回報。

唯一的問題是,為了將數據拉入 excel,我必須在保存文件之前設置 time.sleep(40),因為通過 excel 的連接需要足夠的時間來拉取數據。 我想知道是否有一種方法可以自動檢測數據何時被提取? - 計划是將其擴展為在循環中或跨多個線程執行許多查詢,為了提高效率,我需要檢測何時提取數據以開始下一步。

任何想法真的會有幫助嗎? 查看函數 run_VBA 這是我目前的代碼:

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()

我知道只從 python api 獲取這些數據會更有意義,但是不支持我在這里做的 'bsrch' 類型的查詢。

但是我願意接受更好的方法嗎? 最好是python,或者可能是R

我建議在 R 中使用Rblpapi包,因為它有一個 bsrch 函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM