rtstock package

Submodules

rtstock.error module

Exceptions module.

Custom exceptions used by Realtime Stock package.

exception rtstock.error.RequestError[source]

Bases: exceptions.Exception

Class for request exception.

rtstock.stock module

Stock module.

This module contains all the classes used to retrieve information about a single stock from Yahoo Finances. That includes, real-time quotes information as well as historical data.

class rtstock.stock.Stock(ticker)[source]

Bases: object

Class for handling stock.

Provides methods to retrieve real-time quotes and historical data from Yahoo Finance database.

>>> from rtstock.stock
  • import Stock
    >>>
    >>> stock = Stock('AAPL')
    >>> print(stock)
    <Stock AAPL>
    
    param ticker:Stock ticker in Yahoo Finances format.
    type ticker:string
get_historical(start_date, end_date)[source]

Get stock’s daily historical information.

Returns a dictionary with Adj Close, Close, High, Low, Open and Volume, between the start_date and the end_date. Is start_date and end_date were not provided all the available information will be retrieved. Information provided by YQL platform. Check here for more information on YQL.

Warning

Request limited to a period not greater than 366 days. Use download_historical() to download the full historical data.

>>> stock.get_historical('2016-03-01', '2016-03-02')
[
    {
        'Close': '100.75',
        'Low': '99.639999',
        'High': '100.889999',
        'Adj_Close': '100.140301',
        'Date': '2016-03-02',
        'Open': '100.510002',
        'Volume': '33169600'
    },
    {
        'Close': '100.529999',
        'Low': '97.419998',
        'High': '100.769997',
        'Adj_Close': '99.921631',
        'Date': '2016-03-01',
        'Open': '97.650002',
        'Volume': '50407100'
    }
]
Parameters:
  • start_date (string on the format of "yyyy-mm-dd") – Start date
  • end_date (string on the format of "yyyy-mm-dd") – End date
Returns:

Daily historical information.

Return type:

list of dictionaries

get_info()[source]

Get all stock’s information provided by Yahoo Finance.

There is no guarantee that all the fields will be available for all stocks. That being said, the following fields will be retrieved by this method as a python dictionary from YQL platform:

  • Ask
  • AverageDailyVolume
  • Bid
  • BookValue
  • Change
  • Change_PercentChange
  • ChangeFromFiftydayMovingAverage
  • ChangeFromTwoHundreddayMovingAverage
  • ChangeFromYearHigh
  • ChangeFromYearLow
  • ChangeinPercent
  • Currency
  • DaysHigh
  • DaysLow
  • DaysRange
  • DividendPayDate
  • DividendShare
  • DividendYield
  • EarningsShare
  • EBITDA
  • EPSEstimateCurrentYear
  • EPSEstimateNextQuarter
  • EPSEstimateNextYear
  • ExDividendDate
  • FiftydayMovingAverage
  • LastTradeDate
  • LastTradePriceOnly
  • LastTradeTime
  • LastTradeWithTime
  • MarketCapitalization
  • Name
  • OneyrTargetPrice
  • Open
  • PEGRatio
  • PERatio
  • PercebtChangeFromYearHigh
  • PercentChange
  • PercentChangeFromFiftydayMovingAverage
  • PercentChangeFromTwoHundreddayMovingAverage
  • PercentChangeFromYearLow
  • PreviousClose
  • PriceBook
  • PriceEPSEstimateCurrentYear
  • PriceEPSEstimateNextYear
  • PriceSales
  • ShortRatio
  • StockExchange
  • Symbol
  • TwoHundreddayMovingAverage
  • Volume
  • YearHigh
  • YearLow
  • YearRange

Check here for more information on YQL.

Returns:Dictionary with all the available information.
Return type:dictionary
get_latest_price()[source]

Get stock’s latest price.

Get the latest available quote from Yahoo Finance along with its respective time.

>>> stock.get_latest_price()
{
    'LastTradePriceOnly': '95.89',
    'LastTradeTime': '4:00pm'
}
Returns:Dictionary with latest price and trade time.
Return type:dictionary
get_ticker()[source]

Get stock’s ticker.

>>> stock.get_ticker()
'AAPL'
Returns:Ticker.
Return type:string
save_historical(output_folder)[source]

Download historical data from Yahoo Finance.

Downloads full historical data from Yahoo Finance as CSV. The following fields are available: Adj Close, Close, High, Low, Open and Volume. Files will be saved to output_folder as <ticker>.csv.

Parameters:output_folder (string) – Output folder path
set_ticker(ticker)[source]

Set stock’s ticker.

>>> stock.set_ticker('YHOO')
>>> print(stock)
<Stock YHOO>
Parameters:ticker (string) – Stock ticker in Yahoo Finances format.

rtstock.utils module

Utility functions.

This module contains utility functions to gather information from Yahoo Finance.

rtstock.utils.download_historical(tickers_list, output_folder)[source]

Download historical data from Yahoo Finance.

Downloads full historical data from Yahoo Finance as CSV. The following fields are available: Adj Close, Close, High, Low, Open and Volume. Files will be saved to output_folder as <ticker>.csv.

Parameters:
  • tickers_list (list of strings) – List of tickers that will be returned.
  • output_folder (string) – Output folder path
rtstock.utils.request_historical(ticker, start_date, end_date)[source]

Get stock’s daily historical information.

Returns a dictionary with Adj Close, Close, High, Low, Open and Volume, between the start_date and the end_date. Is start_date and end_date were not provided all the available information will be retrieved. Information provided by YQL platform. Check here for more information on YQL.

Warning

Request limited to a period not greater than 366 days. Use download_historical() to download the full historical data.

>>> request_historical('AAPL', '2016-03-01', '2016-03-02')
[
    {
        'Close': '100.75',
        'Low': '99.639999',
        'High': '100.889999',
        'Adj_Close': '100.140301',
        'Date': '2016-03-02',
        'Open': '100.510002',
        'Volume': '33169600'
    },
    {
        'Close': '100.529999',
        'Low': '97.419998',
        'High': '100.769997',
        'Adj_Close': '99.921631',
        'Date': '2016-03-01',
        'Open': '97.650002',
        'Volume': '50407100'
    }
]
Parameters:
  • start_date (string on the format of "yyyy-mm-dd") – Start date
  • end_date (string on the format of "yyyy-mm-dd") – End date
Returns:

Daily historical information.

Return type:

list of dictionaries

rtstock.utils.request_quotes(tickers_list, selected_columns=[u'*'])[source]

Request Yahoo Finance recent quotes.

Returns quotes information from YQL. The columns to be requested are listed at selected_columns. Check here for more information on YQL.

>>> request_quotes(['AAPL'], ['Name', 'PreviousClose'])
{
    'PreviousClose': '95.60',
    'Name': 'Apple Inc.'
}
Parameters:
  • table (string) – Table name.
  • tickers_list (list of strings) – List of tickers that will be returned.
  • selected_columns (list of strings, optional) – List of columns to be returned, defaults to [‘*’]
Returns:

Requested quotes.

Return type:

json

Raises:

TypeError, TypeError

Module contents