Skip to content

Instantly share code, notes, and snippets.

@capm
Last active May 4, 2022 07:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capm/bf2de99024e4670806456e00ad961413 to your computer and use it in GitHub Desktop.
Save capm/bf2de99024e4670806456e00ad961413 to your computer and use it in GitHub Desktop.
Install instructions for the Bloomberg API with Anaconda Jupyter

Bloomberg API with Python

Requirements

  • Windows 10 x64.
  • Latest version of Anaconda.

Setting up the environment

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.

VSBuildTools

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

Bloomberg API

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

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.

Some examples

xbbg

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

And the result:
US PE Yield Curve

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)

pdblp

@capm
Copy link
Author

capm commented Sep 20, 2019

YC_US_PEN

@nikanez
Copy link

nikanez commented Apr 29, 2020

Hi, i get the following message:

TypeError Traceback (most recent call last)
in
----> 1 blp.bdp(["YCGT0025 Index", "YCGT0361 Index"], "NAME")

~\Anaconda3\lib\site-packages\xbbg\blp.py in bdp(tickers, flds, **kwargs)
45 .set_index(['ticker', 'field'])
46 .unstack(level=1)
---> 47 .rename_axis(index=None, columns=[None, None])
48 .droplevel(axis=1, level=0)
49 .loc[:, cols]

TypeError: rename_axis() got an unexpected keyword argument 'index'

my code is as yours:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
#%matplotlib notebook
import pandas_datareader.data as web
import datetime
from xbbg import blp

blp.bdp(["YCGT0025 Index", "YCGT0361 Index"], "NAME")

can you please help?

@capm
Copy link
Author

capm commented Apr 30, 2020

Hi, i get the following message:

TypeError Traceback (most recent call last)
in
----> 1 blp.bdp(["YCGT0025 Index", "YCGT0361 Index"], "NAME")

~\Anaconda3\lib\site-packages\xbbg\blp.py in bdp(tickers, flds, **kwargs)
45 .set_index(['ticker', 'field'])
46 .unstack(level=1)
---> 47 .rename_axis(index=None, columns=[None, None])
48 .droplevel(axis=1, level=0)
49 .loc[:, cols]

TypeError: rename_axis() got an unexpected keyword argument 'index'

my code is as yours:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
#%matplotlib notebook
import pandas_datareader.data as web
import datetime
from xbbg import blp

blp.bdp(["YCGT0025 Index", "YCGT0361 Index"], "NAME")

can you please help?

Try blp.bdp(["YCGT0025 Index", "YCGT0361 Index"], ["NAME"]).

@nikanez
Copy link

nikanez commented May 13, 2020

Tried that

blp.bdp(["YCGT0025 Index", "YCGT0361 Index"], ["NAME"])

and i still get the exact same error


TypeError Traceback (most recent call last)
in
----> 1 blp.bdp(["YCGT0025 Index", "YCGT0361 Index"], ["NAME"])

~\Anaconda3\lib\site-packages\xbbg\blp.py in bdp(tickers, flds, **kwargs)
45 .set_index(['ticker', 'field'])
46 .unstack(level=1)
---> 47 .rename_axis(index=None, columns=[None, None])
48 .droplevel(axis=1, level=0)
49 .loc[:, cols]

TypeError: rename_axis() got an unexpected keyword argument 'index'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment