#!/usr/bin/env python # coding: utf-8 # # Example using the data manager classes # This notebook shows how to use the data manager framework for simpler API usage and for caching capabilities. # # Please note that in order to request bloomberg fields using property access, it must be CAPITALIZED. (sid.PX_AST NOT sid.px_last) # In[1]: import pandas as pd import tia.bbg.datamgr as dm # ## Single Security Accessor # In[2]: # create a DataManager for simpler api access mgr = dm.BbgDataManager() # retrieve a single security accessor from the manager msft = mgr['MSFT US EQUITY'] # In[3]: # Can now access any Bloomberg field (as long as it is upper case) msft.PX_LAST, msft.PX_OPEN # In[4]: # Access multiple fields at the same time msft['PX_LAST', 'PX_OPEN'] # In[5]: # OR pass an array msft[['PX_LAST', 'PX_OPEN']] # In[6]: # Have the manager default to returning a frame instead of values mgr.sid_result_mode = 'frame' msft.PX_LAST # In[7]: # multiple fields returned as data frame msft[['PX_LAST', 'PX_OPEN']] # In[8]: # Retrieve historical data msft.get_historical(['PX_OPEN', 'PX_HIGH', 'PX_LOW', 'PX_LAST'], '1/1/2014', '1/12/2014').head() # ## Multi-security accessor # In[9]: sids = mgr['MSFT US EQUITY', 'IBM US EQUITY', 'CSCO US EQUITY'] sids.PX_LAST # In[10]: sids.get_historical('PX_LAST', '1/1/2014', '11/12/2014').head() # In[11]: sids.get_historical(['PX_OPEN', 'PX_LAST'], '1/1/2014', '11/12/2014').head() # ## Caching # In[12]: # # ability to cache requests in memory or in h5 file # ms = dm.MemoryStorage() cmgr = dm.CachedDataManager(mgr, ms, pd.datetime.now()) # In[13]: cmsft = cmgr['MSFT US EQUITY'] cmsft.PX_LAST # In[14]: get_ipython().run_line_magic('timeit', 'msft.PX_LAST') # In[15]: get_ipython().run_line_magic('timeit', 'cmsft.PX_LAST') # In[16]: csids = cmgr['MSFT US EQUITY', 'IBM US EQUITY'] sids = mgr['MSFT US EQUITY', 'IBM US EQUITY'] # In[17]: get_ipython().run_line_magic('timeit', "sids.get_historical('PX_LAST', start='1/3/2000', end='1/3/2014').head()") # In[18]: get_ipython().run_line_magic('timeit', "csids.get_historical('PX_LAST', start='1/3/2000', end='1/3/2014').head()") # In[19]: # # HD Storage # - note after executing the warning from hf api. I decided to leave blanks instead of replacing # import tempfile fh, fp = tempfile.mkstemp() h5storage = dm.HDFStorage(fp) # Can set compression level for smaller files h5mgr = dm.CachedDataManager(mgr, h5storage, pd.datetime.now()) h5msft = h5mgr['MSFT US EQUITY'] h5msft.PX_LAST # In[20]: # Notice no warning as it is taken from cache h5msft.PX_LAST # In[21]: h5msft.get_historical('PX_LAST', start='1/2/2000', end='1/2/2014').head() # In[22]: get_ipython().run_line_magic('timeit', "h5msft.get_historical('PX_LAST', start='1/2/2000', end='1/2/2014')") # In[23]: # notice only IBM gets warning as MSFT is already cached, so it only retrieves IBM data h5sids = h5mgr['MSFT US EQUITY', 'IBM US EQUITY'] h5sids.get_historical('PX_LAST', start='1/3/2000', end='1/2/2014').tail() # In[24]: # not perfect as it retrieves for each security and then concats BUT better than roundtrip to bloomberg plus consistency added for free get_ipython().run_line_magic('timeit', "h5sids.get_historical('PX_LAST', start='1/3/2000', end='1/2/2014')")