From 3985d9557ae4fa7f02c519fae76526277ec55b4e Mon Sep 17 00:00:00 2001 From: Avantgarde Asset Management <120227333+avantgardeam@users.noreply.github.com> Date: Wed, 22 Nov 2023 23:38:18 -0300 Subject: [PATCH] Add `.ipynb` Documentation (#35) * add .ipynb docs * correct .ipynb title --- doc/source/quickstart.ipynb | 2582 +++++++++++++++++++++++++++++++++++ 1 file changed, 2582 insertions(+) create mode 100644 doc/source/quickstart.ipynb diff --git a/doc/source/quickstart.ipynb b/doc/source/quickstart.ipynb new file mode 100644 index 0000000..8c07aca --- /dev/null +++ b/doc/source/quickstart.ipynb @@ -0,0 +1,2582 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Quickstart Guide\n", + "\n", + "We highly advise thoroughly reviewing the [BLPAPI Core Developer Guide](https://data.bloomberglp.com/professional/sites/10/2017/03/BLPAPI-Core-Developer-Guide.pdf) when developing your Bloomberg Queries. This guide is essential for understanding function parameters, request options, overrides, error messages, and troubleshooting methods." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import datetime\n", + "import json\n", + "\n", + "import pandas\n", + "\n", + "from blp import blp" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "bquery = blp.BlpQuery().start()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bloomberg Queries\n", + "\n", + "## Data History (BDH)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
datesecurityPX_LASTVOLUME
02019-01-02SPY US Equity250.18126925199.0
12019-01-02TLT US Equity122.1519841527.0
22019-01-03SPY US Equity244.21144140692.0
32019-01-03TLT US Equity123.5421187045.0
42019-01-04SPY US Equity252.39142628834.0
52019-01-04TLT US Equity122.1112970226.0
62019-01-07SPY US Equity254.38103139100.0
72019-01-07TLT US Equity121.758498104.0
82019-01-08SPY US Equity256.77102512587.0
92019-01-08TLT US Equity121.437737103.0
102019-01-09SPY US Equity257.9795006554.0
112019-01-09TLT US Equity121.249349245.0
122019-01-10SPY US Equity258.8896823923.0
132019-01-10TLT US Equity120.468222860.0
\n", + "
" + ], + "text/plain": [ + " date security PX_LAST VOLUME\n", + "0 2019-01-02 SPY US Equity 250.18 126925199.0\n", + "1 2019-01-02 TLT US Equity 122.15 19841527.0\n", + "2 2019-01-03 SPY US Equity 244.21 144140692.0\n", + "3 2019-01-03 TLT US Equity 123.54 21187045.0\n", + "4 2019-01-04 SPY US Equity 252.39 142628834.0\n", + "5 2019-01-04 TLT US Equity 122.11 12970226.0\n", + "6 2019-01-07 SPY US Equity 254.38 103139100.0\n", + "7 2019-01-07 TLT US Equity 121.75 8498104.0\n", + "8 2019-01-08 SPY US Equity 256.77 102512587.0\n", + "9 2019-01-08 TLT US Equity 121.43 7737103.0\n", + "10 2019-01-09 SPY US Equity 257.97 95006554.0\n", + "11 2019-01-09 TLT US Equity 121.24 9349245.0\n", + "12 2019-01-10 SPY US Equity 258.88 96823923.0\n", + "13 2019-01-10 TLT US Equity 120.46 8222860.0" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery.bdh(\n", + " [\"SPY US Equity\", \"TLT US Equity\"],\n", + " [\"PX_LAST\", \"VOLUME\"],\n", + " start_date=\"20190101\",\n", + " end_date=\"20190110\",\n", + " options={\"adjustmentSplit\": True}\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Data Point (BDP)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
securityNAMEPX_LAST
0CL1 ComdtyGeneric 1st 'CL' Future77.43
\n", + "
" + ], + "text/plain": [ + " security NAME PX_LAST\n", + "0 CL1 Comdty Generic 1st 'CL' Future 77.43" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery.bdp([\"CL1 Comdty\"], [\"NAME\", \"PX_LAST\"])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Data Set (BDS)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Member Ticker and Exchange CodePercentage Weight
0BOF42.80
1C H44.24
2CLF48.44
3COH47.81
4CTH41.53
5GCG416.41
6HGH45.30
7HOF42.07
8KCH43.19
9KWH41.35
10LAF244.12
11LCG43.86
12LHG41.68
13LLF41.00
14LNF41.73
15LXF42.54
16NGF247.37
17QSF42.71
18S F45.47
19SBH43.84
20SIH44.74
21SMF43.38
22W H42.21
23XBF42.21
\n", + "
" + ], + "text/plain": [ + " Member Ticker and Exchange Code Percentage Weight\n", + "0 BOF4 2.80\n", + "1 C H4 4.24\n", + "2 CLF4 8.44\n", + "3 COH4 7.81\n", + "4 CTH4 1.53\n", + "5 GCG4 16.41\n", + "6 HGH4 5.30\n", + "7 HOF4 2.07\n", + "8 KCH4 3.19\n", + "9 KWH4 1.35\n", + "10 LAF24 4.12\n", + "11 LCG4 3.86\n", + "12 LHG4 1.68\n", + "13 LLF4 1.00\n", + "14 LNF4 1.73\n", + "15 LXF4 2.54\n", + "16 NGF24 7.37\n", + "17 QSF4 2.71\n", + "18 S F4 5.47\n", + "19 SBH4 3.84\n", + "20 SIH4 4.74\n", + "21 SMF4 3.38\n", + "22 W H4 2.21\n", + "23 XBF4 2.21" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery.bds(\"BCOM Index\", \"INDX_MWEIGHT\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Intraday Bar (BDIB)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
timeopenhighlowclosevolumenumEventsvalue
02023-11-01 13:30:00171.000171.97170.120171.8288272888414651.415716e+09
12023-11-01 14:30:00171.835172.08171.000171.4305189464262418.903882e+08
22023-11-01 15:30:00171.440172.00171.195171.9102887219145054.957753e+08
32023-11-01 16:30:00171.905172.38171.630172.1623109733143085.350173e+08
42023-11-01 17:30:00172.160172.71171.910172.6204089553205617.043825e+08
52023-11-01 18:30:00172.620174.22172.100173.9108659156462551.499576e+09
62023-11-01 19:30:00173.920174.23173.610173.97013746588329672.391479e+09
\n", + "
" + ], + "text/plain": [ + " time open high low close volume numEvents \\\n", + "0 2023-11-01 13:30:00 171.000 171.97 170.120 171.828 8272888 41465 \n", + "1 2023-11-01 14:30:00 171.835 172.08 171.000 171.430 5189464 26241 \n", + "2 2023-11-01 15:30:00 171.440 172.00 171.195 171.910 2887219 14505 \n", + "3 2023-11-01 16:30:00 171.905 172.38 171.630 172.162 3109733 14308 \n", + "4 2023-11-01 17:30:00 172.160 172.71 171.910 172.620 4089553 20561 \n", + "5 2023-11-01 18:30:00 172.620 174.22 172.100 173.910 8659156 46255 \n", + "6 2023-11-01 19:30:00 173.920 174.23 173.610 173.970 13746588 32967 \n", + "\n", + " value \n", + "0 1.415716e+09 \n", + "1 8.903882e+08 \n", + "2 4.957753e+08 \n", + "3 5.350173e+08 \n", + "4 7.043825e+08 \n", + "5 1.499576e+09 \n", + "6 2.391479e+09 " + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery.bdib(\n", + " \"AAPL US Equity\",\n", + " event_type=\"TRADE\",\n", + " interval=60,\n", + " start_datetime=\"2023-11-01\", # Different date format\n", + " end_datetime=\"2023-11-02\", # Different date format\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Equity Screening (BEQS)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
securityPrice:D-1Market CapS&P LT FC Iss Cred RtgTickerMoody's Issuer RtgTier 1 Cap Rt LFTot Rsk Bsd Cap Rt LFShort Name
08331 JP7.4514696.089966e+09A-8331 JPA111.51000011.580000CHIBA BANK LTD
1ABN NA13.9568881.201920e+10AABN NAA116.50000019.799999ABN AMRO BANK-CV
2ACA FP12.9112153.946861e+10A+ACA FPAa313.70000017.900000CREDIT AGRICOLE
3BAC US30.0100002.372537e+11A-BAC USA113.60000015.400000BANK OF AMERICA
4BBVA SM9.0632275.414058e+10ABBVA SMA314.45000016.510000BBVA
5BNP FP62.1278387.159116e+10A+BNP FPAa315.50000017.799999BNP PARIBAS
6BNS CN44.4306875.364239e+10A+BNS CNA214.60000016.900000BANK OF NOVA SCO
7CBK GR12.2997811.511882e+10A-CBK GRA216.50000019.200001COMMERZBANK
8CFR US99.3700036.354122e+09A-CFR USA313.81000015.280000CULLEN/FROST
9DANSKE DC26.0308932.243711e+10A+DANSKE DCA320.60000023.200001DANSKE BANK A/S
10DBK GR12.3249382.518035e+10A-DBK GRA116.29999918.799999DEUTSCHE BANK-RG
11JPM US153.2899934.418039e+11A-JPM USA115.90000017.799999JPMORGAN CHASE
12NA CN66.2781372.250676e+10ANA CNA316.10000016.900000NATL BK CANADA
13NAB AU18.2913575.732886e+10AA-NAB AUAa314.19000019.879999NATL AUST BANK
14PNC US130.3399965.188792e+10A-PNC USA311.50000013.400000PNC FINANCIAL SE
15RY CN87.8050541.230937e+11AA-RY CNA115.40000017.299999ROYAL BANK OF CA
16SAN SM4.0842496.619073e+10A+SAN SMA213.80000016.299999BANCO SANTANDER
17SEBA SS12.0843612.589788e+10A+SEBA SSAa320.60000021.900000SEB AB-A
18SHBA SS9.4590721.885026e+10AA-SHBA SSAa221.20000124.500000SVENSKA HAN-A
19SWEDA SS17.6389602.001985e+10A+SWEDA SSAa320.50000023.000000SWEDBANK AB-A
20TD CN61.9946101.112967e+11AA-TD CNA117.20000119.600000TORONTO-DOM BANK
21TFC US32.0400014.263737e+10A-TFC USA311.40000013.500000TRUIST FINANCIAL
22USB US37.6300015.860595e+10AUSB USA311.20000013.400000US BANCORP
23WBC AU13.8969774.891449e+10AA-WBC AUAa314.59000020.450001WESTPAC BANKING
\n", + "
" + ], + "text/plain": [ + " security Price:D-1 Market Cap S&P LT FC Iss Cred Rtg Ticker \\\n", + "0 8331 JP 7.451469 6.089966e+09 A- 8331 JP \n", + "1 ABN NA 13.956888 1.201920e+10 A ABN NA \n", + "2 ACA FP 12.911215 3.946861e+10 A+ ACA FP \n", + "3 BAC US 30.010000 2.372537e+11 A- BAC US \n", + "4 BBVA SM 9.063227 5.414058e+10 A BBVA SM \n", + "5 BNP FP 62.127838 7.159116e+10 A+ BNP FP \n", + "6 BNS CN 44.430687 5.364239e+10 A+ BNS CN \n", + "7 CBK GR 12.299781 1.511882e+10 A- CBK GR \n", + "8 CFR US 99.370003 6.354122e+09 A- CFR US \n", + "9 DANSKE DC 26.030893 2.243711e+10 A+ DANSKE DC \n", + "10 DBK GR 12.324938 2.518035e+10 A- DBK GR \n", + "11 JPM US 153.289993 4.418039e+11 A- JPM US \n", + "12 NA CN 66.278137 2.250676e+10 A NA CN \n", + "13 NAB AU 18.291357 5.732886e+10 AA- NAB AU \n", + "14 PNC US 130.339996 5.188792e+10 A- PNC US \n", + "15 RY CN 87.805054 1.230937e+11 AA- RY CN \n", + "16 SAN SM 4.084249 6.619073e+10 A+ SAN SM \n", + "17 SEBA SS 12.084361 2.589788e+10 A+ SEBA SS \n", + "18 SHBA SS 9.459072 1.885026e+10 AA- SHBA SS \n", + "19 SWEDA SS 17.638960 2.001985e+10 A+ SWEDA SS \n", + "20 TD CN 61.994610 1.112967e+11 AA- TD CN \n", + "21 TFC US 32.040001 4.263737e+10 A- TFC US \n", + "22 USB US 37.630001 5.860595e+10 A USB US \n", + "23 WBC AU 13.896977 4.891449e+10 AA- WBC AU \n", + "\n", + " Moody's Issuer Rtg Tier 1 Cap Rt LF Tot Rsk Bsd Cap Rt LF \\\n", + "0 A1 11.510000 11.580000 \n", + "1 A1 16.500000 19.799999 \n", + "2 Aa3 13.700000 17.900000 \n", + "3 A1 13.600000 15.400000 \n", + "4 A3 14.450000 16.510000 \n", + "5 Aa3 15.500000 17.799999 \n", + "6 A2 14.600000 16.900000 \n", + "7 A2 16.500000 19.200001 \n", + "8 A3 13.810000 15.280000 \n", + "9 A3 20.600000 23.200001 \n", + "10 A1 16.299999 18.799999 \n", + "11 A1 15.900000 17.799999 \n", + "12 A3 16.100000 16.900000 \n", + "13 Aa3 14.190000 19.879999 \n", + "14 A3 11.500000 13.400000 \n", + "15 A1 15.400000 17.299999 \n", + "16 A2 13.800000 16.299999 \n", + "17 Aa3 20.600000 21.900000 \n", + "18 Aa2 21.200001 24.500000 \n", + "19 Aa3 20.500000 23.000000 \n", + "20 A1 17.200001 19.600000 \n", + "21 A3 11.400000 13.500000 \n", + "22 A3 11.200000 13.400000 \n", + "23 Aa3 14.590000 20.450001 \n", + "\n", + " Short Name \n", + "0 CHIBA BANK LTD \n", + "1 ABN AMRO BANK-CV \n", + "2 CREDIT AGRICOLE \n", + "3 BANK OF AMERICA \n", + "4 BBVA \n", + "5 BNP PARIBAS \n", + "6 BANK OF NOVA SCO \n", + "7 COMMERZBANK \n", + "8 CULLEN/FROST \n", + "9 DANSKE BANK A/S \n", + "10 DEUTSCHE BANK-RG \n", + "11 JPMORGAN CHASE \n", + "12 NATL BK CANADA \n", + "13 NATL AUST BANK \n", + "14 PNC FINANCIAL SE \n", + "15 ROYAL BANK OF CA \n", + "16 BANCO SANTANDER \n", + "17 SEB AB-A \n", + "18 SVENSKA HAN-A \n", + "19 SWEDBANK AB-A \n", + "20 TORONTO-DOM BANK \n", + "21 TRUIST FINANCIAL \n", + "22 US BANCORP \n", + "23 WESTPAC BANKING " + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery.beqs(\n", + " \"Core Capital Ratios\",\n", + " screen_type=\"GLOBAL\",\n", + " options={\"Group\": \"General\"}\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## BQL Queries\n", + "\n", + "`bquery.bql` accepts a Bloomberg Query String. For optimal query creation, utilize Excel's BQL Builder in Advanced View. Once your query is ready, click the \"Copy\" button to copy it, and then paste it into the `bquery.bql` argument." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
securityfieldsecondary_namesecondary_valuevalue
0IBM US Equitypx_lastCURRENCYUSD154.35
1AAPL US Equitypx_lastCURRENCYUSD191.45
2IBM US Equitypx_lastDATE2023-11-21T00:00:00Z154.35
3AAPL US Equitypx_lastDATE2023-11-21T00:00:00Z191.45
\n", + "
" + ], + "text/plain": [ + " security field secondary_name secondary_value value\n", + "0 IBM US Equity px_last CURRENCY USD 154.35\n", + "1 AAPL US Equity px_last CURRENCY USD 191.45\n", + "2 IBM US Equity px_last DATE 2023-11-21T00:00:00Z 154.35\n", + "3 AAPL US Equity px_last DATE 2023-11-21T00:00:00Z 191.45" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery.bql(\"get(px_last) for(['IBM US Equity', 'AAPL US Equity'])\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Advanced Query Techniques\n", + "\n", + "## Using Overrides\n", + "\n", + "Various fields can be overriden. For info on what fields support what overrides, the best place to check is using `FLDS` from the terminal." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
defaultoverride
securityCRNCY_ADJ_PX_LASTsecurityCRNCY_ADJ_PX_LAST
0IBM US Equity154.35IBM US Equity140.7789
\n", + "
" + ], + "text/plain": [ + " default override \n", + " security CRNCY_ADJ_PX_LAST security CRNCY_ADJ_PX_LAST\n", + "0 IBM US Equity 154.35 IBM US Equity 140.7789" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pandas.concat(\n", + " [\n", + " bquery.bdp([\"IBM US Equity\"], [\"CRNCY_ADJ_PX_LAST\"]),\n", + " bquery.bdp(\n", + " [\"IBM US Equity\"], [\"CRNCY_ADJ_PX_LAST\"], overrides=[(\"EQY_FUND_CRNCY\", \"EUR\")]\n", + " )\n", + " ],\n", + " axis=1,\n", + " keys=[\"default\", \"override\"]\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Index MemberPercent Weight
01COV GY-2.424536e-14
1ADS GY-2.424536e-14
2AIR GY-2.424536e-14
3ALV GY-2.424536e-14
4BAS GY-2.424536e-14
5BAYN GY-2.424536e-14
6BEI GY-2.424536e-14
7BMW GY-2.424536e-14
8BNR GY-2.424536e-14
9CBK GY-2.424536e-14
10CON GY-2.424536e-14
11DB1 GY-2.424536e-14
12DBK GY-2.424536e-14
13DHL GY-2.424536e-14
14DTE GY-2.424536e-14
15DTG GY-2.424536e-14
16ENR GY-2.424536e-14
17EOAN GY-2.424536e-14
18FRE GY-2.424536e-14
19HEI GY-2.424536e-14
20HEN3 GY-2.424536e-14
21HNR1 GY-2.424536e-14
22IFX GY-2.424536e-14
23MBG GY-2.424536e-14
24MRK GY-2.424536e-14
25MTX GY-2.424536e-14
26MUV2 GY-2.424536e-14
27P911 GY-2.424536e-14
28PAH3 GY-2.424536e-14
29QIA GY-2.424536e-14
30RHM GY-2.424536e-14
31RWE GY-2.424536e-14
32SAP GY-2.424536e-14
33SHL GY-2.424536e-14
34SIE GY-2.424536e-14
35SRT3 GY-2.424536e-14
36SY1 GY-2.424536e-14
37VNA GY-2.424536e-14
38VOW3 GY-2.424536e-14
39ZAL GY-2.424536e-14
\n", + "
" + ], + "text/plain": [ + " Index Member Percent Weight\n", + "0 1COV GY -2.424536e-14\n", + "1 ADS GY -2.424536e-14\n", + "2 AIR GY -2.424536e-14\n", + "3 ALV GY -2.424536e-14\n", + "4 BAS GY -2.424536e-14\n", + "5 BAYN GY -2.424536e-14\n", + "6 BEI GY -2.424536e-14\n", + "7 BMW GY -2.424536e-14\n", + "8 BNR GY -2.424536e-14\n", + "9 CBK GY -2.424536e-14\n", + "10 CON GY -2.424536e-14\n", + "11 DB1 GY -2.424536e-14\n", + "12 DBK GY -2.424536e-14\n", + "13 DHL GY -2.424536e-14\n", + "14 DTE GY -2.424536e-14\n", + "15 DTG GY -2.424536e-14\n", + "16 ENR GY -2.424536e-14\n", + "17 EOAN GY -2.424536e-14\n", + "18 FRE GY -2.424536e-14\n", + "19 HEI GY -2.424536e-14\n", + "20 HEN3 GY -2.424536e-14\n", + "21 HNR1 GY -2.424536e-14\n", + "22 IFX GY -2.424536e-14\n", + "23 MBG GY -2.424536e-14\n", + "24 MRK GY -2.424536e-14\n", + "25 MTX GY -2.424536e-14\n", + "26 MUV2 GY -2.424536e-14\n", + "27 P911 GY -2.424536e-14\n", + "28 PAH3 GY -2.424536e-14\n", + "29 QIA GY -2.424536e-14\n", + "30 RHM GY -2.424536e-14\n", + "31 RWE GY -2.424536e-14\n", + "32 SAP GY -2.424536e-14\n", + "33 SHL GY -2.424536e-14\n", + "34 SIE GY -2.424536e-14\n", + "35 SRT3 GY -2.424536e-14\n", + "36 SY1 GY -2.424536e-14\n", + "37 VNA GY -2.424536e-14\n", + "38 VOW3 GY -2.424536e-14\n", + "39 ZAL GY -2.424536e-14" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery.bds(\n", + " \"DAX Index\",\n", + " \"INDX_MWEIGHT_HIST\",\n", + " overrides=[(\"END_DATE_OVERRIDE\", \"20230630\")],\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Using SEDOLs" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
securityID_SEDOL1
0SPY US Equity2840215
1TLT US Equity2971546
\n", + "
" + ], + "text/plain": [ + " security ID_SEDOL1\n", + "0 SPY US Equity 2840215\n", + "1 TLT US Equity 2971546" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery.bdp(\n", + " [\"SPY US Equity\", \"TLT US Equity\"],\n", + " [\"ID_SEDOL1\"],\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
datesecurityPX_LASTVOLUME
02019-01-02/sedol1/2840215231.4905126925199.0
12019-01-02/sedol1/2971546110.048319841527.0
22019-01-03/sedol1/2840215225.9664144140692.0
32019-01-03/sedol1/2971546111.300621187045.0
42019-01-04/sedol1/2840215233.5354142628834.0
52019-01-04/sedol1/2971546110.012312970226.0
62019-01-07/sedol1/2840215235.3767103139100.0
72019-01-07/sedol1/2971546109.68798498104.0
82019-01-08/sedol1/2840215237.5882102512587.0
92019-01-08/sedol1/2971546109.39967737103.0
102019-01-09/sedol1/2840215238.698595006554.0
112019-01-09/sedol1/2971546109.22859349245.0
122019-01-10/sedol1/2840215239.540596823923.0
132019-01-10/sedol1/2971546108.52578222860.0
\n", + "
" + ], + "text/plain": [ + " date security PX_LAST VOLUME\n", + "0 2019-01-02 /sedol1/2840215 231.4905 126925199.0\n", + "1 2019-01-02 /sedol1/2971546 110.0483 19841527.0\n", + "2 2019-01-03 /sedol1/2840215 225.9664 144140692.0\n", + "3 2019-01-03 /sedol1/2971546 111.3006 21187045.0\n", + "4 2019-01-04 /sedol1/2840215 233.5354 142628834.0\n", + "5 2019-01-04 /sedol1/2971546 110.0123 12970226.0\n", + "6 2019-01-07 /sedol1/2840215 235.3767 103139100.0\n", + "7 2019-01-07 /sedol1/2971546 109.6879 8498104.0\n", + "8 2019-01-08 /sedol1/2840215 237.5882 102512587.0\n", + "9 2019-01-08 /sedol1/2971546 109.3996 7737103.0\n", + "10 2019-01-09 /sedol1/2840215 238.6985 95006554.0\n", + "11 2019-01-09 /sedol1/2971546 109.2285 9349245.0\n", + "12 2019-01-10 /sedol1/2840215 239.5405 96823923.0\n", + "13 2019-01-10 /sedol1/2971546 108.5257 8222860.0" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery.bdh(\n", + " [\"/sedol1/2840215\", \"/sedol1/2971546\"],\n", + " [\"PX_LAST\", \"VOLUME\"],\n", + " start_date=\"20190101\",\n", + " end_date=\"20190110\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Troubleshooting\n", + "\n", + "The most frequent errors in `blpapi` are due to poor input. As `blp` is a wrapper for `blpapi`, these errors are passed to the user. To resolve them, examine the error message for issues, then contact the Bloomberg help desk.\n", + "\n", + "For instance, the code below will produce an error because it includes an invalid ticker.\n", + "\n", + "```python\n", + "bquery.bdh(\n", + " [\"NOT_A_TICKER US Equity\", \"SPY US Equity\"],\n", + " [\"PX_LAST\", \"VOLUME\"],\n", + " start_date=\"20190101\",\n", + " end_date=\"20190110\",\n", + ")\n", + "```\n", + "```\n", + "TypeError: Response for 'NOT_A_TICKER US Equity' contains securityError\n", + "{\n", + " \"securityError\": {\n", + " \"source\": \"3923::bbdbh4\",\n", + " \"code\": 15,\n", + " \"category\": \"BAD_SEC\",\n", + " \"message\": \"Unknown/Invalid securityInvalid Security [nid:3923] \",\n", + " \"subcategory\": \"INVALID_SECURITY\",\n", + " }\n", + "}\n", + "```\n", + "\n", + "To ignore errors, instantiate a parser as shown below:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Response for 'NOT_A_TICKER US Equity' contains securityError {'securityError': {'source': '53646:rsfhdsvc2', 'code': 15, 'category': 'BAD_SEC', 'message': 'Unknown/Invalid securityInvalid Security [nid:53646]', 'subcategory': 'INVALID_SECURITY'}}\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
datesecurityPX_LASTVOLUME
02019-01-02SPY US Equity231.4905126925199.0
12019-01-03SPY US Equity225.9664144140692.0
22019-01-04SPY US Equity233.5354142628834.0
32019-01-07SPY US Equity235.3767103139100.0
42019-01-08SPY US Equity237.5882102512587.0
52019-01-09SPY US Equity238.698595006554.0
62019-01-10SPY US Equity239.540596823923.0
\n", + "
" + ], + "text/plain": [ + " date security PX_LAST VOLUME\n", + "0 2019-01-02 SPY US Equity 231.4905 126925199.0\n", + "1 2019-01-03 SPY US Equity 225.9664 144140692.0\n", + "2 2019-01-04 SPY US Equity 233.5354 142628834.0\n", + "3 2019-01-07 SPY US Equity 235.3767 103139100.0\n", + "4 2019-01-08 SPY US Equity 237.5882 102512587.0\n", + "5 2019-01-09 SPY US Equity 238.6985 95006554.0\n", + "6 2019-01-10 SPY US Equity 239.5405 96823923.0" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bquery = blp.BlpQuery(parser=blp.BlpParser(raise_security_errors=False)).start()\n", + "\n", + "bquery.bdh(\n", + " [\"NOT_A_TICKER US Equity\", \"SPY US Equity\"],\n", + " [\"PX_LAST\", \"VOLUME\"],\n", + " start_date=\"20190101\",\n", + " end_date=\"20190110\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Additional Features\n", + "\n", + "## Retrieve JSON Data\n", + "\n", + "### Query Generation Utility Methods" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"HistoricalDataRequest\": {\n", + " \"securities\": [\n", + " \"SPY US Equity\"\n", + " ],\n", + " \"fields\": [\n", + " \"VOLUME\"\n", + " ],\n", + " \"startDate\": \"20190101\",\n", + " \"endDate\": \"20190105\"\n", + " }\n", + "}\n" + ] + } + ], + "source": [ + "query = blp.create_query(\n", + " request_type=\"HistoricalDataRequest\",\n", + " values={\n", + " \"securities\": [\"SPY US Equity\"],\n", + " \"fields\": [\"VOLUME\"],\n", + " \"startDate\": \"20190101\",\n", + " \"endDate\": \"20190105\",\n", + " },\n", + ")\n", + "print(json.dumps(query, indent=2))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "# deal with Timestamps for pretty printing for response\n", + "\n", + "def ts_to_json(obj):\n", + " return obj.strftime(\"%Y-%m-%dT%H:%M:%S.%f%z\")\n", + "\n", + "\n", + "def time_to_json(obj):\n", + " return obj.strftime(\"%H:%M:%S\")\n", + "\n", + "class CustomJSONEncoder(json.JSONEncoder):\n", + " def default(self, obj):\n", + " if isinstance(obj, pandas.Timestamp):\n", + " return ts_to_json(obj)\n", + " if isinstance(obj, datetime.time):\n", + " return time_to_json(obj)\n", + " return super().default(obj)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[\n", + " {\n", + " \"eventType\": 5,\n", + " \"eventTypeName\": \"blpapi.Event.RESPONSE\",\n", + " \"messageNumber\": 0,\n", + " \"message\": {\n", + " \"fragmentType\": 0,\n", + " \"correlationIds\": [\n", + " 33\n", + " ],\n", + " \"messageType\": \"HistoricalDataResponse\",\n", + " \"timeReceived\": \"2023-11-21T04:17:39.755538+0000\",\n", + " \"element\": {\n", + " \"HistoricalDataResponse\": {\n", + " \"securityData\": {\n", + " \"security\": \"SPY US Equity\",\n", + " \"eidData\": [],\n", + " \"sequenceNumber\": 0,\n", + " \"fieldExceptions\": [],\n", + " \"fieldData\": [\n", + " {\n", + " \"fieldData\": {\n", + " \"date\": \"2019-01-02T00:00:00.000000\",\n", + " \"VOLUME\": 126925199.0\n", + " }\n", + " },\n", + " {\n", + " \"fieldData\": {\n", + " \"date\": \"2019-01-03T00:00:00.000000\",\n", + " \"VOLUME\": 144140692.0\n", + " }\n", + " },\n", + " {\n", + " \"fieldData\": {\n", + " \"date\": \"2019-01-04T00:00:00.000000\",\n", + " \"VOLUME\": 142628834.0\n", + " }\n", + " }\n", + " ]\n", + " }\n", + " }\n", + " }\n", + " }\n", + " }\n", + "]\n" + ] + } + ], + "source": [ + "resp = bquery.query(query, parse=False, collector=list)\n", + "print(json.dumps(resp, indent=2, cls=CustomJSONEncoder))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Support for Context Manager" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
datesecurityPX_LASTVOLUME
02021-01-04GME US Equity4.312540089896.0
12021-01-05GME US Equity4.342519845828.0
22021-01-06GME US Equity4.590024224992.0
32021-01-07GME US Equity4.520024517104.0
42021-01-08GME US Equity4.422525927840.0
52021-01-11GME US Equity4.985059710448.0
62021-01-12GME US Equity4.987528242660.0
72021-01-13GME US Equity7.8500578006944.0
82021-01-14GME US Equity9.9775374869640.0
92021-01-15GME US Equity8.8750187465432.0
102021-01-19GME US Equity9.8400298887696.0
112021-01-20GME US Equity9.7800133887156.0
122021-01-21GME US Equity10.7575228319016.0
132021-01-22GME US Equity16.2525788631784.0
142021-01-25GME US Equity19.1975711496000.0
152021-01-26GME US Equity36.9950714351896.0
162021-01-27GME US Equity86.8775373586664.0
172021-01-28GME US Equity48.4000235266380.0
182021-01-29GME US Equity81.2500202264220.0
\n", + "
" + ], + "text/plain": [ + " date security PX_LAST VOLUME\n", + "0 2021-01-04 GME US Equity 4.3125 40089896.0\n", + "1 2021-01-05 GME US Equity 4.3425 19845828.0\n", + "2 2021-01-06 GME US Equity 4.5900 24224992.0\n", + "3 2021-01-07 GME US Equity 4.5200 24517104.0\n", + "4 2021-01-08 GME US Equity 4.4225 25927840.0\n", + "5 2021-01-11 GME US Equity 4.9850 59710448.0\n", + "6 2021-01-12 GME US Equity 4.9875 28242660.0\n", + "7 2021-01-13 GME US Equity 7.8500 578006944.0\n", + "8 2021-01-14 GME US Equity 9.9775 374869640.0\n", + "9 2021-01-15 GME US Equity 8.8750 187465432.0\n", + "10 2021-01-19 GME US Equity 9.8400 298887696.0\n", + "11 2021-01-20 GME US Equity 9.7800 133887156.0\n", + "12 2021-01-21 GME US Equity 10.7575 228319016.0\n", + "13 2021-01-22 GME US Equity 16.2525 788631784.0\n", + "14 2021-01-25 GME US Equity 19.1975 711496000.0\n", + "15 2021-01-26 GME US Equity 36.9950 714351896.0\n", + "16 2021-01-27 GME US Equity 86.8775 373586664.0\n", + "17 2021-01-28 GME US Equity 48.4000 235266380.0\n", + "18 2021-01-29 GME US Equity 81.2500 202264220.0" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "with blp.BlpQuery() as bq:\n", + " df = bq.bdh(\n", + " [\"GME US Equity\"],\n", + " [\"PX_LAST\", \"VOLUME\"],\n", + " start_date=\"20210101\",\n", + " end_date=\"20210130\",\n", + " )\n", + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Streaming Data" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "SESSION_STATUS - Message 0 - SessionConnectionDown = {\n", + " server = \"localhost:8194\"\n", + "}\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"fragmentType\": 0,\n", + " \"correlationIds\": [\n", + " \"USDCAD Curncy\"\n", + " ],\n", + " \"messageType\": \"MarketDataEvents\",\n", + " \"timeReceived\": \"2023-11-21T04:17:42.131746+0000\",\n", + " \"element\": {\n", + " \"MarketDataEvents\": {\n", + " \"MKTDATA_EVENT_TYPE\": \"SUMMARY\",\n", + " \"MKTDATA_EVENT_SUBTYPE\": \"INITPAINT\",\n", + " \"BID\": 1.3706,\n", + " \"ASK\": 1.3707,\n", + " \"BEST_BID\": 1.3706,\n", + " \"BEST_ASK\": 1.3707,\n", + " \"BID_ALL_SESSION\": 1.3706,\n", + " \"ASK_ALL_SESSION\": 1.3707,\n", + " \"IND_BID_FLAG\": false,\n", + " \"IND_ASK_FLAG\": false,\n", + " \"BID_TDY\": 1.3706,\n", + " \"PRICE_LAST_BID_RT\": 1.3706,\n", + " \"ASK_TDY\": 1.3707,\n", + " \"PRICE_LAST_ASK_RT\": 1.3707,\n", + " \"BID_DIR\": 1,\n", + " \"ASK_DIR\": 1,\n", + " \"BID2\": 1.3706,\n", + " \"ASK2\": 1.3707,\n", + " \"BID_ALL_SESSION_TDY_RT\": 1.3706,\n", + " \"ASK_ALL_SESSION_TDY_RT\": 1.3707,\n", + " \"CURRENT_SESSION_RT\": 2,\n", + " \"PX_OPEN_ALL_WITH_SWITCHOVER_RT\": 1.3724546432495117,\n", + " \"PX_CLOSE_ALL_WITH_SWITCHOVER_RT\": 1.3725,\n", + " \"LAST_PRICE_ALL_SESSION_DIR_RT\": 1,\n", + " \"SIMP_LAST_PX_ALL_SESS_DIR_RT\": 1,\n", + " \"ID_BB_SEC_NUM_SRC\": 12884922137,\n", + " \"LAST_PRICE\": 1.3707,\n", + " \"LAST2_TRADE\": 1.3707,\n", + " \"HIGH\": 1.373,\n", + " \"LOW\": 1.3705,\n", + " \"OPEN\": 1.3725,\n", + " \"OPEN_TDY\": 1.3725,\n", + " \"PRICE_OPEN_RT\": 1.3725,\n", + " \"LAST_PRICE_TDY\": 1.3707,\n", + " \"HIGH_TDY\": 1.373,\n", + " \"PRICE_HIGH_RT\": 1.373,\n", + " \"LOW_TDY\": 1.3705,\n", + " \"PRICE_LOW_RT\": 1.3705,\n", + " \"MID\": 1.3706,\n", + " \"MID_TDY\": 1.3706,\n", + " \"RT_EXCH_MARKET_STATUS\": \"ACTV\",\n", + " \"LAST_ALL_SESSIONS\": 1.3707,\n", + " \"PREV_CLOSE_VALUE_REALTIME\": 1.3725,\n", + " \"TRADING_DT_REALTIME\": \"2023-11-21T00:00:00.000000\",\n", + " \"PREV_TRADING_DT_REALTIME\": \"2023-11-20T00:00:00.000000\",\n", + " \"PRICE_CHANGE_1Y_NET_RT\": 0.02590000070631504,\n", + " \"PRICE_CHANGE_1Y_PCT_RT\": 1.9221999645233154,\n", + " \"CLOSING_PRICE_1Y_AGO_RT\": 1.3447999954223633,\n", + " \"ZERO_BID_PRICE_INDICATOR_RT\": true,\n", + " \"ZERO_ASK_PRICE_INDICATOR_RT\": true,\n", + " \"RT_API_MACHINE\": \"apipubx0@md1p-rr-004\",\n", + " \"PX_LOW_BID_RT\": 1.3704500198364258,\n", + " \"PX_HIGH_ASK_RT\": 1.3736000061035156,\n", + " \"PX_HIGH_BID_RT\": 1.372910499572754,\n", + " \"PX_LOW_ASK_RT\": 1.3705700635910034,\n", + " \"ALL_PRICE\": 1.3707,\n", + " \"MID2\": 1.3706,\n", + " \"MID_DIR\": 1,\n", + " \"RT_PRICING_SOURCE\": \"BGN\",\n", + " \"ASK_CHANGE\": -0.0019001174926758235,\n", + " \"BID_CHANGE\": -0.0017002532958985128,\n", + " \"SPREAD_BA\": 0.0001,\n", + " \"PRICE_52_WEEK_HIGH_RT\": 1.3899,\n", + " \"PRICE_52_WEEK_HIGH_DATE_RT\": \"2023-11-01T00:00:00.000000\",\n", + " \"PRICE_52_WEEK_LOW_RT\": 1.3093,\n", + " \"PRICE_52_WEEK_LOW_DATE_RT\": \"2023-07-14T00:00:00.000000\",\n", + " \"PRICE_PREVIOUS_CLOSE_RT\": 1.3725,\n", + " \"PRICE_LAST_RT\": 1.3707,\n", + " \"PREVIOUS_CLOSE_BID_PRICE_RT\": 1.3723,\n", + " \"PREVIOUS_CLOSE_ASK_PRICE_RT\": 1.3726,\n", + " \"LAST_CONTINUOUS_TRADE_PRICE_RT\": 1.3707,\n", + " \"LAST_TRADE_CANCELED_IND_RT\": false,\n", + " \"UNADJUSTED_PREV_LAST_PRICE_RT\": 1.3725,\n", + " \"ADJUSTED_PREV_LAST_PRICE_RT\": 1.3725,\n", + " \"BASE_PRICE_ENABLED_RT\": false,\n", + " \"BB_STANDARD_CC_AVAIL_RT\": false,\n", + " \"NUM_TRADES_RT\": 0,\n", + " \"BLOOMBERG_CLOSE_METHOD_RT\": \"NA\",\n", + " \"LAST_TICK_DIRECTION_RT\": 0,\n", + " \"CHG_NET_REG_SES_PRV_RG_SES_CL_RT\": -0.0018019676208496094,\n", + " \"CHG_PCT_REG_SES_PRV_RG_SES_CL_RT\": -0.13129546830859543,\n", + " \"ALT_BOOK_BEST_ASK_CC_RT\": null,\n", + " \"ALT_BOOK_NAME_RT\": null,\n", + " \"ALT_BOOK_BEST_BID_CC_RT\": null,\n", + " \"MIN_NEGOTIATED_TRADE_SIZE_RT\": null,\n", + " \"SETTLMNT_PX_ALTRNTVE_NOTTN_RT\": null,\n", + " \"ALT_BOOK_BEST_BID_NUM_OF_ORDR_RT\": null,\n", + " \"ALT_BOOK_BEST_ASK_NUM_OF_ORDR_RT\": null,\n", + " \"SETTLMNT_PX_ALT_NOTTN_METHOD_RT\": null,\n", + " \"SHORTENED_BID_ASK_RT\": \"1.3706/07\",\n", + " \"IS_DELAYED_STREAM\": false,\n", + " \"CHG_NET_MTD_RT\": -0.0168,\n", + " \"CHG_PCT_MTD_RT\": -1.2108,\n", + " \"CHG_NET_QTD_RT\": 0.013,\n", + " \"CHG_PCT_QTD_RT\": 0.9575,\n", + " \"CHG_NET_YTD_RT\": 0.0153,\n", + " \"CHG_PCT_YTD_RT\": 1.1288,\n", + " \"CHG_NET_1M_RT\": 0.0016,\n", + " \"CHG_PCT_1M_RT\": 0.1169,\n", + " \"CHG_NET_3M_RT\": 0.0163,\n", + " \"CHG_PCT_3M_RT\": 1.2035,\n", + " \"REALTIME_2_DAY_CHANGE_NET\": -0.0016,\n", + " \"REALTIME_2_DAY_CHANGE_PERCENT\": -0.1166,\n", + " \"REALTIME_5_DAY_CHANGE_NET\": 0.0014,\n", + " \"REALTIME_5_DAY_CHANGE_PERCENT\": 0.1022,\n", + " \"RT_PX_AS_PCT_INTRADAY_RANGE\": 8.0,\n", + " \"REALTIME_PERCENT_BID_ASK_SPREAD\": 0.00730000000000075,\n", + " \"CHG_NET_1D_ALL_FROM_REGULAR_RT\": -0.0018,\n", + " \"CHG_PCT_1D_ALL_FROM_REGULAR_RT\": -0.1311,\n", + " \"PX_CHG_NET_1D_ALL_SWITCHOVER_RT\": -0.0018,\n", + " \"PX_CHG_PCT_1D_ALL_SWITCHOVER_RT\": -0.1311,\n", + " \"API_RULES_VERSION\": 201411210,\n", + " \"PREV_SES_LAST_PRICE\": 1.3725,\n", + " \"RT_PX_CHG_NET_1D\": -0.0018,\n", + " \"RT_PX_CHG_PCT_1D\": -0.13109999895095825,\n", + " \"PRICE_CHANGE_ON_DAY_RT\": -0.0018,\n", + " \"NET_CHANGE_ON_DAY_TODAY_RT\": -0.0018,\n", + " \"PERCENT_CHANGE_ON_DAY_TODAY_RT\": -0.1311,\n", + " \"LAST_UPDATE_BID_RT\": \"01:17:40\",\n", + " \"LAST_UPDATE_ASK_RT\": \"01:17:40\",\n", + " \"TIME\": \"01:17:40\",\n", + " \"BID_ASK_TIME\": \"01:17:40\",\n", + " \"SES_START\": \"19:00:00\",\n", + " \"SES_END\": \"18:59:59\",\n", + " \"PRICE_LAST_TIME_RT\": \"01:17:40\",\n", + " \"LAST_BID_TIME_TODAY_REALTIME\": \"01:17:40\",\n", + " \"LAST_ASK_TIME_TODAY_REALTIME\": \"01:17:40\",\n", + " \"LAST_PRICE_TIME_TODAY_REALTIME\": \"01:17:40\",\n", + " \"LAST_UPDATE_ALL_SESSIONS_RT\": \"01:17:40\"\n", + " }\n", + " }\n", + "}\n", + "{\n", + " \"fragmentType\": 0,\n", + " \"correlationIds\": [\n", + " \"USDCAD Curncy\"\n", + " ],\n", + " \"messageType\": \"MarketDataEvents\",\n", + " \"timeReceived\": \"2023-11-21T04:17:42.131746+0000\",\n", + " \"element\": {\n", + " \"MarketDataEvents\": {\n", + " \"MKTDATA_EVENT_TYPE\": \"TRADE\",\n", + " \"MKTDATA_EVENT_SUBTYPE\": \"NEW\",\n", + " \"LAST_ALL_SESSIONS\": 1.3707,\n", + " \"LAST2_TRADE\": 1.3707,\n", + " \"LAST_PRICE\": 1.3707,\n", + " \"LAST_PRICE_TDY\": 1.3707,\n", + " \"LAST2_DIR\": null,\n", + " \"LAST_TICK_DIRECTION_RT\": 0,\n", + " \"ALL_PRICE_COND_CODE\": null,\n", + " \"ALL_PRICE\": 1.3707,\n", + " \"LAST_PRICE_COND_CODE_RT\": null,\n", + " \"PRICE_CHANGE_1Y_NET_RT\": 0.02590000070631504,\n", + " \"PRICE_CHANGE_1Y_PCT_RT\": 1.9258999824523926,\n", + " \"LAST_CONTINUOUS_TRADE_PRICE_RT\": 1.3707,\n", + " \"PRICE_LAST_RT\": 1.3707,\n", + " \"EVT_TRADE_PRICE_RT\": 1.3707,\n", + " \"EVT_TRADE_SIZE_RT\": 0,\n", + " \"EVT_TRADE_CONDITION_CODE_RT\": null,\n", + " \"EVT_TRADE_SOURCE_TYPE_CODE_RT\": null,\n", + " \"EVT_TRADE_INTEGER_IDENTIFIER_RT\": null,\n", + " \"EVT_TRADE_IDENTIFIER_RT\": null,\n", + " \"EXCH_CODE_LAST_ALL_SESSION_RT\": null,\n", + " \"LOC_SRC_FOR_LST_TRD_ALL_SESS_RT\": null,\n", + " \"CHG_NET_REG_SESSION_CLOSE\": null,\n", + " \"CHG_PCT_REG_SESSION_CLOSE\": null,\n", + " \"CHG_NET_PREV_DAY_POST_CLS\": null,\n", + " \"CHG_PCT_PREV_DAY_POST_CLS\": null,\n", + " \"CHG_NET_1D_PRE_FROM_REGULAR_RT\": null,\n", + " \"CHG_PCT_1D_PRE_FROM_REGULAR_RT\": null,\n", + " \"CHG_NET_1D_ALL_FROM_REGULAR_RT\": -0.0018,\n", + " \"CHG_PCT_1D_ALL_FROM_REGULAR_RT\": -0.1311,\n", + " \"CHG_NET_1D_POST_FROM_REGULAR_RT\": null,\n", + " \"CHG_PCT_1D_POST_FROM_REGULAR_RT\": null,\n", + " \"PX_CHG_NET_1D_ALL_SWITCHOVER_RT\": -0.0018,\n", + " \"PX_CHG_PCT_1D_ALL_SWITCHOVER_RT\": -0.1311,\n", + " \"CHG_NET_REG_SES_PRV_RG_SES_CL_RT\": -0.0018,\n", + " \"CHG_PCT_REG_SES_PRV_RG_SES_CL_RT\": -0.131148,\n", + " \"CHG_NET_MTD_RT\": -0.0168,\n", + " \"CHG_PCT_MTD_RT\": -1.2108,\n", + " \"CHG_NET_QTD_RT\": 0.013,\n", + " \"CHG_PCT_QTD_RT\": 0.9575,\n", + " \"CHG_NET_YTD_RT\": 0.0153,\n", + " \"CHG_PCT_YTD_RT\": 1.1288,\n", + " \"CHG_NET_1M_RT\": 0.0016,\n", + " \"CHG_PCT_1M_RT\": 0.1169,\n", + " \"CHG_NET_3M_RT\": 0.0163,\n", + " \"CHG_PCT_3M_RT\": 1.2035,\n", + " \"REALTIME_2_DAY_CHANGE_NET\": -0.0016,\n", + " \"REALTIME_2_DAY_CHANGE_PERCENT\": -0.1166,\n", + " \"REALTIME_5_DAY_CHANGE_NET\": 0.0014,\n", + " \"REALTIME_5_DAY_CHANGE_PERCENT\": 0.1022,\n", + " \"RT_PX_AS_PCT_INTRADAY_RANGE\": 8.0,\n", + " \"REALTIME_PERCENT_BID_ASK_SPREAD\": 0.00730000000000075,\n", + " \"RT_THEORETICAL_PCT_CHG_FROM_LAST\": null,\n", + " \"REALTIME_15_SEC_PRICE_PCT_CHG\": null,\n", + " \"REALTIME_ONE_MIN_PRICE_PCT_CHG\": null,\n", + " \"REALTIME_FIVE_MIN_PRICE_PCT_CHG\": null,\n", + " \"REALTIME_15_MIN_PRICE_PCT_CHG\": null,\n", + " \"REALTIME_ONE_HOUR_PRICE_PCT_CHG\": null,\n", + " \"REALTIME_VOLUME_5_DAY_INTERVAL\": null,\n", + " \"DELTA_AVAT_1_DAY_INTERVAL\": null,\n", + " \"DELTA_AVAT_5_DAY_INTERVAL\": null,\n", + " \"DELTA_AVAT_10_DAY_INTERVAL\": null,\n", + " \"DELTA_AVAT_20_DAY_INTERVAL\": null,\n", + " \"DELTA_AVAT_30_DAY_INTERVAL\": null,\n", + " \"DELTA_AVAT_100_DAY_INTERVAL\": null,\n", + " \"DELTA_AVAT_180_DAY_INTERVAL\": null,\n", + " \"DELTA_ATAT_1_DAY_INTERVAL\": null,\n", + " \"DELTA_ATAT_5_DAY_INTERVAL\": null,\n", + " \"DELTA_ATAT_10_DAY_INTERVAL\": null,\n", + " \"DELTA_ATAT_20_DAY_INTERVAL\": null,\n", + " \"DELTA_ATAT_30_DAY_INTERVAL\": null,\n", + " \"DELTA_ATAT_100_DAY_INTERVAL\": null,\n", + " \"DELTA_ATAT_180_DAY_INTERVAL\": null,\n", + " \"RT_PX_CHG_NET_1D\": -0.0018,\n", + " \"RT_PX_CHG_PCT_1D\": -0.13109999895095825,\n", + " \"PRICE_CHANGE_ON_DAY_RT\": -0.0018,\n", + " \"NET_CHANGE_ON_DAY_TODAY_RT\": -0.0018,\n", + " \"PERCENT_CHANGE_ON_DAY_TODAY_RT\": -0.1311,\n", + " \"LAST_UPDATE_CHG_1D_RT\": -0.0017999999690800905,\n", + " \"EVT_TRADE_TIME_RT\": \"01:17:40\",\n", + " \"TRADE_UPDATE_STAMP_RT\": \"2023-11-21T01:17:40.000000-0300\",\n", + " \"IS_DELAYED_STREAM\": false,\n", + " \"EVT_UPDATE_ACTIONS_RT\": 271,\n", + " \"TIME\": \"01:17:40\",\n", + " \"PRICE_LAST_TIME_RT\": \"01:17:40\",\n", + " \"LAST_PRICE_TIME_TODAY_REALTIME\": \"01:17:40\",\n", + " \"EVENT_TIME\": \"01:17:40\",\n", + " \"LAST_UPDATE_ALL_SESSIONS_RT\": \"01:17:40\",\n", + " \"BLOOMBERG_SEND_TIME_RT\": \"2023-11-21T04:17:42.131000+0000\"\n", + " }\n", + " }\n", + "}\n" + ] + } + ], + "source": [ + "with blp.BlpStream() as bs:\n", + " bs.subscribe({\"USDCAD Curncy\": {\"fields\": [\"LAST_PRICE\"]}})\n", + " n = 0\n", + " for ev in bs.events(timeout=60):\n", + " print(json.dumps(ev, indent=2, cls=CustomJSONEncoder))\n", + " n += 1\n", + " if n > 1:\n", + " break" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "myenv", + "language": "python", + "name": "myenv" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}