如何通过python在历史的bloomberg api中取代

时间:2011-04-13 09:32:41

标签: python api win32com bloomberg

在Excel API中,Bloomberg允许覆盖并允许您使用特定的周期:

BDH("IBM EQUITY","BEST SALES","1/1/2011","2/1/2011","BEST FPERIOD OVERRIDE","BF",, "DAYS=W","FILL=C","SORT=D")

在Python中:

import win32com.client
from datetime import datetime

blp = win32com.client.Dispatch('Bloomberg.Data.1')
ibm_sales=blp.BLPGetHistoricalData('ibm equity', 'best sales', datetime(2011,1,1), datetime(2011,2,1))

如何添加覆盖“BEST FPERIOD OVERRIDE”,“BF”?

3 个答案:

答案 0 :(得分:3)

不确定它是否有帮助,但在.NET SDK中,要使用的语法应如下所示:

Element overrides = request["overrides"];
Element override1 = overrides.AppendElement();
override1.SetElement("fieldId", "BEST FPERIOD OVERRIDE");
override1.SetElement("value", "BF");

答案 1 :(得分:2)

你必须对Erwin的回答做出一些改变,以便在python中使用它是改变覆盖.AppendElement()来覆盖.AppendElment()。我不确定为什么这个拼写错误会出现在Python而不是其他语言中,但它确实存在。

答案 2 :(得分:0)

请参阅此Python函数,该函数最多可以处理6个覆盖。

# Fill SessionOptions
sessionOptions = blpapi.SessionOptions()
sessionOptions.setServerHost('localhost')
sessionOptions.setServerPort(8194)

# Create a Session
session = blpapi.Session(sessionOptions)

# Start a Session
if not session.start():
    print("Failed to start session.")
    return

try:
    # Open service to get Bloomberg data from
    if not session.openService("//blp/refdata"):
        print("Failed to open //blp/refdata")
        return
    # Obtain previously opened service
    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("ReferenceDataRequest")
    request.getElement("securities").appendValue(securities)
    request.getElement("fields").appendValue(requestField)
    overrides1 = request.getElement("overrides").appendElement()
    overrides1.setElement("fieldId", overrideID1)
    overrides1.setElement("value", overrideValue1)

    if (overrideID2 is not None and overrideValue2 is not None):
        overrides2 = request.getElement("overrides").appendElement()
        overrides2.setElement("fieldId", overrideID2)
        overrides2.setElement("value", overrideValue2)      
    if (overrideID3 is not None and overrideValue3 is not None):
        overrides3 = request.getElement("overrides").appendElement()
        overrides3.setElement("fieldId", overrideID3)
        overrides3.setElement("value", overrideValue3)    
    if (overrideID4 is not None and overrideValue4 is not None):
        overrides4 = request.getElement("overrides").appendElement()
        overrides4.setElement("fieldId", overrideID4)
        overrides4.setElement("value", overrideValue4)  
    if (overrideID5 is not None and overrideValue5 is not None):
        overrides5 = request.getElement("overrides").appendElement()
        overrides5.setElement("fieldId", overrideID5)
        overrides5.setElement("value", overrideValue5)  
    if (overrideID6 is not None and overrideValue6 is not None):
        overrides6 = request.getElement("overrides").appendElement()
        overrides6.setElement("fieldId", overrideID6)
        overrides6.setElement("value", overrideValue6)  

    # Send the request
    session.sendRequest(request)

    # Process received events
    while(True):
        # We provide timeout to give the chance for Ctrl+C handling:
        ev = session.nextEvent(500)
        for msg in ev:
            if msg.messageType() == "ReferenceDataResponse":
                return msg.getElement("securityData").getValueAsElement(0).getElement("fieldData").getElementAsString(requestField)
        if ev.eventType() == blpapi.Event.RESPONSE:
            # Response completely received, so we could exit
            break
finally:
    # Stop the session
    session.stop()