- Windows 10 x64.
- Latest version of Anaconda.
First, we need to install Visual Studio Build Tools in the Bloomberg terminal. You can get the latest version of VS Tools 2019 here. VS Tools allow us to compile and package applications from source.
After installing VS Build Tools, you should update your anaconda packages. Use the following code on your Anaconda Powershell Prompt (run it with admin privileges):
# Go to the folder where Anaconda is installed
cd c:\ProgramData\Anaconda\Scripts
# Update all packages
.\conda.exe update --all
A couple of tutorials on the web (here and here) suggest you should build from source the Bloomberg API for Python. However, this didn't work for me. So I downloaded the wheel file from the Bloomberg repositories. The file I've selected works with a PC with a x64 architecture (amd64
), for the 3.7 version of Python (cp37
), and it must match with the SDK version of C++ that comes with Bloomberg (blpapi-3.12.2
). You'll get a message of error if they don't match:
Mismatch between C++ and Python SDK libraries.
Python SDK version 3.13.1 Found C++ SDK version 3.12.3.1
To install the wheel package run from your Anaconda Powershell Prompt:
.\pip.exe install ~/Downloads/blpapi-3.12.2-cp37-cp37m-win_amd64.whl
xbbg
and pdblp
are packages for Python. According to its website xbbg
is a Bloomberg data toolkit for humans, and pdblp
is a pandas wrapper for the Bloomberg API. Both of them are wrappers for the Bloomberg API and they provide a simple way to get data from Bloomberg using the API. We'll install both of them.
From the Anaconda Powershell Prompt, run:
# To install xbbg
.\pip.exe install xbbg
# To install pbblp
.\pip.exe install pdblp
You can find the documentation with some examples for xbbg
here and for pdblp
here.
Import the library.
from xbbg import blp
We are going to download the yield curve for the US and Peru. On your Bloomberg terminal YCGT0025 Index
and YCGT0361 Index
are the yield curves indexes for US and Peru, respectively. With the bdp
we ask for the NAMES
of the indexes.
blp.bdp(["YCGT0025 Index", "YCGT0361 Index"], "NAME")
And we get:
name | |
---|---|
ticker | |
YCGT0025 Index | US Treasury Actives Curve |
YCGT0361 Index | PEN Peru Sovereign Curve |
So now we are going to get the components of each index. Using bds
command and with the INDX_MEMBERS
field from Bloomberg.
# Securities for the US Treasury Actives Curve
YC_US = blp.bds("YCGT0025 Index", "INDX_MEMBERS")
# Securities for the PEN Peru Sovereign Curve
YC_Peru = blp.bds("YCGT0361 Index", "INDX_MEMBERS")
Now we get the yield (YLD_YTM_ASK
), the name (SECURITY NAME
), and the maturity (MATURITY
) of each point of the yield curve that we got in the previous step, using the blp
command.
# US Treasury Actives Curve
YC_US_VAL = blp.bdp(YC_US.member_ticker_and_exchange_code.tolist(), ['YLD_YTM_ASK', 'SECURITY NAME', 'MATURITY'])
# PEN Peru Sovereign Curve
YC_PE_VAL = blp.bdp(YC_Peru.member_ticker_and_exchange_code.tolist(), ['YLD_YTM_ASK', 'SECURITY NAME', 'MATURITY'])
We are going to see the type of data of each table with dtypes
and then we are going to convert the maturity to a date format. We have to load some libraries first.
# You'll see that the maturity column for each is not a date.
YC_US_VAL.dtypes
YC_PE_VAL.dtypes
# Load libraries
# Data
import pandas as pd
import numpy as np
# Time
from datetime import date
# Now we convert the maturity column to date
# US Treasury Actives Curve
YC_US_VAL.maturity = pd.to_datetime(YC_US_VAL.maturity)
# PEN Peru Sovereign Curve
YC_PE_VAL.maturity = pd.to_datetime(YC_PE_VAL.maturity)
We are going to create a column with the maturity of each bond in term of years. For that we are going to substract the maturity from the actual date.
# US Treasury Actives Curve
YC_US_VAL["Yr"] = (YC_US_VAL.maturity - pd.to_datetime(date.today()))/np.timedelta64(1,'Y')
# PEN Peru Sovereign Curve
YC_PE_VAL["Yr"] = (YC_PE_VAL.maturity - pd.to_datetime(date.today()))/np.timedelta64(1,'Y')
And finally its time to graph each yield curve.
# Sort data by Yr column
YC_US_VAL = YC_US_VAL.sort_values(by=["Yr"])
YC_PE_VAL = YC_PE_VAL.sort_values(by=["Yr"])
# Plot curves
plt.plot(YC_US_VAL.Yr, YC_US_VAL.yld_ytm_ask, 'b')
plt.plot(YC_PE_VAL.Yr, YC_PE_VAL.yld_ytm_ask, 'r')
Set the tickers for the yield curve.
US_Yld = ['912796VZ Govt', '912796WD Govt', '912796SX Govt', '912796TL Govt', '912796TJ Govt', '912828YC Govt', '912828YF Govt', '912828YE Govt', '912828YD Govt', '912828YB Govt', '912810SJ Govt']
Set the fields.
flds = ['Security_Name', 'YLD_YTM_ASK']
Retrieve the data
blp.bdp(US_Yld, flds)