tiingo数据平台api的rest客户端
tiingo的Python项目详细描述
用法
如果您想在安装之前尝试此库,请单击下面的打开一个包含联机可运行示例的文件夹。
首先,从pypi安装库:
pip install tiingo
如果您希望以 p and as dataframe 或 series 格式接收结果,并且您尚未安装pandas,请将其作为可选依赖项安装:
pip install tiingo[pandas]
接下来,初始化您的客户机。建议使用环境 变量以方便初始化客户端。
fromtiingoimportTiingoClient# Set TIINGO_API_KEY in your environment variables in your .bash_profile, OR# pass a dictionary with 'api_key' as a key into the TiingoClient.client=TiingoClient()
或者,您可以使用字典自定义/授权您的客户。
config={}# To reuse the same HTTP Session across API calls (and have better performance), include a session key.config['session']=True# If you don't have your API key as an environment variable,# pass it in via a configuration dictionary.config['api_key']="MY_SECRET_API_KEY"# Initializeclient=TiingoClient(config)
现在您可以使用 tiingoclient 来进行api调用。(除以下示例中使用的参数外,每个端点都可以使用其他参数,请检查每个函数的docstring以获取详细信息。)。
# Get Tickerticker_metadata=client.get_ticker_metadata("GOOGL")# Get latest prices, based on 3+ sources as JSON, sampled weeklyticker_price=client.get_ticker_price("GOOGL",frequency="weekly")# Get historical GOOGL prices from August 2017 as JSON, sampled dailyhistorical_prices=client.get_ticker_price("GOOGL",fmt='json',startDate='2017-08-01',endDate='2017-08-31',frequency='daily')# Check what tickers are available, as well as metadata about each ticker# including supported currency, exchange, and available start/end dates.tickers=client.list_stock_tickers()# Get news articles about given tickers or search terms from given domainsarticles=client.get_news(tickers=['GOOGL','AAPL'],tags=['Laptops'],sources=['washingtonpost.com'],startDate='2017-01-01',endDate='2017-08-31')
要以 pandas 格式接收结果,请使用 get dataframe() 方法:
#Get a pd.DataFrame of the price history of a single symbol (default is daily):ticker_history=client.get_dataframe("GOOGL")#The method returns all of the available information on a symbol, such as open, high, low, close,#adjusted close, etc. This page in the tiingo api documentation lists the available information on each#symbol: https://api.tiingo.com/docs/tiingo/daily#priceData.#Frequencies and start and end dates can be specified similarly to the json method above.#Get a pd.Series of only one column of the available response data by specifying one of the valid the#'metric_name' parameters:ticker_history=client.get_dataframe("GOOGL",metric_name='adjClose')#Get a pd.DataFrame for a list of symbols for a specified metric_name (default is adjClose if no#metric_name is specified):ticker_history=client.get_dataframe(['GOOGL','AAPL'],frequency='weekly',metric_name='volume',startDate='2017-01-01',endDate='2018-05-31')
您可以指定任何一天结束的频率(每日、每周、每月,或 get_ticker_price 和 get_dataframe 方法的任何日内频率。每周频率重新采样至周五的一天结束,每月频率重新采样至每月的最后一天,每年频率重新采样至每年的12-31日的一天结束。日内频率用整数后跟"min"或"hour"指定,例如"30min"或"1hour"。