使用tia.bbg.datamgr(Python 2.7)获取数据时'只能加入iterable'

时间:2017-07-27 11:49:06

标签: python numpy data-management

我正在编写一个脚本,使用TIA工具包从Bloomberg获取数据。我正在尝试将PX_VALUEstart的日期stocks放在dict1的字典中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 ,以便我可以稍后操纵这些值。 到目前为止,这是我的脚本没有计算:

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
>>> 

这是实际的输出:

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

我还编写了一个适用于计算的1股权的脚本:

<router-outlet></router-outlet>
<router-outlet name="modal"></router-outlet>

1 个答案:

答案 0 :(得分:0)

您的单一安全解决方案可以做到这一点:

eqt = mgr['GOOGL US Equity']

但您的多安全解决方案(实际上)确实如此:

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

现在,我显然无法在没有Bloomberg安装的情况下对此进行测试,但从错误消息中可以清楚地看出问题出在eqt上。您是否100%确定可以将BBG ID元组作为dm.BbgDataManager()的关键字传递给您?你得到的结果表明你不能。

按照您的工作单一安全解决方案的路线,但循环浏览感兴趣的股票:

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