def fundamentals_from_fids(fids_list, sandbox=False): if sandbox == False: intrinio_sdk.ApiClient().configuration.api_key['api_key'] = config('INTRINIO_KEY') security_api = intrinio_sdk.SecurityApi() elif sandbox == True: intrinio_sdk.ApiClient().configuration.api_key['api_key'] = config('INTRINIO_SANDBOX_KEY') security_api = intrinio_sdk.SecurityApi() # Initialize api's fapi = intrinio_sdk.FundamentalsApi() fund_dict = {} for id in fids_list: for attempt in range(5): try: fundamentals_ret = fapi.get_fundamental_standardized_financials(id) except: print('Connection error. Retry attempt {}'.format(attempt)) sleep(2) else: break fund_get = fundamentals_ret.standardized_financials_dict fund_info = fundamentals_ret.fundamental_dict funds = {} funds['date'] = fund_info['filing_date'] funds['fiscal_year'] = fund_info['fiscal_year'] funds['quarter'] = fund_info['fiscal_period'] for f in fund_get: funds[f['data_tag']['tag'].lower()] = f['value'] fund_dict[id] = funds return fund_dict
def get_intrinio_fids(tkr_id, after_date, end_date='', sandbox=False): if sandbox == False: intrinio_sdk.ApiClient().configuration.api_key['api_key'] = config('INTRINIO_KEY') security_api = intrinio_sdk.SecurityApi() elif sandbox == True: intrinio_sdk.ApiClient().configuration.api_key['api_key'] = config('INTRINIO_SANDBOX_KEY') security_api = intrinio_sdk.SecurityApi() # Initialize api's capi = intrinio_sdk.CompanyApi() capi_params = { 'identifier' : tkr_id, 'filed_after' : '', 'filed_before' : '', 'reported_only' : False, # 'fiscal_year' : fiscal_year, 'statement_code' : 'income_statement', 'type' : 'QTR', 'start_date' : after_date, 'end_date' : end_date, 'page_size' : 500, 'next_page' : '' } # Set up dictionary to populate with financial report availibility information for attempt in range(5): try: fundamentals = capi.get_company_fundamentals(**capi_params) except: print("There was a server error. Retrying, attempt {}.".format(attempt)) sleep(5) else: break # n_results = len(fundamentals.fundamentals) # for i in range(n_results): # if fundamentals.fundamentals[i].filing_date != None: # fundamentals.fundamentals[i].filing_date = fundamentals.fundamentals[i].filing_date.strftime('%Y-%m-%d') return(fundamentals.fundamentals)
def __init__(self): try: intrinio_sdk.ApiClient().configuration.api_key[ 'api_key'] = 'OmNiNGM1YmUyZTFhOGVhODgzMWIxZmNmZjdiYTllZWFj' self.security_api = intrinio_sdk.SecurityApi() self.company_api = intrinio_sdk.CompanyApi() except ApiException as e: print("Exception when establishing connection with api: %s\r\n" % e)
def main(): identifier = '^GSPC' start_date = '2018-01-01' end_date = '2019-01-01' frequency = 'daily' page_size = 100 next_page = '' response = intrinio.SecurityApi().get_security_stock_prices( identifier, start_date=start_date, end_date=end_date, frequency=frequency, page_size=page_size, next_page=next_page) print(response)
def get_stonks(): intrinio_sdk.ApiClient().configuration.api_key['api_key'] = 'API-KEY' security_api = intrinio_sdk.SecurityApi() identifiers = ['AAPL', 'MSFT', 'GS', 'NKE'] price_list = list() try: for identifier in identifiers: api_response = security_api.get_security_realtime_price(identifier) price_list.append((identifier, api_response.ask_price)) except ApiException as e: print( "Exception when calling SecurityApi->get_security_realtime_price: %s\r\n" % e) finally: return price_list
def get_all_securities(next_page = '', currency = 'USD', composite_mic = 'USCOMP'): intrinio_sdk = setup_intrinio_environment.get_connection() security_api = intrinio_sdk.SecurityApi() try: api_response = security_api.get_all_securities( active = True, currency = currency, composite_mic = composite_mic, next_page = next_page, page_size = 10000 ) except ApiException as e: print('Exception: SecurityApi -> get_all_securities: %s\r\n' % e) return None return pd.DataFrame(api_response.securities_dict)
def Getstock(name): intrinio_sdk.ApiClient( ).configuration.api_key['api_key'] = '[your-intrino-api-key]' security_api = intrinio_sdk.SecurityApi() identifier = name # str | A Security identifier (Ticker, FIGI, ISIN, CUSIP, Intrinio ID) #returns info on stock in json format try: api_response = security_api.get_security_realtime_price(identifier) high = api_response.high_price low = api_response.low_price opn = api_response.open_price exc = api_response.exchange_volume stock = [high, low, opn, exc] return (stock) except ApiException as e: zip = ( "Exception when calling SecurityApi->get_security_realtime_price: %s\n" % e)
def getdata(self): security_api = intrinio_sdk.SecurityApi() try: api_response = security_api.get_security_stock_prices( self.identifier, start_date=self.start_date, end_date=self.end_date, frequency=self.frequency, page_size=self.page_size, next_page=self.next_page) data_frame = pd.DataFrame(api_response.stock_prices_dict) except ApiException as e: print( "Exception when calling SecurityApi->get_security_stock_prices: %s\r\n" % e) print("wait for 1 min") finaldata = data_frame.filter([ 'date', 'open', 'high', 'low', 'close', 'volume', 'adj_open', 'adj_high', 'adj_low', 'adj_close', 'adj_volume' ]) finaldata['100ma'] = finaldata['close'].rolling(window=100, min_periods=0).mean() self.date = finaldata['date'] finaldata['date'] = pd.to_datetime(finaldata['date']) #plot_close(finaldata) finaldata.to_csv( f'D:\\PROJECTS\\python projects\\stockpredict\\Data\\{self.identifier}_daily_with_date.csv', index=False) # finaldata.sort_values(by = 'date', axis = 0,ascending = True) # finaldata.set_index('date',inplace =True) # #print(finaldata.to_string()) # finaldata.to_csv(f'D:\\PROJECTS\\python projects\\stockpredict\\Data\\{self.identifier}_daily.csv',index = False) return finaldata
def get_data_intrinio(ticker): import intrinio_sdk from pprint import pprint from intrinio_sdk.rest import ApiException intrinio_sdk.ApiClient( ).configuration.api_key['api_key'] = globals.INTRINIO_API_KEYS[0] security_api = intrinio_sdk.SecurityApi() identifier = ticker # str | A Security identifier (Ticker, FIGI, ISIN, CUSIP, Intrinio ID) try: start_date = '2019-08-01' # date | Return intraday prices starting at the specified date (optional) end_date = '2019-08-04' # date | Return intraday prices stopping at the specified date (optional) api_response1 = security_api.get_security_realtime_price( identifier, start_date=start_date, end_date=end_date) api_response2 = security_api.get_security_realtime_price( identifier) #, start_date=start_date, end_date=end_date) pprint(api_response1) pprint(api_response2) except ApiException as e: print( "Exception when calling SecurityApi->get_security_intraday_prices: %s\n" % e)
import intrinio_sdk import pandas as pd intrinio_sdk.ApiClient().configuration.api_key[ 'api_key'] = '{OjBkNDgwZTRhODgwMzgzNzU0OWI1NTM0YTA4NGNhYjI1}' security_api = intrinio_sdk.SecurityApi() r = security_api.get_security_stock_prices(identifier='AAPL', start_date='2000-01-01', end_date='2010-12-31', frequency='daily', page_size=10000) response_list = [x.to_dict() for x in r.stock_prices] df_intrinio = pd.DataFrame(response_list).sort_values('date') df_intrinio.set_index('date', inplace=True) df_intrinio.to_csv('/Users/chelseaw/Desktop/intrinio.csv') fileIntrinio = open('/Users/chelseaw/Desktop/intrinio.csv') print(fileIntrinio.read())
def gather_stock_time_series( api_key, ticker, start_date=None, end_date=None, output_format='dict', allow_max_rows=False): """ Given the ticker, start date, and end date, return from the Intrinio API stock data for that time frame in either a dictionary or a pandas dataframe format. Parameters ----------- api_key : str API key (sandbox or production) from Intrinio ticker : str the ticker symbol you would like to get stock data for start_date : str (optional) the earliest date in the format of "%Y-%m-%d", e.g. "2019-12-31" to get data for end_date : str (optional) the most recent date in the format of "%Y-%m-%d", e.g. "2019-12-31" to get data for output_format : str (optional, default = 'dict') the output format for the data, options are 'dict' for dictionary or 'pddf' for pandas dataframe allow_max_rows : bool (optional, default = False) if False, then only 100 rows will show in the output, otherwise up to 10000 rows will show (based on dates) Returns ----------- object of type output_format stock data for the specific timeframe in the specified output format Example ----------- >>> gather_stock_time_series(api_key, 'AAPL', start_date="2020-01-15", end_date="2020-01-30", output_format="pddf") """ # error messages msg1 = "Invalid data format: ticker must be a string" msg2 = "Invalid Date format: date must be a string in the format %Y-%m-%d" msg3 = "Invalid Input: end_date must be later than start_date" msg4 = "Invalid API Key: please input a valid API key as a string" # ensure the type of the ticker is a string if type(ticker) != str: return msg1 try: # change dates to datetime objects if start_date is not None: start_date = datetime.strptime(start_date, '%Y-%m-%d').date() if end_date is not None: end_date = datetime.strptime(end_date, '%Y-%m-%d').date() except Exception: return msg2 if start_date is not None and end_date is not None: if start_date >= end_date: return msg3 # initialize API key intrinio_sdk.ApiClient().configuration.api_key['api_key'] = api_key # initialize security API security_api = intrinio_sdk.SecurityApi() # if allow_max_rows=False if allow_max_rows is False: rows = 100 else: rows = 10000 try: # put stock prices into a variable stock_prices = security_api.get_security_stock_prices( ticker, start_date=start_date, end_date=end_date, page_size=rows).stock_prices except Exception: return msg4 # initialize a results dictionary results = {'date': [], 'close': [], 'adj_close': [], 'high': [], 'adj_high': [], 'low': [], 'adj_low': [], 'open': [], 'adj_open': [], 'volume': [], 'adj_volume': [], 'frequency': [], 'intraperiod': []} # fill in dictionary for i in list(range(0, len(stock_prices), 1)): results['date'].append(stock_prices[i].date) results['close'].append(stock_prices[i].close) results['adj_close'].append(stock_prices[i].adj_close) results['high'].append(stock_prices[i].high) results['adj_high'].append(stock_prices[i].adj_high) results['low'].append(stock_prices[i].low) results['adj_low'].append(stock_prices[i].adj_low) results['open'].append(stock_prices[i].open) results['adj_open'].append(stock_prices[i].adj_open) results['volume'].append(stock_prices[i].volume) results['adj_volume'].append(stock_prices[i].adj_volume) results['frequency'].append(stock_prices[i].frequency) results['intraperiod'].append(stock_prices[i].intraperiod) # if the ouput format is a dataframe, change to that if output_format == 'pddf': results = pd.DataFrame(results) return results
import intrinio_sdk from intrinio_sdk.rest import ApiException import os import numpy import time import ta as analysis import pandas as pd intrinio_sdk.ApiClient().configuration.api_key['api_key'] = os.environ.get( 'INTRINIO_API_KEY') company_api = intrinio_sdk.SecurityApi() # Returns stock prices as Intrinio ApiResponseSecurityStockPrices def get_stock_prices(id): try: prices = company_api.get_security_stock_prices(identifier=id) except ApiException as e: print("Exception when calling CompanyApi->filter_companies: %s\n" % e) return (prices) # Check that stock prices has been under and over the moving average def price_exceeds_ma(ticker_data, ma): result = ticker_data.stock_prices[ 1].close > ma and ticker_data.stock_prices[ 2].close > ma and ticker_data.stock_prices[ 3].close < ma and ticker_data.stock_prices[4].close < ma return (result)
def gather_stock_returns(api_key, ticker, buy_date, sell_date): """ Given the tickers, buy-in date, sell-out date, returns the historical prices and profit/loss (based on the adjusted closing prices). Parameters ----------- api_key : str API key (sandbox or production) from Intrinio tickers : list or str a single ticker or a list containing tickers. e.g. 'AAPL' or ['AAPL', 'CSCO'] buy_date : str the buy-in date in the format of "%Y-%m-%d", e.g. "2019-12-31". If the input date is not a trading day, it will be automatically changed to the next nearest trading day. sell_date : str the sell-out date in the format of "%Y-%m-%d", e.g. "2019-12-31". If the input date is not a trading day, it will be automatically changed to the last nearest trading day. Returns ----------- pandas.core.frame.DataFrame a dataframe that contains the companies, historical prices and corresponding profit/loss Example ----------- >>> gather_stock_returns(api_key, ['AAPL', 'CSCO'], "2017-12-31", "2019-03-01") """ msg1 = "Invalid Input: sell_date must be later than buy_date" msg2 = "Invalid Date format: date must be a string in the format %Y-%m-%d" msg3 = "Invalid API Key: please input a valid API key as a string" # test whether the input dates are in the right format try: buy_date = datetime.strptime(buy_date, '%Y-%m-%d').date() sell_date = datetime.strptime(sell_date, '%Y-%m-%d').date() if buy_date >= sell_date: return msg1 except Exception: return msg2 if type(ticker) == str: # if user gives just one ticker ticker = [ticker] # initialize API key intrinio_sdk.ApiClient().configuration.api_key['api_key'] = api_key # initialize security API security_api = intrinio_sdk.SecurityApi() # test if the API Key works try: security_api.get_security_stock_prices(ticker[0], start_date=buy_date, end_date=sell_date) except Exception: return msg3 # create the result DataFrame to record and report results = pd.DataFrame(columns=['Stock', 'Buy date', 'Buy price', 'Sell date', 'Sell price', 'Return (%)'], index=range(len(ticker))) # iterate through all the tickers and record the results i = 0 # if buy_date it not a trading day (holiday), we'll get the nearest next # trading day instead buy_date_upper = buy_date + timedelta(days=10) # the same idea for sell_date, but we'll get the nearest **last** trading # day instead. sell_date_lower = sell_date - timedelta(days=10) for ticker in ticker: api_response = security_api.get_security_stock_prices( ticker, start_date=buy_date, end_date=buy_date_upper ) buy_price = api_response.stock_prices[-1].adj_close buy_date = api_response.stock_prices[-1].date.strftime("%Y-%m-%d") api_response = security_api.get_security_stock_prices( ticker, start_date=sell_date_lower, end_date=sell_date) sell_price = api_response.stock_prices[0].adj_close sell_date = api_response.stock_prices[0].date.strftime("%Y-%m-%d") rtn = ((sell_price - buy_price) / buy_price)*100 rtn = round(rtn, 2) results.iloc[i, :] = [ticker, buy_date, buy_price, sell_date, sell_price, rtn] i += 1 return results
page_size = 100 # initialize the next page in the api call // '' ==> get the most recent next_page = '' # security price storage store = pd.DataFrame(columns=[ 'intrinio_id', 'asofdate', 'factor', 'dividend', 'dividend_currency', 'split_ratio' ]) # keep on making API calls per security until there are no more pages to pull from while next_page is not None: try: # API CALL res = intrinio_sdk.SecurityApi( ).get_security_stock_price_adjustments( identifier, start_date=start_date, end_date=end_date, page_size=page_size, next_page=next_page).to_dict() # get the prices from the call result temp = pd.DataFrame( res.get('stock_price_adjustments', [])).assign(intrinio_id=sec).rename(columns={ 'date': 'asofdate' }).reindex([ 'intrinio_id', 'asofdate', 'factor', 'dividend', 'dividend_currency', 'split_ratio' ], axis='columns') # remove all dates that are already in the database for the security temp = temp.loc[
def find_fundamentals(tkr_id, sandbox=False, nocomm=False): """ Returns a list of available fundamental financial indicators for the specified company. -- tkr_id: stock ticker name. 'AAPL' for Apple inc, 'XOM' for Exxon Mobile Corp. etc. (if using a developer sandbox key, only DOW 30 will be available) -- sandbox: Use this to turn sandbox mode on and off if you have a developers sandbox api key. Limited to DOW 30, but much less strict limits on api calls. *In .env file name main key INTRINIO_KEY and developer sandbox key INTRINIO_SANDBOX_KEY """ # Sandbox check and get env key if sandbox == False: intrinio_sdk.ApiClient().configuration.api_key['api_key'] = config( 'INTRINIO_KEY') security_api = intrinio_sdk.SecurityApi() elif sandbox == True: intrinio_sdk.ApiClient().configuration.api_key['api_key'] = config( 'INTRINIO_SANDBOX_KEY') security_api = intrinio_sdk.SecurityApi() # Initialize api's capi = intrinio_sdk.CompanyApi() fapi = intrinio_sdk.FundamentalsApi() # Set parameters to get most recent financial report fund_params = { 'identifier': tkr_id, 'filed_after': '2018-06-01', 'filed_before': '2018-11-01', 'reported_only': False, # 'fiscal_year' :fiscal_year, 'statement_code': 'income_statement', 'type': '', 'start_date': '', 'end_date': '', 'page_size': 1, 'next_page': '' } # Get most recent financials report fundamentals = capi.get_company_fundamentals(**fund_params) id_to_check = fundamentals.fundamentals[0].id fun_check = fapi.get_fundamental_standardized_financials(id_to_check) available_fun = [] common = ['date', 'fiscal_year', 'quarter'] # Make list of available fundamentals and add above for fun in fun_check.standardized_financials: available_fun.append(fun.data_tag.tag) if nocomm == False: for fun in common: available_fun.append(fun) else: available_fun.append('date') return (available_fun)
start_date = dt.datetime.strftime(dates_in_table.max() + dt.timedelta(days = 1), '%Y-%m-%d') if len(dates_in_table) != 0 else '' # set end date as most recent date end_date = dt.datetime.strftime(dt.date.today(),'%Y-%m-%d') # data freq --> 'daily' is the most granular frequency = 'daily' # records per page // larger page sizes are flaged by API as bulk d/l page_size = 100 # initialize the next page in the api call // '' ==> get the most recent next_page = '' # security price storage store = pd.DataFrame(columns = ['intrinio_id', 'asofdate', 'intraperiod', 'frequency', 'open_px', 'high_px', 'low_px', 'close_px', 'volume', 'adj_open', 'adj_high', 'adj_low', 'adj_close', 'adj_volume']) # keep on making API calls per security until there are no more pages to pull from while next_page is not None: try: # API CALL res = intrinio_sdk.SecurityApi().get_security_stock_prices(identifier, start_date=start_date, end_date=end_date, frequency=frequency, page_size=page_size, next_page=next_page).to_dict() # get the prices from the call result temp = pd.DataFrame(res.get('stock_prices',[])).assign(intrinio_id = sec).rename(columns = {'date':'asofdate', 'open':'open_px', 'high':'high_px', 'low':'low_px', 'close':'close_px'}).reindex(['intrinio_id', 'asofdate', 'intraperiod', 'frequency', 'open_px', 'high_px', 'low_px', 'close_px', 'volume', 'adj_open', 'adj_high', 'adj_low', 'adj_close', 'adj_volume'], axis = 'columns') # remove all dates that are already in the database for the security temp = temp.loc[~pd.to_datetime(temp['asofdate']).isin(dates_in_table),:] # add to previous calls store = pd.concat([store, temp], axis = 'index') # set the next page for the next API call next_page = res.get('next_page','') except Exception as err_msg: # write original query into the error msg err = pd.concat([err, pd.DataFrame([[identifier, start_date, end_date, frequency, page_size, next_page, str(err_msg)]], columns = ['identifier', 'start_date', 'end_date', 'frequency', 'page_size', 'next_page', 'err_msg'])], axis = 'index').reset_index(drop = True) # set the next_page = None --> condition to break out of loop next_page = None # only sleep if there is more to pull
from connectors import intrinio_util from support.financial_cache import cache from datetime import timedelta log = logging.getLogger() try: API_KEY = os.environ['INTRINIO_API_KEY'] except KeyError as ke: raise ValidationError("INTRINIO_API_KEY was not set", None) intrinio_sdk.ApiClient().configuration.api_key['api_key'] = API_KEY FUNDAMENTALS_API = intrinio_sdk.FundamentalsApi() COMPANY_API = intrinio_sdk.CompanyApi() SECURITY_API = intrinio_sdk.SecurityApi() INTRINIO_CACHE_PREFIX = 'intrinio' ''' Testing APIs using requests package ''' def retry_server_errors(func): ''' decorator that will retry intrinio server side errors, and let others pass through. Retries the error up to 5 times and sleeps 2 seconds between retries ''' def wrapper(*args, **kwargs):