예제 #1
0
def get_instruments_by_symbols(inputSymbols, info=None):
    """Takes any number of stock tickers and returns information held by the market
    such as ticker name, bloomberg id, and listing date.

    :param inputSymbols: May be a single stock ticker or a list of stock tickers.
    :type inputSymbols: str or list
    :param info: Will filter the results to have a list of the values that correspond to key that matches info.
    :type info: Optional[str]
    :returns: If info parameter is left as None then the list will contain a dictionary of key/value pairs for each ticker. \
    Otherwise, it will be a list of strings where the strings are the values of the key that corresponds to info.

    """
    symbols = helper.inputs_to_set(inputSymbols)
    url = urls.instruments()
    data = []
    for item in symbols:
        payload = {'symbol': item}
        itemData = helper.request_get(url, 'indexzero', payload)

        if itemData:
            data.append(itemData)
        else:
            print(helper.error_ticker_does_not_exist(item))

    return (helper.filter(data, info))
예제 #2
0
def get_instruments_by_symbols(inputSymbols, info=None):
    """Takes any number of stock tickers and returns information held by the market
    such as ticker name, bloomberg id, and listing date.

    :param inputSymbols: May be a single stock ticker or a list of stock tickers.
    :type inputSymbols: str or list
    :param info: Will filter the results to have a list of the values that correspond to key that matches info.
    :type info: Optional[str]
    :returns: [list] If info parameter is left as None then the list will a dictionary of key/value pairs for each ticker. \
    Otherwise, it will be a list of strings where the strings are the values of the key that corresponds to info.
    :Dictionary Keys: * id
                      * url
                      * quote
                      * fundamentals
                      * splits
                      * state
                      * market
                      * simple_name
                      * name
                      * tradeable
                      * tradability
                      * symbol
                      * bloomberg_unique
                      * margin_initial_ratio
                      * maintenance_ratio
                      * country
                      * day_trade_ratio
                      * list_date
                      * min_tick_size
                      * type
                      * tradable_chain_id
                      * rhs_tradability
                      * fractional_tradability
                      * default_collar_fraction

    """
    symbols = helper.inputs_to_set(inputSymbols)
    url = urls.instruments()
    data = []
    for item in symbols:
        payload = {'symbol': item}
        itemData = helper.request_get(url, 'indexzero', payload)

        if itemData:
            data.append(itemData)
        else:
            print(helper.error_ticker_does_not_exist(item),
                  file=helper.get_output())

    return (helper.filter(data, info))
예제 #3
0
def get_instrument_by_id(id, info=None):
    """Takes a single url for the stock. Should be located at ``https://api.robinhood.com/instruments/<id>`` where <id> is the
    id of the stock.

    :param url: The url of the stock. Can be found in several locations including \
    in the dictionary returned from get_instruments_by_symbols(inputSymbols,info=None)
    :type url: str
    :param info: Will filter the results to have a list of the values that correspond to key that matches info.
    :type info: Optional[str]
    :returns: If info parameter is left as None then the list will contain a dictionary of key/value pairs for each ticker. \
    Otherwise, it will be a list of strings where the strings are the values of the key that corresponds to info.

    """
    url = urls.instruments() + id
    data = helper.request_get(url, 'regular')

    return (helper.filter(data, info))
예제 #4
0
def find_instrument_data(query):
    """Will search for stocks that contain the query keyword and return the instrument data.

    :param query: The keyword to search for.
    :type query: str
    :returns: [list] Returns a list of dictionaries that contain the instrument data for each stock that matches the query.
    :Dictionary Keys: * id
                      * url
                      * quote
                      * fundamentals
                      * splits
                      * state
                      * market
                      * simple_name
                      * name
                      * tradeable
                      * tradability
                      * symbol
                      * bloomberg_unique
                      * margin_initial_ratio
                      * maintenance_ratio
                      * country
                      * day_trade_ratio
                      * list_date
                      * min_tick_size
                      * type
                      * tradable_chain_id
                      * rhs_tradability
                      * fractional_tradability
                      * default_collar_fraction

    """
    url = urls.instruments()
    payload = {'query': query}

    data = helper.request_get(url, 'pagination', payload)

    if len(data) == 0:
        print('No results found for that keyword', file=helper.get_output())
        return ([None])
    else:
        print('Found ' + str(len(data)) + ' results', file=helper.get_output())
        return (data)
예제 #5
0
def find_instrument_data(query):
    """Will search for stocks that contain the query keyword and return the instrument data.

    :param query: The keyword to search for.
    :type query: str
    :returns: Returns a list of dictionaries that contain the instrument data for each stock that matches the query.

    """
    url = urls.instruments()
    payload = {'query': query}

    data = helper.request_get(url, 'pagination', payload)

    if len(data) == 0:
        print('No results found for that keyword')
        return ([None])
    else:
        print('Found ' + str(len(data)) + ' results')
        return (data)
예제 #6
0
def get_name_by_symbol(symbol):
    """Returns the name of a stock from the stock ticker.

    :param symbol: The ticker of the stock as a string.
    :type symbol: str
    :returns: Returns the simple name of the stock. If the simple name does not exist then returns the full name.

    """
    try:
        symbol = symbol.upper().strip()
    except AttributeError as message:
        print(message)
        return None

    url = urls.instruments()
    payload = {'symbol': symbol}
    data = helper.request_get(url, 'indexzero', payload)

    if not data['simple_name']:
        return data['name']
    else:
        return data['simple_name']
예제 #7
0
def get_name_by_symbol(symbol):
    """Returns the name of a stock from the stock ticker.

    :param symbol: The ticker of the stock as a string.
    :type symbol: str
    :returns: [str] Returns the simple name of the stock. If the simple name does not exist then returns the full name.

    """
    try:
        symbol = symbol.upper().strip()
    except AttributeError as message:
        print(message, file=helper.get_output())
        return None

    url = urls.instruments()
    payload = {'symbol': symbol}
    data = helper.request_get(url, 'indexzero', payload)
    if not data:
        return (None)
    # If stock doesn't have a simple name attribute then get the full name.
    filter = helper.filter(data, info='simple_name')
    if not filter or filter == "":
        filter = helper.filter(data, info='name')
    return (filter)
예제 #8
0
def get_instruments():
    """ Get a list of all tradeable instruments """
    instruments = []
    count = 0

    url = urls.instruments()
    results = helper.request_get(url)
    if isinstance(results, list):
        print("API Request Error. Returning...")
        return {}
    for instrument in results["results"]:
        if instrument["tradeable"] and instrument["tradability"] == "tradable":
            instruments.append({
                "symbol": instrument["symbol"],
                "id": instrument["id"]
            })
            count += 1

    while results.get("next"):
        url = results["next"]
        results = helper.request_get(url)
        if isinstance(results, list):
            print("API Request Error. Returning...")
            return {}
        for instrument in results["results"]:
            if instrument["tradeable"] and instrument[
                    "tradability"] == "tradable":
                instruments.append({
                    "symbol": instrument["symbol"],
                    "id": instrument["id"]
                })
                count += 1

    print("Successfully fetched {} instruments.".format(count))

    return instruments