Search code examples
pythonnumpydata-management

'can only join iterable' when fetching data using tia.bbg.datamgr (Python 2.7)


I'm writing a script that fetches data from Bloomberg using the TIA toolkit. I'm trying to place the PX_VALUE from the start date for each equity in stocks in a dictionary called dict1 so that i can manipulate those values later. Here is my script so far without the calculations:

from __future__ import division
import numpy as np
import pandas as pd
import datetime
import tia
import tia.bbg.datamgr as dm
from operator import itemgetter

start = datetime.date(2017, 1, 3)
end = datetime.date(2017, 7, 25)

diffdays = ((end - start).days)/365
resolution = 0.01
diff2dp = int(np.round(diffdays/resolution))*resolution
diff = 1/diff2dp

dict1 = {}

stocks = ('GOOGL US Equity','MSFT US Equity', 'IBM US Equity')
mgr = dm.BbgDataManager()
eqt = mgr[stocks]

for eq in eqt:
    df = eq.get_historical(['PX_LAST'], start, end)
    k = df.loc[start]['PX_LAST']
    dict1 [stocks] = k

print dict1

And here is the actual Output:

Traceback (most recent call last):
  File "C:\Users\bloomberg\Desktop\examples\CAGR by LouisV2 BROKEN.py", line 23, in <module>
    for eq in eqt:
  File "C:\Python27\lib\site-packages\tia\bbg\datamgr.py", line 94, in __getitem__
    return self.get_attributes(flds, **self.overrides)
  File "C:\Python27\lib\site-packages\tia\bbg\datamgr.py", line 90, in get_attributes
    frame = self.mgr.get_attributes(self.sids, flds, **overrides)
  File "C:\Python27\lib\site-packages\tia\bbg\datamgr.py", line 148, in get_attributes
    return self.terminal.get_reference_data(sids, flds, **overrides).as_frame()
  File "C:\Python27\lib\site-packages\tia\bbg\v3api.py", line 745, in get_reference_data
    return self.execute(req)
  File "C:\Python27\lib\site-packages\tia\bbg\v3api.py", line 711, in execute
    self.logger.info('executing request: %s' % repr(request))
  File "C:\Python27\lib\site-packages\tia\bbg\v3api.py", line 432, in __repr__
    fields=','.join(self.fields),
TypeError: can only join an iterable
>>> 

I have also written a script that works for 1 equity with the calculations:

from __future__ import division
import numpy as np
import pandas as pd
import datetime
import tia
import tia.bbg.datamgr as dm

start = datetime.date(2017, 1, 3)
end = datetime.date(2017, 7, 25)

diffdays = ((end - start).days)/365
resolution = 0.01
diff2dp = int(np.round(diffdays/resolution))*resolution
diff = 1/diff2dp

mgr = dm.BbgDataManager()
eqt = mgr['GOOGL US Equity']
datafetch = eqt.get_historical(['PX_LAST'], start, end)
calc1 = ((datafetch.loc[end]['PX_LAST'])/(datafetch.loc[start]['PX_LAST']))
calc2 = (pow(calc1,diff))-1
calc22dp = int(np.round(calc2/resolution))*resolution

print calc22dp

Solution

  • Your single-security solution does this:

    eqt = mgr['GOOGL US Equity']
    

    but your multiple-security solution does (in effect) this:

    eqt = mgr[('GOOGL US Equity','MSFT US Equity', 'IBM US Equity')]
    

    Now, I obviously cannot test this without a Bloomberg installation, but it is clear from the error message that your problem is with eqt. Are you 100% sure you can pass a tuple of BBG ids as a key to dm.BbgDataManager()? The results you are getting suggests that you can't.

    Follow the line of your working one-security solution, but looping through the stocks of interest:

    stocks = ('GOOGL US Equity','MSFT US Equity', 'IBM US Equity')  
    mgr = dm.BbgDataManager()
    for stock in stocks: 
        eqt = mgr[stock]
        datafetch = eqt.get_historical(['PX_LAST'], start, end)
        calc1 = ((datafetch.loc[end]['PX_LAST'])/(datafetch.loc[start]['PX_LAST']))
        calc2 = (pow(calc1,diff))-1
        calc22dp = int(np.round(calc2/resolution))*resolution
        print calc22dp