How get BQL (bLOOMBERG) query from python
Asked Answered
S

3

5

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.

Shute answered 3/6, 2021 at 19:2 Comment(2)
This is what you need: pypi.org/project/pdblpTokyo
That is a wrapper for the blpapi. It doesn't give you the same abilities as BQNT. It is more for the =BDP and =BDH Excel equivalents.Samualsamuel
B
6

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

Bouse answered 13/8, 2022 at 1:43 Comment(3)
Does this still work for you in January 2023? I'm getting a 'no module named 'bqmonitor'' error when i follow the steps exactly as you say. Are there other folders to copy after their latest updates? I copied them to %userprofile%\Anaconda3\lib\site-packages\ %userprofile%\Anaconda3\envs{mypythonenv}\lib\site-packages\ as envs was empty.Hooey
Just copy this module as well and others that will be requiredDamato
Yes, this is still working at Sept 2023. There have been updates, so there's quite a few more packages to copy over. If you don't have environments, or just want to install it for all environments, just copy to %userprofile%\Anaconda3\lib\site-packages\Bouse
I
4

You can try the blp package from Matthew Gilbert. Currently, it's accessible exclusively through GitHub:

> pip install git+https://github.com/matthewgilbert/blp.git
from blp import blp

bquery = blp.BlpQuery().start()

bquery.bql(expression="get(name) for(holdings('INEMCBI LX Equity'))")

This is an undocumented endpoint, so we haven't yet fully grasped its behavior. If anyone comes across any bugs, please feel free to open a GitHub issue.

TIP: For an easy way to create this expression, try Excel's BQL Builder in Advanced View. After previewing the query, you can just "Copy BQL Query String Only."

Innervate answered 28/8, 2023 at 19:31 Comment(0)
A
3

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
Altman answered 15/6, 2021 at 15:23 Comment(1)
Thanks @Altman ! I thought I would add a little to this that may help others. Since BQNT is sandboxed, you cannot save to the usual local drive places. However, I did find that you can save to the sandboxed project folder, for me it was 'C:/Users/my_user/AppData/Local/bipy/.....'. BQNT has pathlib in the environment, so just run pathlib.Path.cwd() and you will find the folder that you can write to. I wanted to pickle something to use in my own app.Samualsamuel

© 2022 - 2024 — McMap. All rights reserved.