matthewgilbert / pdblp

pandas wrapper for Bloomberg Open API
MIT License
241 stars 67 forks source link

Unknown security from a list of valid securities #31

Closed gflores87 closed 6 years ago

gflores87 commented 6 years ago

I'm querying ten tickers using con.ref(_tickers, "PX_MID") However, one of them refers to an option whose strike is so large that there is no trading and thus, there is no such ticker in bloomberg. The way pdblp works is that if I query 9 valid tickers and one invalid ticker then my entire execution stops. How can I handle the code such that execution continues? Is is possible to just ignore the invalid ticker?

matthewgilbert commented 6 years ago

Currently passing non valid tickers results in errors. This is discussed in #13 and a result of changes in 659c0d2a8af7f8d24cf53adeb5f3790774160cdc.

A work around for this would be to drop erroneous tickers, which you could parse from the error message. This is a hacky work around that wouldn't be very efficient on your data caps, but something like this would work.

import pdblp

con = pdblp.BCon()
con.start()
tickers = ["EJ8189684 Corp", "not_a_ticker", "also_not_a_ticker"]

while True:
    try:
        df = con.ref(tickers, ["MATURITY", "NXT_CALL_DT"])
        break
    except ValueError as e:
        problem_ticker = e.args[0].split()[-1]
        print("Removing {0} from {1}".format(problem_ticker, tickers))
        tickers = list(set(tickers).difference([problem_ticker]))

What is the use case that leads to you not knowing the tickers in advance?

gflores87 commented 6 years ago

Thank you for the workaround! Agree, there seems to not be a pretty solution to this issue. The cause is that I'm querying option tickers. The strike is a function of certain market values which in some special circumstances may produce a strike that is so out of the money that there is no market data and hence the error. The only way to know in advance would be to query the full option chain and extract the largest and smallest strikes and compare my list of strikes to such values. Seems like that is the way to go given the alternatives.

Again, thank you for your help

pepicello commented 5 years ago

What is the use case that leads to you not knowing the tickers in advance?

IMHO it would be great to have such an option on tickers. There are a few use case I can think of where tickers are not known in advance:

wochner commented 4 years ago

Could this be implemented with an optional parameter similar to how it was implemented here ?

wochner commented 4 years ago

Something along the lines of the below would be nice in the bloomberg query functions and a parameter that would enable it and automatically "ignore" security errors.

import pdblp
import pandas as pd

con = pdblp.BCon()
con.start()
tickers = ["EJ8189684 Corp", "not_a_ticker", "also_not_a_ticker"]

df_out = pd.DataFrame()
L = []

for i in tickers:
    try:
        df = con.ref(i, ["MATURITY", "NXT_CALL_DT"])
        df_out = df_out.append(df)
    except ValueError as e:
        problem_ticker = e.args[0].split()[-1]
        L.append(problem_ticker)

problem_tickers = pd.DataFrame(L, columns=['TICKER']) 

I agree with @pepicello there are many use cases were Tickers are changed or get obsolete, especially in the fixed income world.

matthewgilbert commented 4 years ago

I generally don't like the idea of just returning NaN's for arbitrary tickers, with some exceptions as discussed in https://github.com/matthewgilbert/pdblp/issues/13. This becomes quite challenging to do in a consistent way across ref / bdh. If you want to query for valid tickers I think using the //blp/instruments service and writing a instrumentListRequest would be cleaner.