{ "cells": [ { "cell_type": "code", "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "from xbbg import blp, pipeline, const" ], "outputs": [], "execution_count": 1, "metadata": { "id": "-BYJlOV2-BTp", "execution": { "iopub.status.busy": "2021-01-01T17:05:44.607Z", "iopub.execute_input": "2021-01-01T17:05:44.618Z", "iopub.status.idle": "2021-01-01T17:05:44.674Z", "shell.execute_reply": "2021-01-01T17:05:44.661Z" } } }, { "cell_type": "markdown", "source": [ "# Reference Data `BDP` and `BDS`" ], "metadata": { "id": "c5LBd3_p_iCF" } }, { "cell_type": "code", "source": [ "blp.__version__" ], "outputs": [ { "output_type": "execute_result", "execution_count": 2, "data": { "text/plain": "'0.7.5a5'" }, "metadata": {} } ], "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6rIxWjWJTMY7", "outputId": "48c52c3f-3c29-4535-acbc-38c5c10e7f18", "execution": { "iopub.status.busy": "2021-01-01T17:05:44.691Z", "iopub.execute_input": "2021-01-01T17:05:44.699Z", "iopub.status.idle": "2021-01-01T17:05:44.728Z", "shell.execute_reply": "2021-01-01T17:05:44.798Z" } } }, { "cell_type": "code", "source": [ "blp.bdp('AAPL US Equity', flds=['Security_Name', 'Last_Price'])" ], "outputs": [ { "output_type": "execute_result", "execution_count": 3, "data": { "text/plain": " security_name last_price\nAAPL US Equity Apple Inc 132.69", "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
security_namelast_price
AAPL US EquityApple Inc132.69
\n
" }, "metadata": {} } ], "execution_count": 3, "metadata": { "id": "H48xG1Hd_Twg", "colab": { "base_uri": "https://localhost:8080/", "height": 81 }, "outputId": "755004b3-83e8-4a1d-bb04-c67705b8c4ac", "execution": { "iopub.status.busy": "2021-01-01T17:05:44.750Z", "iopub.execute_input": "2021-01-01T17:05:44.762Z", "iopub.status.idle": "2021-01-01T17:05:45.819Z", "shell.execute_reply": "2021-01-01T17:05:45.807Z" } } }, { "cell_type": "code", "source": [ "blp.bdp('6758 JP Equity', flds='Crncy_Adj_Mkt_Cap', Eqy_Fund_Crncy='USD')" ], "outputs": [ { "output_type": "execute_result", "execution_count": 4, "data": { "text/plain": " crncy_adj_mkt_cap\n6758 JP Equity 125,678.20", "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n
crncy_adj_mkt_cap
6758 JP Equity125,678.20
\n
" }, "metadata": {} } ], "execution_count": 4, "metadata": { "id": "pEya245x_qWa", "colab": { "base_uri": "https://localhost:8080/", "height": 81 }, "outputId": "54fed43a-0e60-4f1b-c2b1-51d543bf2b75", "execution": { "iopub.status.busy": "2021-01-01T17:05:45.839Z", "iopub.execute_input": "2021-01-01T17:05:45.850Z", "iopub.status.idle": "2021-01-01T17:05:46.178Z", "shell.execute_reply": "2021-01-01T17:05:46.155Z" } } }, { "cell_type": "code", "source": [ "holders = blp.bds('AMZN US Equity', flds='All_Holders_Public_Filings', cache=True)\n", "(\n", " holders\n", " .loc[:, ~holders.columns.str.contains(\n", " f'holder_id|portfolio_name|change|number|'\n", " f'metro|percent_of_portfolio|source'\n", " )]\n", " .rename(\n", " index=lambda tkr: tkr.replace(' Equity', ''),\n", " columns={\n", " 'holder_name_': 'holder',\n", " 'position_': 'position',\n", " 'filing_date__': 'filing_dt',\n", " 'percent_outstanding': 'pct_out',\n", " 'insider_status_': 'insider',\n", " }\n", " )\n", ").head()" ], "outputs": [ { "output_type": "execute_result", "execution_count": 5, "data": { "text/plain": " holder position filing_dt insider pct_out institution_type_ country\nAMZN US Bezos Jeffrey P 53,220,618.00 2020-12-02 Y 10.61 Unclassified n/a\nAMZN US Vanguard Group Inc/The 32,783,886.00 2020-09-30 N-P 6.53 Investment Advisor United States\nAMZN US BlackRock Inc 27,661,214.00 2020-09-30 N-P 5.51 Investment Advisor United States\nAMZN US Scott MacKenzie 19,786,240.00 2019-08-01 N-P 3.94 Unclassified n/a\nAMZN US FMR LLC 18,657,381.00 2020-12-31 N-P 3.72 Investment Advisor United States", "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
holderpositionfiling_dtinsiderpct_outinstitution_type_country
AMZN USBezos Jeffrey P53,220,618.002020-12-02Y10.61Unclassifiedn/a
AMZN USVanguard Group Inc/The32,783,886.002020-09-30N-P6.53Investment AdvisorUnited States
AMZN USBlackRock Inc27,661,214.002020-09-30N-P5.51Investment AdvisorUnited States
AMZN USScott MacKenzie19,786,240.002019-08-01N-P3.94Unclassifiedn/a
AMZN USFMR LLC18,657,381.002020-12-31N-P3.72Investment AdvisorUnited States
\n
" }, "metadata": {} } ], "execution_count": 5, "metadata": { "id": "tyXZPF62Angb", "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "outputId": "977ad30f-43c5-4bb4-f271-a73b788f2e18", "execution": { "iopub.status.busy": "2021-01-01T17:05:46.196Z", "iopub.execute_input": "2021-01-01T17:05:46.208Z", "iopub.status.idle": "2021-01-01T17:05:46.250Z", "shell.execute_reply": "2021-01-01T17:05:46.288Z" } } }, { "cell_type": "code", "source": [ "blp.dividend('SPY US Equity', start_date='2019')" ], "outputs": [ { "output_type": "execute_result", "execution_count": 6, "data": { "text/plain": " dec_date ex_date rec_date pay_date dvd_amt dvd_freq dvd_type\nSPY US Equity 2020-12-17 2020-12-18 2020-12-21 2021-01-29 1.58 Quarter Income\nSPY US Equity 2020-09-17 2020-09-18 2020-09-21 2020-10-30 1.34 Quarter Income\nSPY US Equity 2020-06-18 2020-06-19 2020-06-22 2020-07-31 1.37 Quarter Income\nSPY US Equity 2020-03-19 2020-03-20 2020-03-23 2020-04-30 1.41 Quarter Income\nSPY US Equity 2019-12-19 2019-12-20 2019-12-23 2020-01-31 1.57 Quarter Income\nSPY US Equity 2019-09-19 2019-09-20 2019-09-23 2019-10-31 1.38 Quarter Income\nSPY US Equity 2019-06-20 2019-06-21 2019-06-24 2019-07-31 1.43 Quarter Income\nSPY US Equity 2019-03-14 2019-03-15 2019-03-18 2019-04-30 1.23 Quarter Income", "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
dec_dateex_daterec_datepay_datedvd_amtdvd_freqdvd_type
SPY US Equity2020-12-172020-12-182020-12-212021-01-291.58QuarterIncome
SPY US Equity2020-09-172020-09-182020-09-212020-10-301.34QuarterIncome
SPY US Equity2020-06-182020-06-192020-06-222020-07-311.37QuarterIncome
SPY US Equity2020-03-192020-03-202020-03-232020-04-301.41QuarterIncome
SPY US Equity2019-12-192019-12-202019-12-232020-01-311.57QuarterIncome
SPY US Equity2019-09-192019-09-202019-09-232019-10-311.38QuarterIncome
SPY US Equity2019-06-202019-06-212019-06-242019-07-311.43QuarterIncome
SPY US Equity2019-03-142019-03-152019-03-182019-04-301.23QuarterIncome
\n
" }, "metadata": {} } ], "execution_count": 6, "metadata": { "id": "X6_popxkC_ft", "colab": { "base_uri": "https://localhost:8080/", "height": 300 }, "outputId": "5e6ce9f6-4f13-460a-a1c8-8035f967ee5a", "execution": { "iopub.status.busy": "2021-01-01T17:05:46.268Z", "iopub.execute_input": "2021-01-01T17:05:46.278Z", "iopub.status.idle": "2021-01-01T17:05:46.609Z", "shell.execute_reply": "2021-01-01T17:05:46.659Z" } } }, { "cell_type": "code", "source": [ "blp.earning('FB US Equity', Eqy_Fund_Year=2018, Number_Of_Periods=2)" ], "outputs": [ { "output_type": "execute_result", "execution_count": 7, "data": { "text/plain": " segment_name level fy2019 fy2018 fy2019_pct fy2018_pct\nFB US Equity US & Canada 1 32,206.00 25,727.00 45.55 46.07\nFB US Equity Europe 1 16,826.00 13,631.00 23.80 24.41\nFB US Equity Asia-Pacific 1 15,406.00 11,733.00 21.79 21.01\nFB US Equity Rest of World 1 6,259.00 4,747.00 8.85 8.50", "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
segment_namelevelfy2019fy2018fy2019_pctfy2018_pct
FB US EquityUS & Canada132,206.0025,727.0045.5546.07
FB US EquityEurope116,826.0013,631.0023.8024.41
FB US EquityAsia-Pacific115,406.0011,733.0021.7921.01
FB US EquityRest of World16,259.004,747.008.858.50
\n
" }, "metadata": {} } ], "execution_count": 7, "metadata": { "id": "QCr2OoJZDHJE", "colab": { "base_uri": "https://localhost:8080/", "height": 175 }, "outputId": "efa9bc13-51af-4998-b040-dfe2aeaae51d", "execution": { "iopub.status.busy": "2021-01-01T17:05:46.623Z", "iopub.execute_input": "2021-01-01T17:05:46.633Z", "iopub.status.idle": "2021-01-01T17:05:47.349Z", "shell.execute_reply": "2021-01-01T17:05:47.397Z" } } }, { "cell_type": "markdown", "source": [ "# Historical Data" ], "metadata": { "id": "47tzPYCDDi10" } }, { "cell_type": "markdown", "source": [ "Historical data with Excel compatible overrides" ], "metadata": { "id": "AgjNESzWD_VT" } }, { "cell_type": "code", "source": [ "blp.bdh(\n", " tickers='SHCOMP Index', flds=['high', 'low', 'last_price'],\n", " start_date='2019-11', end_date='2020', Per='W', Fill='P', Days='A',\n", ")" ], "outputs": [ { "output_type": "execute_result", "execution_count": 8, "data": { "text/plain": " SHCOMP Index \n high low last_price\n2019-11-01 2,980.13 2,917.15 2,958.20\n2019-11-08 3,008.31 2,962.84 2,964.18\n2019-11-15 2,949.96 2,891.20 2,891.34\n2019-11-22 2,933.99 2,873.99 2,885.29\n2019-11-29 2,915.04 2,858.58 2,871.98\n2019-12-06 2,912.01 2,857.32 2,912.01\n2019-12-13 2,969.98 2,902.79 2,967.68\n2019-12-20 3,039.38 2,958.71 3,004.94\n2019-12-27 3,036.11 2,960.43 3,005.03", "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
SHCOMP Index
highlowlast_price
2019-11-012,980.132,917.152,958.20
2019-11-083,008.312,962.842,964.18
2019-11-152,949.962,891.202,891.34
2019-11-222,933.992,873.992,885.29
2019-11-292,915.042,858.582,871.98
2019-12-062,912.012,857.322,912.01
2019-12-132,969.982,902.792,967.68
2019-12-203,039.382,958.713,004.94
2019-12-273,036.112,960.433,005.03
\n
" }, "metadata": {} } ], "execution_count": 8, "metadata": { "id": "BXYJi9yfDhvB", "colab": { "base_uri": "https://localhost:8080/", "height": 363 }, "outputId": "9bd34a72-da26-42dc-abde-d4a2713f11c8", "execution": { "iopub.status.busy": "2021-01-01T17:05:47.361Z", "iopub.execute_input": "2021-01-01T17:05:47.370Z", "iopub.status.idle": "2021-01-01T17:05:47.778Z", "shell.execute_reply": "2021-01-01T17:05:47.768Z" } } }, { "cell_type": "markdown", "source": [ "Dividend / split adjustments" ], "metadata": { "id": "SE7izShuEFJv" } }, { "cell_type": "code", "source": [ "pd.concat([\n", " blp.bdh(\n", " 'AAPL US Equity', 'Px_Last', '20140605', '20140610',\n", " CshAdjNormal=True, CshAdjAbnormal=True, CapChg=True\n", " ).rename(columns={'Px_Last': 'Px_Adj'}),\n", " blp.bdh(\n", " 'AAPL US Equity', 'Px_Last', '20140605', '20140610',\n", " CshAdjNormal=False, CshAdjAbnormal=False, CapChg=False\n", " ).rename(columns={'Px_Last': 'Px_Raw'}),\n", "], axis=1)" ], "outputs": [ { "output_type": "execute_result", "execution_count": 9, "data": { "text/plain": " AAPL US Equity \n Px_Adj Px_Raw\n2014-06-05 20.87 647.35\n2014-06-06 20.81 645.57\n2014-06-09 21.14 93.70\n2014-06-10 21.27 94.25", "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
AAPL US Equity
Px_AdjPx_Raw
2014-06-0520.87647.35
2014-06-0620.81645.57
2014-06-0921.1493.70
2014-06-1021.2794.25
\n
" }, "metadata": {} } ], "execution_count": 9, "metadata": { "id": "VK8Dgs9HENnG", "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "outputId": "eaf32134-f75e-46f2-8067-5f4499c9c648", "execution": { "iopub.status.busy": "2021-01-01T17:05:47.790Z", "iopub.execute_input": "2021-01-01T17:05:47.800Z", "iopub.status.idle": "2021-01-01T17:05:48.508Z", "shell.execute_reply": "2021-01-01T17:05:48.542Z" } } }, { "cell_type": "markdown", "source": [ "# Intraday Bars" ], "metadata": { "id": "6jiuSZWfGp0F" } }, { "cell_type": "code", "source": [ "cur_dt = pd.Timestamp('today', tz='America/New_York').date()\n", "recent = pd.bdate_range(end=cur_dt, periods=2, tz='America/New_York')\n", "pre_dt = max(filter(lambda dd: dd < cur_dt, recent))\n", "pre_dt.date()" ], "outputs": [ { "output_type": "execute_result", "execution_count": 10, "data": { "text/plain": "datetime.date(2020, 12, 31)" }, "metadata": {} } ], "execution_count": 10, "metadata": { "id": "TUxTkLe5HMZA", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "686bcc7b-03b3-4e2c-ea30-f9b15ebca3fa", "execution": { "iopub.status.busy": "2021-01-01T17:05:48.523Z", "iopub.execute_input": "2021-01-01T17:05:48.531Z", "iopub.status.idle": "2021-01-01T17:05:48.619Z", "shell.execute_reply": "2021-01-01T17:05:48.602Z" } } }, { "cell_type": "code", "source": [ "blp.bdib('QQQ US Equity', dt=pre_dt, session='day').tail()" ], "outputs": [ { "output_type": "execute_result", "execution_count": 11, "data": { "text/plain": " QQQ US Equity \n open high low close volume num_trds value\n2020-12-31 15:55:00-05:00 313.62 313.71 313.52 313.69 248779 1317 78,021,848.00\n2020-12-31 15:56:00-05:00 313.69 313.89 313.69 313.88 166509 940 52,248,948.00\n2020-12-31 15:57:00-05:00 313.89 314.02 313.83 313.91 302438 1647 94,943,712.00\n2020-12-31 15:58:00-05:00 313.88 313.97 313.82 313.91 296271 1494 92,996,616.00\n2020-12-31 15:59:00-05:00 313.92 314.24 313.57 313.74 1059753 3740 332,712,192.00", "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
QQQ US Equity
openhighlowclosevolumenum_trdsvalue
2020-12-31 15:55:00-05:00313.62313.71313.52313.69248779131778,021,848.00
2020-12-31 15:56:00-05:00313.69313.89313.69313.8816650994052,248,948.00
2020-12-31 15:57:00-05:00313.89314.02313.83313.91302438164794,943,712.00
2020-12-31 15:58:00-05:00313.88313.97313.82313.91296271149492,996,616.00
2020-12-31 15:59:00-05:00313.92314.24313.57313.7410597533740332,712,192.00
\n
" }, "metadata": {} } ], "execution_count": 11, "metadata": { "id": "rIB0MtFvGri9", "colab": { "base_uri": "https://localhost:8080/", "height": 238 }, "outputId": "12d6186c-79e0-4a01-8978-4fa0bedf2397", "execution": { "iopub.status.busy": "2021-01-01T17:05:48.634Z", "iopub.execute_input": "2021-01-01T17:05:48.644Z", "shell.execute_reply": "2021-01-01T17:05:48.783Z", "iopub.status.idle": "2021-01-01T17:05:48.732Z" } } }, { "cell_type": "code", "source": [ "blp.bdib('388 HK Equity', dt=pre_dt, session='am_open_7')" ], "outputs": [ { "output_type": "execute_result", "execution_count": 12, "data": { "text/plain": " 388 HK Equity \n open high low close volume num_trds value\n2020-12-31 09:30:00+08:00 425.80 426.40 425.40 426.00 102300 140 43,568,700.00\n2020-12-31 09:31:00+08:00 426.40 427.80 426.20 427.20 9400 36 4,012,060.00\n2020-12-31 09:32:00+08:00 427.80 427.80 426.80 427.20 97600 153 41,678,700.00\n2020-12-31 09:33:00+08:00 426.80 429.00 426.80 428.20 90600 141 38,781,060.00\n2020-12-31 09:34:00+08:00 428.80 430.60 428.60 430.60 161700 225 69,512,560.00\n2020-12-31 09:35:00+08:00 430.60 431.40 430.00 431.00 40800 78 17,577,380.00\n2020-12-31 09:36:00+08:00 431.40 432.00 431.00 431.40 47400 77 20,454,480.00\n2020-12-31 09:37:00+08:00 431.00 431.00 429.40 429.60 27500 56 11,834,160.00", "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
388 HK Equity
openhighlowclosevolumenum_trdsvalue
2020-12-31 09:30:00+08:00425.80426.40425.40426.0010230014043,568,700.00
2020-12-31 09:31:00+08:00426.40427.80426.20427.209400364,012,060.00
2020-12-31 09:32:00+08:00427.80427.80426.80427.209760015341,678,700.00
2020-12-31 09:33:00+08:00426.80429.00426.80428.209060014138,781,060.00
2020-12-31 09:34:00+08:00428.80430.60428.60430.6016170022569,512,560.00
2020-12-31 09:35:00+08:00430.60431.40430.00431.00408007817,577,380.00
2020-12-31 09:36:00+08:00431.40432.00431.00431.40474007720,454,480.00
2020-12-31 09:37:00+08:00431.00431.00429.40429.60275005611,834,160.00
\n
" }, "metadata": {} } ], "execution_count": 12, "metadata": { "id": "NVuSvgmhb-iI", "colab": { "base_uri": "https://localhost:8080/", "height": 332 }, "outputId": "33a39a57-27c3-4e0d-b5b2-97ff2394f367", "execution": { "iopub.status.busy": "2021-01-01T17:05:48.748Z", "iopub.execute_input": "2021-01-01T17:05:48.758Z", "shell.execute_reply": "2021-01-01T17:05:48.890Z", "iopub.status.idle": "2021-01-01T17:05:48.826Z" } } }, { "cell_type": "markdown", "source": [ "# Intraday Tick Data" ], "metadata": { "id": "DP_bqoeYAr30" } }, { "cell_type": "code", "source": [ "blp.bdtick('QQQ US Equity', dt=pre_dt).tail(10)" ], "outputs": [ { "output_type": "execute_result", "execution_count": 13, "data": { "text/plain": " QQQ US Equity \n typ value volume cond exch\n2020-12-31 15:00:00-05:00 TRADE 312.88 100 R6,IS Q\n2020-12-31 15:00:00-05:00 TRADE 312.88 149 R6,IS P\n2020-12-31 15:00:00-05:00 TRADE 312.88 1 R6,IS,OL X\n2020-12-31 15:00:00-05:00 TRADE 312.89 41 OL Z\n2020-12-31 15:00:00-05:00 TRADE 312.88 17 OL B\n2020-12-31 15:00:00-05:00 TRADE 312.89 500 NaN P\n2020-12-31 15:00:00-05:00 TRADE 312.89 200 R6,IS Q\n2020-12-31 15:00:00-05:00 TRADE 312.89 200 R6,IS Q\n2020-12-31 15:00:00-05:00 TRADE 312.89 200 R6,IS K\n2020-12-31 15:00:00-05:00 TRADE 312.89 200 R6,IS Z", "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
QQQ US Equity
typvaluevolumecondexch
2020-12-31 15:00:00-05:00TRADE312.88100R6,ISQ
2020-12-31 15:00:00-05:00TRADE312.88149R6,ISP
2020-12-31 15:00:00-05:00TRADE312.881R6,IS,OLX
2020-12-31 15:00:00-05:00TRADE312.8941OLZ
2020-12-31 15:00:00-05:00TRADE312.8817OLB
2020-12-31 15:00:00-05:00TRADE312.89500NaNP
2020-12-31 15:00:00-05:00TRADE312.89200R6,ISQ
2020-12-31 15:00:00-05:00TRADE312.89200R6,ISQ
2020-12-31 15:00:00-05:00TRADE312.89200R6,ISK
2020-12-31 15:00:00-05:00TRADE312.89200R6,ISZ
\n
" }, "metadata": {} } ], "execution_count": 13, "metadata": { "id": "h16z0fv7ApPa", "colab": { "base_uri": "https://localhost:8080/", "height": 394 }, "outputId": "c0fe5dde-0d22-4368-e55e-ece0cf6f4cd3", "execution": { "iopub.status.busy": "2021-01-01T17:05:48.840Z", "iopub.execute_input": "2021-01-01T17:05:48.851Z", "iopub.status.idle": "2021-01-01T17:05:59.166Z", "shell.execute_reply": "2021-01-01T17:05:59.150Z" } } }, { "cell_type": "markdown", "source": [ "# Equity Screen `BEQS`" ], "metadata": { "id": "EGbQrVbcGjTg" } }, { "cell_type": "code", "source": [ "blp.beqs('Core Capital Ratios', typ='GLOBAL').iloc[:5, :6]" ], "outputs": [ { "output_type": "execute_result", "execution_count": 14, "data": { "text/plain": " ticker short_name market_cap tier_1_cap_rt_lf tot_rsk_bsd_cap_rt_lf moody's_issuer_rtg\n8331 JP 8331 JP CHIBA BANK LTD 4,488,526,848.00 11.89 12.44 A1\n8355 JP 8355 JP SHIZUOKA BANK 4,359,666,688.00 16.17 16.17 A1\nABN NA ABN NA ABN AMRO BANK-CV 9,208,644,608.00 19.00 23.40 A1\nACA FP ACA FP CREDIT AGRICOLE 36,767,424,512.00 14.10 18.20 Aa3\nBAC US BAC US BANK OF AMERICA 262,205,423,616.00 13.50 16.10 A2", "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
tickershort_namemarket_captier_1_cap_rt_lftot_rsk_bsd_cap_rt_lfmoody's_issuer_rtg
8331 JP8331 JPCHIBA BANK LTD4,488,526,848.0011.8912.44A1
8355 JP8355 JPSHIZUOKA BANK4,359,666,688.0016.1716.17A1
ABN NAABN NAABN AMRO BANK-CV9,208,644,608.0019.0023.40A1
ACA FPACA FPCREDIT AGRICOLE36,767,424,512.0014.1018.20Aa3
BAC USBAC USBANK OF AMERICA262,205,423,616.0013.5016.10A2
\n
" }, "metadata": {} } ], "execution_count": 14, "metadata": { "id": "2Q4CEzWkF0GH", "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "outputId": "1db4c0d8-21e2-4e82-853e-10cc473c50c2", "execution": { "iopub.status.busy": "2021-01-01T17:05:59.184Z", "iopub.execute_input": "2021-01-01T17:05:59.199Z", "shell.execute_reply": "2021-01-01T17:06:03.432Z", "iopub.status.idle": "2021-01-01T17:06:03.443Z" } } }, { "cell_type": "markdown", "source": [ "# Subscription" ], "metadata": { "id": "513ExSuJAn7m" } }, { "cell_type": "markdown", "source": [ "`blp.live` will yield market data as `dict`" ], "metadata": { "id": "r_F2LaVFzTs-" } }, { "cell_type": "code", "source": [ "async for snap in blp.live(['ESA Index', 'NQA Index'], info=const.LIVE_INFO, max_cnt=2):\n", " print(snap)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{'TICKER': 'NQA Index', 'FIELD': 'LAST_PRICE', 'MKTDATA_EVENT_TYPE': 'SUMMARY', 'MKTDATA_EVENT_SUBTYPE': 'INITPAINT', 'BID': 12882.25, 'ASK': 12887.0, 'LAST_PRICE': 12885.5, 'VOLUME': 277076, 'MID': 12884.63, 'SPREAD_BA': 4.75, 'IS_DELAYED_STREAM': True, 'REALTIME_PERCENT_BID_ASK_SPREAD': 0.03690000000000282, 'RT_PX_CHG_PCT_1D': 0.3425999879837036}\n", "{'TICKER': 'ESA Index', 'FIELD': 'LAST_PRICE', 'MKTDATA_EVENT_TYPE': 'SUMMARY', 'MKTDATA_EVENT_SUBTYPE': 'INITPAINT', 'BID': 3748.5, 'ASK': 3749.0, 'LAST_PRICE': 3748.75, 'VOLUME': 917212, 'MID': 3748.75, 'SPREAD_BA': 0.5, 'IS_DELAYED_STREAM': True, 'REALTIME_PERCENT_BID_ASK_SPREAD': 0.013300000000000978, 'RT_PX_CHG_PCT_1D': 0.6578999757766724}\n" ] } ], "execution_count": 15, "metadata": { "id": "IUU5v2ogAL9w", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "e8f84db3-d66e-42b3-9fa3-cd996d000504", "execution": { "iopub.status.busy": "2021-01-01T17:06:03.466Z", "iopub.execute_input": "2021-01-01T17:06:03.475Z", "iopub.status.idle": "2021-01-01T17:06:05.210Z", "shell.execute_reply": "2021-01-01T17:06:05.255Z" } } }, { "cell_type": "markdown", "source": [ "# Pipelines" ], "metadata": { "id": "nfE-ygMl9O46" } }, { "cell_type": "code", "source": [ "fx = blp.bdib('JPY Curncy', dt=pre_dt)\n", "jp = pd.concat([\n", " blp.bdib(ticker, dt=pre_dt, session='day')\n", " for ticker in ['7974 JP Equity', '9984 JP Equity']\n", "], axis=1)\n", "jp.tail()" ], "outputs": [ { "output_type": "execute_result", "execution_count": 16, "data": { "text/plain": " 7974 JP Equity ... 9984 JP Equity \\\n open high low close volume ... low close volume \n2020-12-30 14:54:00+09:00 65,980.00 66,000.00 65,940.00 65,970.00 2,600.00 ... 8,075.00 8,079.00 32000 \n2020-12-30 14:55:00+09:00 65,940.00 65,950.00 65,920.00 65,950.00 3,200.00 ... 8,078.00 8,080.00 45400 \n2020-12-30 14:56:00+09:00 65,940.00 65,940.00 65,880.00 65,880.00 4,400.00 ... 8,076.00 8,078.00 46200 \n2020-12-30 14:57:00+09:00 65,900.00 65,920.00 65,880.00 65,910.00 2,600.00 ... 8,077.00 8,083.00 49600 \n2020-12-30 14:58:00+09:00 65,910.00 65,930.00 65,900.00 65,920.00 4,300.00 ... 8,078.00 8,081.00 59400 \n\n \n num_trds value \n2020-12-30 14:54:00+09:00 60 258,512,704.00 \n2020-12-30 14:55:00+09:00 78 366,882,688.00 \n2020-12-30 14:56:00+09:00 91 373,219,200.00 \n2020-12-30 14:57:00+09:00 111 400,801,600.00 \n2020-12-30 14:58:00+09:00 110 479,994,720.00 \n\n[5 rows x 14 columns]", "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
7974 JP Equity...9984 JP Equity
openhighlowclosevolume...lowclosevolumenum_trdsvalue
2020-12-30 14:54:00+09:0065,980.0066,000.0065,940.0065,970.002,600.00...8,075.008,079.003200060258,512,704.00
2020-12-30 14:55:00+09:0065,940.0065,950.0065,920.0065,950.003,200.00...8,078.008,080.004540078366,882,688.00
2020-12-30 14:56:00+09:0065,940.0065,940.0065,880.0065,880.004,400.00...8,076.008,078.004620091373,219,200.00
2020-12-30 14:57:00+09:0065,900.0065,920.0065,880.0065,910.002,600.00...8,077.008,083.0049600111400,801,600.00
2020-12-30 14:58:00+09:0065,910.0065,930.0065,900.0065,920.004,300.00...8,078.008,081.0059400110479,994,720.00
\n

5 rows × 14 columns

\n
" }, "metadata": {} } ], "execution_count": 16, "metadata": { "id": "oeoJykcSAjqY", "colab": { "base_uri": "https://localhost:8080/", "height": 267 }, "outputId": "ef267f23-4218-4277-a9e1-b5a8239dde52", "execution": { "iopub.status.busy": "2021-01-01T17:06:05.226Z", "iopub.execute_input": "2021-01-01T17:06:05.237Z", "iopub.status.idle": "2021-01-01T17:06:05.360Z", "shell.execute_reply": "2021-01-01T17:06:05.395Z" } } }, { "cell_type": "markdown", "source": [ "Get `close` prices and convert to USD" ], "metadata": { "id": "T9-5WgXj_vBp" } }, { "cell_type": "code", "source": [ "prices = (\n", " jp\n", " .pipe(pipeline.get_series, col='close')\n", " .pipe(pipeline.apply_fx, fx=fx)\n", " .tz_convert('Asia/Tokyo')\n", ")\n", "prices.tail()" ], "outputs": [ { "output_type": "execute_result", "execution_count": 17, "data": { "text/plain": " 7974 JP Equity 9984 JP Equity\n2020-12-30 14:54:00+09:00 638.56 78.20\n2020-12-30 14:55:00+09:00 638.37 78.21\n2020-12-30 14:56:00+09:00 637.69 78.19\n2020-12-30 14:57:00+09:00 637.92 78.23\n2020-12-30 14:58:00+09:00 637.96 78.21", "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
7974 JP Equity9984 JP Equity
2020-12-30 14:54:00+09:00638.5678.20
2020-12-30 14:55:00+09:00638.3778.21
2020-12-30 14:56:00+09:00637.6978.19
2020-12-30 14:57:00+09:00637.9278.23
2020-12-30 14:58:00+09:00637.9678.21
\n
" }, "metadata": {} } ], "execution_count": 17, "metadata": { "id": "qCDBiBx4-JMk", "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "outputId": "1bde3569-ba9e-4c40-aadb-88ba6c3680da", "execution": { "iopub.status.busy": "2021-01-01T17:06:05.374Z", "iopub.execute_input": "2021-01-01T17:06:05.383Z", "shell.execute_reply": "2021-01-01T17:06:05.423Z", "iopub.status.idle": "2021-01-01T17:06:05.447Z" } } }, { "cell_type": "markdown", "source": [ "## Customized Pipelines" ], "metadata": { "id": "WY9v31j7lhcm" } }, { "cell_type": "markdown", "source": [ "VWAP for intraday bar data" ], "metadata": { "id": "teA0jx5axwsx" } }, { "cell_type": "code", "source": [ "def vwap(data: pd.DataFrame, fx=None, name=None) -> pd.Series:\n", " return pd.Series({\n", " ticker: (\n", " data[ticker][['close', 'volume']].prod(axis=1).sum()\n", " if fx is None else (\n", " data[ticker].close\n", " .pipe(pipeline.apply_fx, fx)\n", " .close\n", " .mul(data[ticker].volume)\n", " .sum()\n", " )\n", " ) / data[ticker].volume.sum()\n", " for ticker in data.columns.get_level_values(0).unique()\n", " }, name=name)" ], "outputs": [], "execution_count": 18, "metadata": { "id": "Iy5cJP_N_iyk", "execution": { "iopub.status.busy": "2021-01-01T17:06:05.464Z", "iopub.execute_input": "2021-01-01T17:06:05.475Z", "iopub.status.idle": "2021-01-01T17:06:05.490Z", "shell.execute_reply": "2021-01-01T17:06:05.580Z" } } }, { "cell_type": "markdown", "source": [ "VWAP in local currency" ], "metadata": { "id": "aLsq0Gv_xnA-" } }, { "cell_type": "code", "source": [ "jp.pipe(vwap, name=jp.index[-1].date())" ], "outputs": [ { "output_type": "execute_result", "execution_count": 19, "data": { "text/plain": "7974 JP Equity 66,033.79\n9984 JP Equity 8,032.60\nName: 2020-12-30, dtype: float64" }, "metadata": {} } ], "execution_count": 19, "metadata": { "id": "eFZ7BVXKmWbB", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "a8652aa3-d365-4c1f-a748-9b75c176d339", "execution": { "iopub.status.busy": "2021-01-01T17:06:05.504Z", "iopub.execute_input": "2021-01-01T17:06:05.516Z", "iopub.status.idle": "2021-01-01T17:06:05.545Z", "shell.execute_reply": "2021-01-01T17:06:05.585Z" } } }, { "cell_type": "markdown", "source": [ "VWAP in USD" ], "metadata": { "id": "PLKVB7aExpXU" } }, { "cell_type": "code", "source": [ "jp.pipe(vwap, fx=fx, name=jp.index[-1].date())" ], "outputs": [ { "output_type": "execute_result", "execution_count": 20, "data": { "text/plain": "7974 JP Equity 638.34\n9984 JP Equity 77.68\nName: 2020-12-30, dtype: float64" }, "metadata": {} } ], "execution_count": 20, "metadata": { "id": "dJRcosfWw78M", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "9683aecb-fb82-4d36-a7a8-99777001405e", "execution": { "iopub.status.busy": "2021-01-01T17:06:05.560Z", "iopub.execute_input": "2021-01-01T17:06:05.570Z", "iopub.status.idle": "2021-01-01T17:06:05.644Z", "shell.execute_reply": "2021-01-01T17:06:05.700Z" } } }, { "cell_type": "markdown", "source": [ "Total traded volume as of time in day for past few days" ], "metadata": { "id": "CwkBOgdwxhGM" } }, { "cell_type": "code", "source": [ "jp_hist = pd.concat([\n", " pd.concat([\n", " blp.bdib(ticker, dt=dt, session='day')\n", " for ticker in ['7974 JP Equity', '9984 JP Equity']\n", " ], axis=1)\n", " for dt in pd.bdate_range(end='today', periods=10)[:-1]\n", "], sort=False)\n", "unique(jp_hist.index.date)" ], "outputs": [ { "output_type": "execute_result", "execution_count": 21, "data": { "text/plain": "array([datetime.date(2020, 12, 21), datetime.date(2020, 12, 22),\n datetime.date(2020, 12, 23), datetime.date(2020, 12, 24),\n datetime.date(2020, 12, 25), datetime.date(2020, 12, 28),\n datetime.date(2020, 12, 29), datetime.date(2020, 12, 30)],\n dtype=object)" }, "metadata": {} } ], "execution_count": 21, "metadata": { "id": "ufOqMLr8WoAa", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "c3fcfc38-1ced-4ed1-9850-2aa1c70af4b9", "execution": { "iopub.status.busy": "2021-01-01T17:06:05.659Z", "iopub.execute_input": "2021-01-01T17:06:05.671Z", "shell.execute_reply": "2021-01-01T17:06:06.467Z", "iopub.status.idle": "2021-01-01T17:06:06.427Z" } } }, { "cell_type": "code", "source": [ "def drop_zeros(data: pd.DataFrame) -> pd.DataFrame:\n", " return (\n", " data\n", " .replace(0, np.nan)\n", " .dropna(how='all')\n", " .replace(np.nan, 0)\n", " )" ], "outputs": [], "execution_count": 22, "metadata": { "id": "1rjd1Fh6VQR7", "execution": { "iopub.status.busy": "2021-01-01T17:06:06.440Z", "iopub.execute_input": "2021-01-01T17:06:06.449Z", "iopub.status.idle": "2021-01-01T17:06:06.485Z", "shell.execute_reply": "2021-01-01T17:06:06.473Z" } } }, { "cell_type": "code", "source": [ "def traded_volume(data: pd.DataFrame, asof: str) -> pd.DataFrame:\n", " return (\n", " data\n", " .pipe(pipeline.get_series, col='volume')\n", " .between_time('0:00', asof)\n", " .resample('B')\n", " .sum()\n", " .pipe(drop_zeros)\n", " )" ], "outputs": [], "execution_count": 23, "metadata": { "id": "pkYreXZepajc", "execution": { "iopub.status.busy": "2021-01-01T17:06:06.500Z", "iopub.execute_input": "2021-01-01T17:06:06.508Z", "iopub.status.idle": "2021-01-01T17:06:06.523Z", "shell.execute_reply": "2021-01-01T17:06:06.564Z" } } }, { "cell_type": "code", "source": [ "jp_hist.pipe(traded_volume, asof='10:00')" ], "outputs": [ { "output_type": "execute_result", "execution_count": 24, "data": { "text/plain": " 7974 JP Equity 9984 JP Equity\n2020-12-21 00:00:00+09:00 443,500.00 3868900\n2020-12-22 00:00:00+09:00 480,500.00 3702500\n2020-12-23 00:00:00+09:00 331,600.00 3981400\n2020-12-24 00:00:00+09:00 219,200.00 6847500\n2020-12-25 00:00:00+09:00 110,200.00 6366800\n2020-12-28 00:00:00+09:00 250,100.00 5274400\n2020-12-29 00:00:00+09:00 328,200.00 3325300\n2020-12-30 00:00:00+09:00 347,200.00 3665700", "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
7974 JP Equity9984 JP Equity
2020-12-21 00:00:00+09:00443,500.003868900
2020-12-22 00:00:00+09:00480,500.003702500
2020-12-23 00:00:00+09:00331,600.003981400
2020-12-24 00:00:00+09:00219,200.006847500
2020-12-25 00:00:00+09:00110,200.006366800
2020-12-28 00:00:00+09:00250,100.005274400
2020-12-29 00:00:00+09:00328,200.003325300
2020-12-30 00:00:00+09:00347,200.003665700
\n
" }, "metadata": {} } ], "execution_count": 24, "metadata": { "id": "Q8lX043tVQ6Y", "colab": { "base_uri": "https://localhost:8080/", "height": 300 }, "outputId": "c94e7403-99fe-40b1-c6d1-72ff5e42c49a", "execution": { "iopub.status.busy": "2021-01-01T17:06:06.537Z", "iopub.execute_input": "2021-01-01T17:06:06.547Z", "iopub.status.idle": "2021-01-01T17:06:06.584Z", "shell.execute_reply": "2021-01-01T17:06:06.633Z" } } }, { "cell_type": "code", "source": [ "jp_hist.pipe(traded_volume, asof='11:00')" ], "outputs": [ { "output_type": "execute_result", "execution_count": 25, "data": { "text/plain": " 7974 JP Equity 9984 JP Equity\n2020-12-21 00:00:00+09:00 627,200.00 6054000\n2020-12-22 00:00:00+09:00 681,000.00 5594700\n2020-12-23 00:00:00+09:00 531,700.00 6849000\n2020-12-24 00:00:00+09:00 297,700.00 9501200\n2020-12-25 00:00:00+09:00 226,000.00 8100000\n2020-12-28 00:00:00+09:00 350,200.00 7074800\n2020-12-29 00:00:00+09:00 459,500.00 5590500\n2020-12-30 00:00:00+09:00 432,200.00 5847900", "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
7974 JP Equity9984 JP Equity
2020-12-21 00:00:00+09:00627,200.006054000
2020-12-22 00:00:00+09:00681,000.005594700
2020-12-23 00:00:00+09:00531,700.006849000
2020-12-24 00:00:00+09:00297,700.009501200
2020-12-25 00:00:00+09:00226,000.008100000
2020-12-28 00:00:00+09:00350,200.007074800
2020-12-29 00:00:00+09:00459,500.005590500
2020-12-30 00:00:00+09:00432,200.005847900
\n
" }, "metadata": {} } ], "execution_count": 25, "metadata": { "id": "JmqNerQiTbVW", "colab": { "base_uri": "https://localhost:8080/", "height": 300 }, "outputId": "bf276ae0-6fdb-4f4b-9cdb-ab466389a0dd", "execution": { "iopub.status.busy": "2021-01-01T17:06:06.597Z", "iopub.execute_input": "2021-01-01T17:06:06.605Z", "iopub.status.idle": "2021-01-01T17:06:06.656Z", "shell.execute_reply": "2021-01-01T17:06:06.639Z" } } }, { "cell_type": "code", "source": [], "outputs": [], "execution_count": null, "metadata": { "id": "L1qu50oaO6Rv" } } ], "metadata": { "colab": { "name": "Bloomberg Data Examples.ipynb", "provenance": [], "collapsed_sections": [], "toc_visible": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python", "version": "3.7.9", "mimetype": "text/x-python", "codemirror_mode": { "name": "ipython", "version": 3 }, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "file_extension": ".py" }, "nteract": { "version": "0.27.0" } }, "nbformat": 4, "nbformat_minor": 0 }