开发者

How to get overrides in historical bloomberg api via python

In the Excel API, Bloomberg allows for overrides and allows you to specific periodicity:

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

In Python:

import win32com.client
from datetime import 开发者_JAVA技巧datetime

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

How do you add the overrides "BEST FPERIOD OVERRIDE","BF"?


Not sure if it can help, but in the .NET SDK, the syntax to be used should be as follows:

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


One change you have to make to Erwin's answer to make this work in python is change overrides.AppendElement() to overrides.AppendElment(). I'm not sure why this typo manifests itself in Python and not other languages, but there it is.


Please see this Python function which can handle up to 6 overrides.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜