Functions to evaluate stock statistics
stock_stats.py: function
This module contains all the statistics needed for stocks.
- src.stock_stats.evaluate_annualized_return(data: Series, n_years: int) float
Evaluate the annualized return.
The annualized return for n_years is ((1+ Rc) ^ (1/n_years)) - 1
- Parameters:
data (pd.Series) – The data series of stock price.
n_years (int) – The number of years we want to calculate the return for.
- Returns:
The annualized cumulative return. If data is not long enough, return np.nan.
- Return type:
float
- src.stock_stats.evaluate_annualized_volatility(data: Series, n_years: int = 1) float
Evaluate the annualized volatility.
- Parameters:
data (pd.Series) – The data.
n_years (int) – The number of years we want to calculate the volatility for.
- Returns:
The annualized volatility.
- Return type:
float
- src.stock_stats.evaluate_cumulative_return(data: Series) float | str
Evaluate the cumulative return of a series in percent.
Lets write P_initial the initial value of our series, and P_current the current value of our series.
The cumulative return is then (P_current - P_initial) / P_initial
- Parameters:
data (pd.Series) – The data series of stock price.
- Returns:
The cumulative return. If data is not long enough, return np.nan.
- Return type:
float
- src.stock_stats.evaluate_stats_information(data: Series, symbol: str) Dict[str, float | str]
Gives several statistics about stock time-series.
This function evaluates several statistics about a stock time-series. The statistics are: - cumulative return - annualized cumulative return - annualized volatility
- Parameters:
data (pd.Series) – The data, sorted in ascending time.
symbol (str) – The stock symbol.
- Returns:
The result dict.
- Return type:
Dict[str, float | str]
Examples
>>> data = pd.Series( ... [1, 5, 7, 2, 3], ... index=pd.Index( ... [ ... pd.Timestamp(0, unit="d"), ... pd.Timestamp(7, unit="d"), ... pd.Timestamp(3, unit="d"), ... pd.Timestamp(4, unit="d"), ... pd.Timestamp(365, unit="d"), ... ] ... ), ... ) >>> evaluate_stats_information(data, "AAPL") {'symbol': 'AAPL', 'cumulativeReturn': -40.0, 'annualizedCumulativeReturn': 200.0, 'annualizedVolatility': 2.41}