matthewgilbert / pdblp

pandas wrapper for Bloomberg Open API
MIT License
240 stars 69 forks source link

Getting historical FX rates. #64

Open spinor8 opened 5 years ago

spinor8 commented 5 years ago

Code Sample, a copy-pastable example if possible

exchange_rate_df = con.ref_hist("USDHKD BGN Curncy", "PX_LAST", dates=['20170331', '20170406'])
print(exchange_rate_df)

Problem description

I am just trying to get the historical FX rates for two (sufficient for MCVE) dates and I am getting the same values for both dates, which I suspect is the current value. I have looked at the examples in the tutorial and API but I can't see anything specific to this case. What am I missing? Thanks.

Expected Output

2017-03-31 7.7709 2017-04-06 7.7700

Version Information

'0.1.8'

matthewgilbert commented 5 years ago

For historical data you should use bdh(), e.g.

con.bdh("USDHKD BGN Curncy", "PX_LAST", start_date='20170331' end_date='20170406')

ref_hist() is intended to artificially create a time series for data from the Blomberg API that does not support historical requests but does support point in time overrides, thus by iteratively making calls with point in time overrides a time series can be patched together. This is slower and much less efficient for data limits when historical data requests are supported such as in this case.

Also for future reference its good to set con.debug = True and post the associated output to indicate what the underlying blpapi library is returning, which makes debugging easier.

spinor8 commented 5 years ago

Thanks for your input. Can you elaborate on how setting con.debug can indicate what sort of data I am receiving back, specifically whether it is historical or current? What should I be looking out for? The tutorial had one or two examples where the date was overridden and naturally I followed it.

Before the current incident, there was another time that I was using pdblp to get historical weights of the index constituents at the end of the last 12 quarters. I thought I was getting the correct values only to realize much later that the differences were actually second-by-second real-time weight variations of the index due to the price of the constituents changing. It took me ages to debug it and find the right syntax for it.

matthewgilbert commented 5 years ago

At it's core pdblp is just a wrapper around blpapi, which is the official Bloomberg maintained library for accessing their API from python. There are times when looking at the raw messages sent and received via blpapi is useful, and setting debug=True will show you these. If the data is coming back and being parsed appropriately but the values do not look like what you expected, than this is likely not an issue with pdblp but something to do with blpapi, such as a mis-specified request.

At a high level if you want historical data, use bdh(), if you want reference data, use ref(). These make underlying HistoricalDataRequests and ReferenceDataRequests respectively. Ultimately if you are trying to make some request that is non standard, there is no getting around reading the underlying documentation for blpapi, which can be found using WAPI in the terminal or using HELP HELP and inquiring about it with Bloomberg.

Below is an example

In [1]: import pdblp
In [2]: con = pdblp.BCon(debug=True, port=8194, timeout=5000).start()
In [3]: con.bdh('SPY US Equity', 'PX_LAST',  '20150629', '20150630')
DEBUG:root:Sending Request:
 HistoricalDataRequest = {
    securities[] = {
        "SPY US Equity"
    }
    fields[] = {
        "PX_LAST"
    }
    periodicityAdjustment = ACTUAL
    periodicitySelection = DAILY
    startDate = "20150629"
    endDate = "20150630"
    overrides[] = {
    }
}
DEBUG:root:Message Received:
 HistoricalDataResponse = {
    securityData = {
        security = "SPY US Equity"
        eidData[] = {
        }
        sequenceNumber = 0
        fieldExceptions[] = {
        }
        fieldData[] = {
            fieldData = {
                date = 2015-06-29
                PX_LAST = 205.420000
            }
            fieldData = {
                date = 2015-06-30
                PX_LAST = 205.850000
            }
        }
    }
}
Out [3]: 
ticker      SPY US Equity
2015-06-29         205.42
2015-06-30         205.85