How get BQL (bLOOMBERG) query from python

Viewed 2910

I have the following query in Excel using BQL:

=BQL("MEMBERS('INEMCBI LX Equity',type=holdings)";"name";"cols=2;rows=223")

This shows a table of securities of a specific mutual fund. Like this:

enter image description here

I want to get the same information using python in Dataframe structure but I dont know any way to get it.

2 Answers

If you run BQNT<GO> on the terminal, you'll get access to Python BQL examples, but the examples won't work outside of BQNT. Since you mention using =BQL, I'm assuming you have access to a terminal.

Example in BQNT:

import bql
bq = bql.Service()
fund = bq.univ.members("INEMCBI LX Equity", type="HOLDINGS")
name = bq.data.name()
req = bql.Request(fund, {"Fund Holdings": name})
res = bq.execute(req)
data = res[0].df()
data

Run BQNT on your Bloomberg terminal to ensure the BQL environment is installed.

Follow the steps exactly as followed.

Open file explorer

  • Navigate to C:\blp\bqnt\environments\bqnt-3\Lib\site-packages and copy these folders:

  • bqapi

  • bqbreg

  • bql

  • bqlmetadata

  • bqrequest

  • bqutil

  • ciso8601

  1. Paste them to your python installation folder %userprofile%\Anaconda3\envs{mypythonenv}\lib\site-packages\

Then you can test this code in your code editor. I use Vscode.

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt
import bql

bq = bql.Service()
query = """get(px_last)for('AAPL US EQUITY')with(dates=range(-1y,0d),fill='prev')"""
data = bql.combined_df(bq.execute(query)).reset_index()

fig = plt.figure(figsize=(12,8))
sb.lineplot(data=data, x='DATE',y='px_last')
plt.show()

Output

Related