Skip to content

Latest commit

 

History

History
150 lines (120 loc) · 6.15 KB

README.md

File metadata and controls

150 lines (120 loc) · 6.15 KB

pycharts

Python client for the YCharts Market Data API.

Requirements

  • Python (2.7, 3.0, 3.2, 3.3, 3.4, 3.5)
  • Valid API key from YCharts.

Installation

Install from github using pip.

pip install git+https://github.com/ycharts/pycharts.git
# or
pip install pycharts

Documentation and Support

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 (sales@ycharts.com) for more info.

Examples

Below are some examples to get started with using the Python Client.

Setup

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)

Discovery Queries

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')

Data Point Queries

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)

Data Series Queries

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')

Info Queries

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'])

Dividend Queries

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)

Stock Split and Spinoff Queries

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)

Exceptions

# 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)