Python client for the YCharts Market Data API.
- Python ( 3.x )
- Valid API key from YCharts.
Install from github using pip
.
pip install git+https://github.com/ycharts/pycharts.git
# or
pip install pycharts
Full API documentation can be found at https://www.ycharts.com/api/docs/.
For questions and information about API access, visit YCharts or email us ([email protected]) for more info.
Below are some examples to get started with using the Python Client.
- Setup
- Discovery Queries
- Data Point Queries
- Data Series Queries
- Info Queries
- Dividend Queries
- Stock Split and Spinoff Queries
- Exceptions
from pycharts import CompanyClient, IndicatorClient, MutualFundClient
ycharts_api_key = 'sample-api-key'
company_client = CompanyClient(ycharts_api_key)
mutual_fund_client = MutualFundClient(ycharts_api_key)
indicator_client = IndicatorClient(ycharts_api_key)
Gets a paginated list of companies, mutual funds or indicators.
companies = company_client.get_securities(exchange='NYSE')
mutual_funds = mutual_fund_client.get_securities(category='Technology')
indicators = indicator_client.get_securities(region='USA')
Get a single date-value pair of data from one or more securities for one or more calculations. It takes an optional date parameter and will return the latest value before the requested date.
# Queries the latest price values for AAPL and MSFT
point_rsp = company_client.get_points(['AAPL', 'MSFT'], ['price'])
# Queries the latest net asset value for M:FCNTX
point_rsp = mutual_fund_client.get_points('M:FCNTX', ['net_asset_value'])
# Queries the latest value for I:USICUI
point_rsp = indicator_client.get_points('I:USICUI')
# Queries the price values for AAPL 21 days ago
previous_point_rsp = company_client.get_points('AAPL', 'price', query_date=-21)
# Queries the net asset value for M:FCNTXPL 21 days ago
twenty_one_days_ago = datetime.datetime.now() - datetime.timedelta(days=21)
previous_point_rsp = mutual_fund_client.get_points('M:FCNTX', ['net_asset_value'],
query_date=twenty_one_days_ago)
# Queries the value for I:USICUI 21 days ago
previous_point_rsp = indicator_client.get_points('I:USICUI', query_date=-31)
Get a series of date-value pairs of data from one or more securities for one or more calculations. It takes optional start_date and end_date parameters and will return all values between the requested dates.
now = datetime.datetime.now()
past = now - datetime.timedelta(days=100)
series_rsp = company_client.get_series(['AAPL', 'MSFT'], ['price'],
query_start_date=past , query_end_date=now)
series_rsp = mutual_fund_client.get_series('M:FCNTX', ['net_asset_value'],
query_start_date=past , query_end_date=now)
series_rsp = indicator_client.get_series('I:USICUI',
query_start_date=past , query_end_date=now)
# example resampling request
series_rsp = company_client.get_series(['AAPL', 'MSFT'], ['price'], query_start_date=past,
query_end_date=now, resampling_frequency='daily', resampling_function='mean')
Get information about one or more securities.
info_rsp = company_client.get_info(['AAPL', 'MSFT'], ['description'])
info_rsp = mutual_fund_client.get_info('M:FCNTX', ['inception_date', 'broad_asset_class'])
info_rsp = indicator_client.get_info('I:USICUI', ['next_release'])
Get dividends from one or more companies or mutual funds.
start_date = datetime.datetime(2015, 1, 1)
dividend_rsp = company_client.get_dividends(['AAPL', 'MSFT'], ex_start_date=start_date,
dividend_type='special')
dividend_rsp = mutual_fund_client.get_dividends('M:FCNTX', ex_start_date=start_date)
Get a list of stock split/spinoff objects that within the date range specified by the optional start and end date parameters.
split_spinoff_end_date = datetime.datetime(2014, 1, 1)
split_rsp = company_client.get_stock_splits(['AAPL'], split_end_date=split_spinoff_end_date)
spinoff_rsp = company_client.get_stock_spinoffs(['AAPL'], spinoff_end_date=split_spinoff_end_date)
# More exception classes in pycharts/exceptions.py
from pycharts import exceptions
try:
bad_point_rsp = company_client.get_points(['AAPL'], ['price'], query_date=45)
except exceptions.PyChartsRequestException as pycharts_error:
print(pycharts_error.error_message)