{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example using the data manager classes\n", "This notebook shows how to use the data manager framework for simpler API usage and for caching capabilities. \n", "\n", "Please note that in order to request bloomberg fields using property access, it must be CAPITALIZED. (sid.PX_AST NOT sid.px_last)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import tia.bbg.datamgr as dm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Single Security Accessor" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# create a DataManager for simpler api access\n", "mgr = dm.BbgDataManager()\n", "# retrieve a single security accessor from the manager\n", "msft = mgr['MSFT US EQUITY']" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(47.590000000000003, 47.229999999999997)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Can now access any Bloomberg field (as long as it is upper case)\n", "msft.PX_LAST, msft.PX_OPEN" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[47.59, 47.23]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Access multiple fields at the same time\n", "msft['PX_LAST', 'PX_OPEN']" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[47.59, 47.23]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# OR pass an array\n", "msft[['PX_LAST', 'PX_OPEN']]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PX_LAST
MSFT US EQUITY47.59
\n", "
" ], "text/plain": [ " PX_LAST\n", "MSFT US EQUITY 47.59" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Have the manager default to returning a frame instead of values\n", "mgr.sid_result_mode = 'frame'\n", "msft.PX_LAST" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PX_LASTPX_OPEN
MSFT US EQUITY47.58547.23
\n", "
" ], "text/plain": [ " PX_LAST PX_OPEN\n", "MSFT US EQUITY 47.585 47.23" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multiple fields returned as data frame\n", "msft[['PX_LAST', 'PX_OPEN']]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "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", "
PX_OPENPX_HIGHPX_LOWPX_LAST
date
2014-01-0237.35037.4037.1037.16
2014-01-0337.20037.2236.6036.91
2014-01-0636.85036.8936.1136.13
2014-01-0736.32536.4936.2136.41
2014-01-0836.00036.1435.5835.76
\n", "
" ], "text/plain": [ " PX_OPEN PX_HIGH PX_LOW PX_LAST\n", "date \n", "2014-01-02 37.350 37.40 37.10 37.16\n", "2014-01-03 37.200 37.22 36.60 36.91\n", "2014-01-06 36.850 36.89 36.11 36.13\n", "2014-01-07 36.325 36.49 36.21 36.41\n", "2014-01-08 36.000 36.14 35.58 35.76" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Retrieve historical data\n", "msft.get_historical(['PX_OPEN', 'PX_HIGH', 'PX_LOW', 'PX_LAST'], '1/1/2014', '1/12/2014').head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multi-security accessor" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "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", "
PX_LAST
CSCO US EQUITY28.89
IBM US EQUITY170.97
MSFT US EQUITY47.58
\n", "
" ], "text/plain": [ " PX_LAST\n", "CSCO US EQUITY 28.89\n", "IBM US EQUITY 170.97\n", "MSFT US EQUITY 47.58" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sids = mgr['MSFT US EQUITY', 'IBM US EQUITY', 'CSCO US EQUITY']\n", "sids.PX_LAST" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "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", "
IBM US EQUITYCSCO US EQUITYMSFT US EQUITY
date
2014-01-02185.5322.00037.16
2014-01-03186.6421.98036.91
2014-01-06186.0022.01036.13
2014-01-07189.7122.31036.41
2014-01-08187.9722.29335.76
\n", "
" ], "text/plain": [ " IBM US EQUITY CSCO US EQUITY MSFT US EQUITY\n", "date \n", "2014-01-02 185.53 22.000 37.16\n", "2014-01-03 186.64 21.980 36.91\n", "2014-01-06 186.00 22.010 36.13\n", "2014-01-07 189.71 22.310 36.41\n", "2014-01-08 187.97 22.293 35.76" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sids.get_historical('PX_LAST', '1/1/2014', '11/12/2014').head()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "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", "
IBM US EQUITYCSCO US EQUITYMSFT US EQUITY
PX_OPENPX_LASTPX_OPENPX_LASTPX_OPENPX_LAST
date
2014-01-02187.21185.5322.1722.00037.35037.16
2014-01-03185.83186.6422.0921.98037.20036.91
2014-01-06187.15186.0021.9622.01036.85036.13
2014-01-07186.39189.7122.2622.31036.32536.41
2014-01-08189.33187.9722.2922.29336.00035.76
\n", "
" ], "text/plain": [ " IBM US EQUITY CSCO US EQUITY MSFT US EQUITY \n", " PX_OPEN PX_LAST PX_OPEN PX_LAST PX_OPEN PX_LAST\n", "date \n", "2014-01-02 187.21 185.53 22.17 22.000 37.350 37.16\n", "2014-01-03 185.83 186.64 22.09 21.980 37.200 36.91\n", "2014-01-06 187.15 186.00 21.96 22.010 36.850 36.13\n", "2014-01-07 186.39 189.71 22.26 22.310 36.325 36.41\n", "2014-01-08 189.33 187.97 22.29 22.293 36.000 35.76" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sids.get_historical(['PX_OPEN', 'PX_LAST'], '1/1/2014', '11/12/2014').head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Caching" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#\n", "# ability to cache requests in memory or in h5 file\n", "#\n", "ms = dm.MemoryStorage()\n", "cmgr = dm.CachedDataManager(mgr, ms, pd.datetime.now())" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PX_LAST
MSFT US EQUITY47.585
\n", "
" ], "text/plain": [ " PX_LAST\n", "MSFT US EQUITY 47.585" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cmsft = cmgr['MSFT US EQUITY']\n", "cmsft.PX_LAST" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 loops, best of 3: 277 ms per loop\n" ] } ], "source": [ "%timeit msft.PX_LAST" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1000 loops, best of 3: 1.66 ms per loop\n" ] } ], "source": [ "%timeit cmsft.PX_LAST" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "csids = cmgr['MSFT US EQUITY', 'IBM US EQUITY']\n", "sids = mgr['MSFT US EQUITY', 'IBM US EQUITY']" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 loops, best of 3: 987 ms per loop\n" ] } ], "source": [ "%timeit sids.get_historical('PX_LAST', start='1/3/2000', end='1/3/2014').head()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 371.09 times longer than the fastest. This could mean that an intermediate result is being cached \n", "1 loops, best of 3: 5.17 ms per loop\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Anaconda\\lib\\site-packages\\pandas\\core\\index.py:1196: FutureWarning: using '-' to provide set differences with Indexes is deprecated, use .difference()\n", " \"use .difference()\",FutureWarning)\n" ] } ], "source": [ "%timeit csids.get_historical('PX_LAST', start='1/3/2000', end='1/3/2014').head()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Anaconda\\lib\\site-packages\\tables\\path.py:100: NaturalNameWarning: object name is not a valid Python identifier: 'MSFT US EQUITY'; it does not match the pattern ``^[a-zA-Z_][a-zA-Z0-9_]*$``; you will not be able to use natural naming to access this object; using ``getattr()`` will still work, though\n", " NaturalNameWarning)\n" ] }, { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PX_LAST
MSFT US EQUITY47.59
\n", "
" ], "text/plain": [ " PX_LAST\n", "MSFT US EQUITY 47.59" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# HD Storage\n", "# - note after executing the warning from hf api. I decided to leave blanks instead of replacing\n", "#\n", "\n", "import tempfile\n", "fh, fp = tempfile.mkstemp()\n", "\n", "h5storage = dm.HDFStorage(fp) # Can set compression level for smaller files\n", "h5mgr = dm.CachedDataManager(mgr, h5storage, pd.datetime.now())\n", "h5msft = h5mgr['MSFT US EQUITY']\n", "h5msft.PX_LAST" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PX_LAST
MSFT US EQUITY47.59
\n", "
" ], "text/plain": [ " PX_LAST\n", "MSFT US EQUITY 47.59" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Notice no warning as it is taken from cache\n", "h5msft.PX_LAST" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "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", "
PX_LAST
date
2000-01-0358.2813
2000-01-0456.3125
2000-01-0556.9063
2000-01-0655.0000
2000-01-0755.7188
\n", "
" ], "text/plain": [ " PX_LAST\n", "date \n", "2000-01-03 58.2813\n", "2000-01-04 56.3125\n", "2000-01-05 56.9063\n", "2000-01-06 55.0000\n", "2000-01-07 55.7188" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h5msft.get_historical('PX_LAST', start='1/2/2000', end='1/2/2014').head()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100 loops, best of 3: 6.18 ms per loop\n" ] } ], "source": [ "%timeit h5msft.get_historical('PX_LAST', start='1/2/2000', end='1/2/2014')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Anaconda\\lib\\site-packages\\tables\\path.py:100: NaturalNameWarning: object name is not a valid Python identifier: 'IBM US EQUITY'; it does not match the pattern ``^[a-zA-Z_][a-zA-Z0-9_]*$``; you will not be able to use natural naming to access this object; using ``getattr()`` will still work, though\n", " NaturalNameWarning)\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", "
IBM US EQUITYMSFT US EQUITY
date
2013-12-26185.3537.44
2013-12-27185.0837.29
2013-12-30186.4137.29
2013-12-31187.5737.41
2014-01-02185.5337.16
\n", "
" ], "text/plain": [ " IBM US EQUITY MSFT US EQUITY\n", "date \n", "2013-12-26 185.35 37.44\n", "2013-12-27 185.08 37.29\n", "2013-12-30 186.41 37.29\n", "2013-12-31 187.57 37.41\n", "2014-01-02 185.53 37.16" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# notice only IBM gets warning as MSFT is already cached, so it only retrieves IBM data\n", "h5sids = h5mgr['MSFT US EQUITY', 'IBM US EQUITY']\n", "h5sids.get_historical('PX_LAST', start='1/3/2000', end='1/2/2014').tail()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100 loops, best of 3: 14.8 ms per loop\n" ] } ], "source": [ "# not perfect as it retrieves for each security and then concats BUT better than roundtrip to bloomberg plus consistency added for free\n", "%timeit h5sids.get_historical('PX_LAST', start='1/3/2000', end='1/2/2014')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }