def examples():
    """
    examples of how to get basic data from bbg
    """

    # get some data for a single name
    x = blp.bdp('BDEV LN Equity', 'px_last')
    print(x)
    print('the type of x', type(x))
    print('the value of x:', x.iloc[0]['px_last'])


    # get multiple data for a single name
    y = blp.bdp('BDEV LN Equity', flds=['px_bid', 'px_ask'])
    print(y)


    # get multiple data for multiple names
    z = blp.bdp(tickers=['BDEV LN Equity', 'BARC LN Equity'], flds=['px_bid', 'px_ask'])
    print(z)
    print('here is the bdev ask >>>', z.loc['BDEV LN Equity','px_ask'])

    # get multiple data for multiple names
    z = blp.bdp(tickers=['XS2011260705 Corp'], flds=['z_sprd_mid', 'z_sprd_mid'])
    print(z)
    print('here is the bdev ask >>>', z.loc['XS2011260705 Corp','z_sprd_mid'])
def get_data(ticker, fields, override, legend_labels, sort):
    """
    Downloads data using blp.bpd
    :param legend_labels: list[string], your name for fields
    :param ticker: str/list, ticker(s) name(s)
    :param fields: str/list, field(s) name(s)
    :param override: dictionary, override values
    :param sort: string, sort value if needed
    :return: data to plot
    """
    # print(override)
    # print(fields, override[0], legend_labels)
    data = pd.DataFrame()
    for idx, field in enumerate(fields):
        # print(idx, field)
        try:
            temp = blp.bdp(tickers=ticker, flds=field, **override[idx])
        except Exception as e:
            temp = blp.bdp(tickers=ticker, flds=field)
        # print(temp)
        data[legend_labels[idx]] = temp

    if sort == 'a':
        data.sort_values(by=data.columns[0], axis=0, inplace=True, ascending=True)
    if sort == 'd':
        data.sort_values(by=data.columns[0], axis=0, inplace=True, ascending=False)

    return data
Example #3
0
def examples():
    """
    examples of how to get basic data from bbg
    """

    # get some data for a single name
    x = blp.bdp('BDEV LN Equity', 'px_last')
    print(x)
    print('the type of x', type(x))
    print('the value of x:', x.iloc[0]['px_last'])

    # get multiple data for a single name
    y = blp.bdp('BDEV LN Equity', flds=['px_bid', 'px_ask'])
    print(y)

    # get multiple data for multiple names
    z = blp.bdp(tickers=['BDEV LN Equity', 'BARC LN Equity'],
                flds=['px_bid', 'px_ask'])
    print(z)
    print('here is the bdev ask >>>', z.loc['BDEV LN Equity', 'px_ask'])

    # get history data for a single name
    print('getting history...')
    todaydate = datetime.datetime.today()
    historicDate = todaydate - datetime.timedelta(3 * 365)
    print(todaydate, historicDate)
    x = blp.bdh('BDEV LN Equity',
                flds='px_last',
                start_date=historicDate,
                end_date='today')

    print(x.head())
    print(x.tail())
Example #4
0
def getListDataFromBbg(securtiyList, fieldList):
    ''' 
    gets data from bloomberg using a while loop until completion

        inputs:
            securtiyList - list of tickers to obtain
            fieldList - the fields requested for each ticker

        output:
            resultsOfDataReq - resulting pandas array of the data that was requested

    '''
    # get the data (bdp returns a dataframe)
    resultsOfDataReq = blp.bdp(tickers=securtiyList, flds=fieldList)

    #pre-loop initialization
    keepLooping = True
    loopingCounter = 0
    while keepLooping:

        # check which items are missing
        listError = list(
            set(securtiyList) - set(resultsOfDataReq.index.values))

        if len(listError) == 0:
            # data collection process finished
            keepLooping = False
            break
        else:
            print(len(listError), 'of', len(securtiyList), 'remaining',
                  ': loop', loopingCounter)
            loopingCounter += 1
            if loopingCounter > 10:
                # more than 10 tries at data collection
                keepLooping = False
                break

        # get and append more data
        try:
            nextChunk = blp.bdp(tickers=listError, flds=fieldList)
            resultsOfDataReq = resultsOfDataReq.append(nextChunk)
        except:
            print('no data to add')

        # remove duplicates
        resultsOfDataReq = resultsOfDataReq.drop_duplicates()

    return resultsOfDataReq
Example #5
0
def get_data(ticker, fields, override):
    """
    Downloads data using blp.bpd
    :param ticker: str/list, ticker(s) name(s)
    :param fields: str/list, field(s) name(s)
    :param override: dictionary, override values
    :return: data to plot
    """
    data = blp.bdp(tickers=ticker, flds=fields, **override)
    return data
Example #6
0
def getListDataFromBbg(securtiyList, fieldList):
    ''' 
    gets data from bloomberg using a while loop

        inputs:
            securtiyList - list of tickers to obtain
            fieldList - the fields requested for each ticker

        output:
            resultsOfDataReq - resulting pandas array of the data that was requested

    '''
    # create pandas dataframe
    # resultsOfDataReq = pd.Dataframe()
    # get the data
    resultsOfDataReq = blp.bdp(tickers=securtiyList, flds=fieldList)

    # check which items are missing
    # listError = list(set(securtiyList)-set(resultsOfDataReq.index.values))
    listError = list(set(securtiyList) - set(resultsOfDataReq.index.values))
    pass
Example #7
0
def settle_dt(cusips, fecha):
    datos = blp.bdp(cusips, ['SETTLE_DT'], USER_LOCAL_TRADE_DATE=fecha)
    return datos
Example #8
0
def bdp(cusips):
    datos = blp.bdp(cusips, ['SECURITY_NAME', 'MATURITY'])
    return datos
Example #9
0
def bloombergAPI_bigData(securtiyList, fieldList, rowChunk):
    ''' 
    if the number of tickers and fields are too large, then this function is used to break up the lists to allow for data connection

        inputs:
            securtiyList - list of tickers to obtain
            fieldList - the fields requested for each ticker
            rowChunk - how many rows to aquire in one batch

        output:
            resultsOfDataReq - resulting pandas array of the data that was requested
    '''

    # split the security list into batches of n rows
    n = rowChunk
    iterationsRequired = int(len(securtiyList) / n)
    remainingBit = len(securtiyList) - iterationsRequired * n

    print(
        f'we will iterate {iterationsRequired} times and {remainingBit} will be left'
    )

    resultsOfDataReq = pd.DataFrame()
    # bulk of data
    for i in range(iterationsRequired):
        print('processing: ', i * n, 'to', (i + 1) * n, 'from the list.')
        nextChunk = blp.bdp(tickers=securtiyList[i * n:(i + 1) * n],
                            flds=fieldList)
        resultsOfDataReq = resultsOfDataReq.append(nextChunk)
        percentDone = round(
            (len(resultsOfDataReq.index) / len(securtiyList)) * 100, 1)
        print('items completed:', len(resultsOfDataReq.index), 'out of',
              len(securtiyList), "=> precent aquired:", percentDone)
        # if i > 2:
        #     print('breaking the loop for testing purpose')
        #     break

    # end stub
    if remainingBit == 0:
        # there is no stub
        pass
    else:
        # process the stub
        print('processing last chuunk: ', iterationsRequired * n, 'to',
              len(securtiyList))
        nextChunk = blp.bdp(tickers=securtiyList[iterationsRequired *
                                                 n:len(securtiyList)],
                            flds=fieldList)
        percentDone = round(
            (len(resultsOfDataReq.index) / len(securtiyList)) * 100, 1)
        print('items completed:', len(resultsOfDataReq.index), 'out of',
              len(securtiyList), "=> precent aquired:", percentDone)

    # compare rows of dataframe to securitiy list to find the missing items
    #print('index column of the pandas list')]
    #print(list(resultsOfDataReq.index.values))
    print('missing items from the securities list')
    listError = list(set(securtiyList) - set(resultsOfDataReq.index.values))
    print(listError)

    return resultsOfDataReq
Example #10
0
def get_bonds():
    """
    function to get bond data from bloomberg using tickers in an excel sheet.

    return:
        data - a pandas data set
    """

    print('getting bond data...')
    # securtiyList = ['US29265WAA62 Corp', 'XS1713463559 Corp', 'XS2000719992 Corp', 'XS0954675129 Corp', 'XS0954675129 Corp']
    fieldList = [
        'ticker', 'coupon', 'nxt_call_dt', 'final_maturity', 'mty_typ',
        'px_mid', 'z_sprd_mid', 'yas_ispread', 'yas_bond_yld', 'yas_risk',
        'crncy', 'payment_rank', 'industry_sector', 'rtg_moody', 'rtg_sp'
    ]

    # the script fis here
    dir_path = os.path.dirname(os.path.realpath(__file__))

    # get items from sheet (in the same folder)
    fileToGet = 'hybridSecurityList.xlsx'
    # join file and correct folder
    fileToGet = os.path.join(dir_path, fileToGet)
    secListXlsx = pd.ExcelFile(fileToGet)

    # the names of the available sheets
    print('getting security list from:', secListXlsx.sheet_names)
    df = pd.DataFrame(secListXlsx.parse('tickers'))
    print('summary of the data')
    print(df.info)

    # put all isin's in a list
    isin = df.iloc[:, 1].tolist()
    securtiyList = [x + " Corp" for x in isin]

    # make the lists unique (and keep the order)
    securtiyList = list(collections.OrderedDict.fromkeys(securtiyList))

    # get the data from bloomberg
    print('getting data from bbg')
    if len(securtiyList) > 49:
        # there are lots of securities
        print(f'lots of data requested: {len(securtiyList)} items requested')
        reducedListSize = input(
            f'reduce list size to value or press "x" to use {len(securtiyList)}  >>>'
        )
        if reducedListSize == 'x':
            # keep the same size
            reducedListSize = len(securtiyList)
        else:
            # reeduce the list size
            reducedListSize = int(reducedListSize)

        # set the reduced list to size selected
        reducedList = securtiyList[0:reducedListSize]
        # run code on the smaller list
        bondData = bloombergAPI_bigData(reducedList, fieldList, 10)
    else:
        bondData = blp.bdp(tickers=securtiyList, flds=fieldList)

    #print('number of columns:', data.head())
    print('data is fetched:')
    print(bondData.info)

    return bondData
Example #11
0
con.start()
today = datetime.datetime.now()
s_date = '2011-01-01'
e_date = today
#read ticker list
names = ['Tickers']
tix = pd.read_csv("tickers.csv", names=names).values.tolist()
# create a global df for all prices. index needs to be created for begining of the month
global_index = pd.date_range(start=s_date, end=e_date, freq='W-FRI')
global_df = pd.DataFrame(index=global_index)

# %% bloomberg data requests
#create loop here for tickers in tix
for tt in tix:
    test_temp = blp.bdh(tickers=tt,
                        flds=['last_price'],
                        start_date=s_date,
                        end_date=e_date,
                        Quote='G',
                        Per='W',
                        Fill='B',
                        Days='W')
    #get the name for dataframe header
    tick_name = blp.bdp(tickers=tt, flds=['Security_Name'])
    header = str(tt).strip('[]')
    test_temp.columns = [header]
    #merge current df with global df
    global_df = global_df.join(test_temp, how='outer')

global_df.to_csv('prices.csv')
Example #12
0
 def init_ref(self, ticker, fields):
     self.tick = ticker
     self.ref_data = blp.bdp(tickers=ticker, flds=fields)