def get_ratings(symbol, info=None): """Returns the ratings for a stock, including the number of buy, hold, and sell ratings. :param symbol: The stock ticker. :type symbol: str :param info: Will data_filter the results to contain a dictionary of values that correspond to the key that matches info. \ Possible values are summary, ratings, and instrument_id :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 contain the values that correspond to the keyword that matches info. In this case, \ the value will also be a dictionary. """ try: symbol = symbol.upper().strip() except AttributeError as message: print(message) return None url = urls.ratings(symbol) data = helper.request_get(url) if len(data['ratings']) == 0: return data else: for item in data['ratings']: old_text = item['text'] item['text'] = old_text.encode('UTF-8') return helper.data_filter(data, info)
def get_option_instrument_data(symbol, expiration_date, strike, option_type, info=None): """Returns the option instrument data for the stock option. :param symbol: The ticker of the stock. :type symbol: str :param expiration_date: Represents the expiration date in the format YYYY-MM-DD. :type expiration_date: str :param strike: Represents the price of the option. :type strike: str :param option_type: Can be either 'call' or 'put'. :type option_type: str :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a dictionary of key/value pairs for the stock. \ If info parameter is provided, the value of the key that matches info is extracted. """ try: symbol = symbol.upper().strip() option_type = option_type.lower().strip() except AttributeError as message: print(message) return [None] option_id = helper.id_for_option(symbol, expiration_date, strike, option_type) url = urls.option_instruments(option_id) data = helper.request_get(url) return helper.data_filter(data, info)
def get_quotes(input_symbols, info=None): """Takes any number of stock tickers and returns information pertaining to its price. :param input_symbols: May be a single stock ticker or a list of stock tickers. :type input_symbols: str or list :param info: Will data_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(input_symbols) url = urls.quotes() payload = {'symbols': ','.join(symbols)} data = helper.request_get(url, 'results', payload) if data is None or data == [None]: return data for count, item in enumerate(data): if item is None: print(helper.error_ticker_does_not_exist(symbols[count])) data = [item for item in data if item is not None] return helper.data_filter(data, info)
def get_instruments_by_symbols(input_symbols, 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 input_symbols: May be a single stock ticker or a list of stock tickers. :type input_symbols: str or list :param info: Will data_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(input_symbols) url = urls.instruments() data = [] for item in symbols: payload = {'symbol': item} item_data = helper.request_get(url, 'indexzero', payload) if item_data: data.append(item_data) else: print(helper.error_ticker_does_not_exist(item)) return helper.data_filter(data, info)
def get_top_movers(direction, info=None): """Returns a list of the top movers up or down for the day. :param direction: The direction of movement either 'up' or 'down' :type direction: str :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each mover. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ try: direction = direction.lower().strip() except AttributeError as message: print(message) return None if direction != 'up' and direction != 'down': print('Error: direction must be "up" or "down"') return [None] url = urls.movers() payload = {'direction': direction} data = helper.request_get(url, 'pagination', payload) return helper.data_filter(data, info)
def get_name_by_url(url): """Returns the name of a stock from the instrument url. 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 as a string. :type url: str :returns: Returns the simple name of the stock. If the simple name does not exist then returns the full name. """ data = helper.request_get(url) if not data: return None # If stock doesn't have a simple name attribute then get the full name. filtered = helper.data_filter(data, info='simple_name') if not filtered or filtered == "": filtered = helper.data_filter(data, info='name') return filtered
def find_options_for_list_of_stocks_by_expiration_date(input_symbols, expiration_date, option_type='both', info=None): """Returns a list of all the option orders that match the seach parameters :param input_symbols: May be a single stock ticker or a list of stock tickers. :type input_symbols: str or list :param expiration_date: Represents the expiration date in the format YYYY-MM-DD. :type expiration_date: str :param option_type: Can be either 'call' or 'put' or leave blank to get both. :type option_type: Optional[str] :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for all options of the stock that match the search parameters. \ If info parameter is provided, a list of strings is returned where the strings are the value of the key that matches info. """ symbols = helper.inputs_to_set(input_symbols) try: option_type = option_type.lower().strip() except AttributeError as message: print(message) return [None] data = [] url = urls.option_instruments() for symbol in symbols: if option_type == 'put' or option_type == 'call': payload = { 'chain_id': helper.id_for_chain(symbol), 'expiration_date': expiration_date, 'state': 'active', 'tradability': 'tradable', 'rhs_tradability': 'tradable', 'type': option_type } else: payload = { 'chain_id': helper.id_for_chain(symbol), 'expiration_date': expiration_date, 'state': 'active', 'tradability': 'tradable', 'rhs_tradability': 'tradable' } other_data = helper.request_get(url, 'pagination', payload) for item in other_data: if item['expiration_date'] == expiration_date and item[ 'tradability'] == 'tradable': data.append(item) for item in data: market_data = get_option_market_data_by_id(item['id']) item.update(market_data) return helper.data_filter(data, info)
def get_linked_bank_accounts(info=None): """Returns all linked bank accounts. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each bank. """ url = urls.linked() data = helper.request_get(url, 'results') return helper.data_filter(data, info)
def get_all_watchlists(info=None): """Returns a list of all watchlists that have been created. Everone has a 'default' watchlist. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of the watchlists. Keywords are 'url', 'user', and 'name'. """ url = urls.watchlists() data = helper.request_get(url, 'pagination') return helper.data_filter(data, info)
def get_all_orders(info=None): """Returns a list of all the orders that have been processed for the account. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each order. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ url = urls.orders() data = helper.request_get(url, 'pagination') return helper.data_filter(data, info)
def get_aggregate_positions(info=None): """Collapses all option orders for a stock into a single dictionary. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each order. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ url = urls.aggregate() data = helper.request_get(url, 'pagination') return helper.data_filter(data, info)
def get_bank_transfers(info=None): """Returns all bank transfers made for the account. :param info: Will data_filter the results to get a specific value. 'direction' gives if it was deposit or withdrawl. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each transfer. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ url = urls.banktransfers() data = helper.request_get(url, 'pagination') return helper.data_filter(data, info)
def get_crypto_currency_pairs(info=None): """Gets a list of all the cypto currencies that you can trade :param info: Will data_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.crypto_currency_pairs() data = helper.request_get(url, 'results') return helper.data_filter(data, info)
def get_referrals(info=None): """Returns a list of referrals. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each referral. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ url = urls.referral() data = helper.request_get(url, 'pagination') return helper.data_filter(data, info)
def load_security_profile(info=None): """Gets the information associated with the security profile. :param info: The name of the key whose value is to be returned from the function. :type info: Optional[str] :returns: The function returns a dictionary of key/value pairs. \ If a string is passed in to the info parameter, then the function will return \ a string corresponding to the value of the key whose name matches the info parameter. """ url = urls.security_profile() data = helper.request_get(url) return helper.data_filter(data, info)
def get_documents(info=None): """Returns a list of documents that have been released by Robinhood to the account. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each document. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ url = urls.documents() data = helper.request_get(url, 'pagination') return helper.data_filter(data, info)
def get_day_trades(info=None): """Returns recent day trades. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each day trade. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ account = profiles.load_account_profile('account_number') url = urls.daytrades(account) data = helper.request_get(url, 'pagination') return helper.data_filter(data, info)
def get_watchlist_by_name(name='Default', info=None): """Returns a list of information related to the stocks in a single watchlist. :param name: The name of the watchlist to get data from. :type name: Optional[str] :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries that contain the instrument urls and a url that references itself. """ url = urls.watchlists(name) data = helper.request_get(url, 'pagination') return helper.data_filter(data, info)
def load_basic_profile(info=None): """Gets the information associated with the personal profile, such as phone number, city, marital status, and date of birth. :param info: The name of the key whose value is to be returned from the function. :type info: Optional[str] :returns: The function returns a dictionary of key/value pairs. If a string \ is passed in to the info parameter, then the function will return a string \ corresponding to the value of the key whose name matches the info parameter. """ url = urls.basic_profile() data = helper.request_get(url) return helper.data_filter(data, info)
def load_portfolio_profile(info=None): """Gets the information associated with the portfolios profile, such as withdrawable amount, market value of account, and excess margin. :param info: The name of the key whose value is to be returned from the function. :type info: Optional[str] :returns: The function returns a dictionary of key/value pairs. \ If a string is passed in to the info parameter, then the function will return \ a string corresponding to the value of the key whose name matches the info parameter. """ url = urls.portfolio_profile() data = helper.request_get(url, 'indexzero') return helper.data_filter(data, info)
def load_account_profile(info=None): """Gets the information associated with the accounts profile,including day trading information and cash being held by Robinhood. :param info: The name of the key whose value is to be returned from the function. :type info: Optional[str] :returns: The function returns a dictionary of key/value pairs. \ If a string is passed in to the info parameter, then the function will return \ a string corresponding to the value of the key whose name matches the info parameter. """ url = urls.account_profile() data = helper.request_get(url, 'indexzero') return helper.data_filter(data, info)
def get_dividends(info=None): """Returns a list of dividend trasactions that include information such as the percentage rate, amount, shares of held stock, and date paid. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each divident payment. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ url = urls.dividends() data = helper.request_get(url, 'pagination') return helper.data_filter(data, info)
def get_bank_account_info(_id, info=None): """Returns a single dictionary of bank information :param _id: The bank id. :type _id: str :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a dictinoary of key/value pairs for the bank. If info parameter is provided, \ the value of the key that matches info is extracted. """ url = urls.linked(_id) data = helper.request_get(url) return helper.data_filter(data, info)
def get_current_positions(info=None): """Returns a list of stocks/options that are currently held. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each ticker. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ url = urls.positions() payload = {'nonzero': 'true'} data = helper.request_get(url, 'pagination', payload) return helper.data_filter(data, info)
def get_crypto_quote_from_id(_id, info=None): """Gets information about a crypto including low price, high price, and open price. Uses the id instead of crypto ticker. :param _id: The id of a crypto. :type _id: str :param info: Will data_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.crypto_quote(_id) data = helper.request_get(url) return helper.data_filter(data, info)
def get_option_instrument_data_by_id(_id, info=None): """Returns the option instrument information. :param _id: The id of the stock. :type _id: str :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a dictionary of key/value pairs for the stock. \ If info parameter is provided, the value of the key that matches info is extracted. """ url = urls.option_instruments(_id) data = helper.request_get(url) return helper.data_filter(data, info)
def get_stock_quote_by_id(stock_id, info=None): """ Represents basic stock quote information :param stock_id: robinhood stock id :type stock_id: str :param info: Will data_filter the results to get a specific value. Possible options are url, instrument, execution_date, divsor, and multiplier. :type info: Optional[str] :return: """ url = urls.marketdata_quotes(stock_id) data = helper.request_get(url) return helper.data_filter(data, info)
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: return None # If stock doesn't have a simple name attribute then get the full name. filtered = helper.data_filter(data, info='simple_name') if not filtered or filtered == "": filtered = helper.data_filter(data, info='name') return filtered
def get_all_open_option_orders(info=None): """Returns a list of all the orders that are currently open. :param info: Will data_filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each order. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ url = urls.option_orders() data = helper.request_get(url, 'pagination') data = [item for item in data if item['cancel_url'] is not None] return helper.data_filter(data, info)
def get_instrument_by_url(url, 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(input_symbols,info=None) :type url: str :param info: Will data_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. """ data = helper.request_get(url, 'regular') return helper.data_filter(data, info)