openseries is a project with tools to analyze financial timeseries of a single asset or a group of assets. It is solely made for daily or less frequent data.
This is a project with tools to analyze financial timeseries of a single
asset or a group of assets. It is solely made for daily or less frequent data.
An example of how to make use of the OpenTimeSeries is shown below. The design
aligns with how we within our fund company's code base have a subclass of the
OpenTimeSeries with class methods for our different data sources. Combined with some
additional tools it allows us to efficiently present investment cases to clients.
The code snippet can be pasted into a Python console to run it.
Install openseries and yfinance first.
fromopenseriesimportOpenTimeSeriesimportyfinanceasyfmsft=yf.Ticker("MSFT")
history=msft.history(period="max")
series=OpenTimeSeries.from_df(history.loc[:, "Close"])
_=series.value_to_log().set_new_label("Microsoft Log Returns of Close Prices")
_,_=series.plot_series()
Sample output using the OpenFrame.all_properties() method:
Scilla Global Equity C (simulation+fund) Global Low Volatility index, SEK
ValueType.PRICE ValueType.PRICE
Total return 3.641282 1.946319
Arithmetic return 0.096271 0.069636
Geometric return 0.093057 0.06464
Volatility 0.120279 0.117866
Return vol ratio 0.800396 0.59081
Downside deviation 0.085956 0.086723
Sortino ratio 1.119993 0.802975
Positive share 0.541783 0.551996
Worst -0.071616 -0.089415
Worst month -0.122503 -0.154485
Max drawdown -0.309849 -0.435444
Max drawdown in cal yr -0.309849 -0.348681
Max drawdown dates 2020-03-23 2009-03-09
CVaR 95.0% -0.01793 -0.018429
VaR 95.0% -0.011365 -0.010807
Imp vol from VaR 95% 0.109204 0.103834
Z-score 0.587905 0.103241
Skew -0.650782 -0.888109
Kurtosis 8.511166 17.527367
observations 4309 4309
span of days 6301 6301
first indices 2006-01-03 2006-01-03
last indices 2023-04-05 2023-04-05
Usage example on Jupyter Nbviewer
Development Instructions
These instructions assume that you have a compatible Python version installed on
your machine and that you are OK to install this project in a virtual environment.
The OpenTimeSeries and OpenFrame classes are both subclasses of
the Pydantic BaseModel. Please refer to its documentation for information
on any attributes or methods inherited from this model.
Windows Powershell
git clone https://github.com/CaptorAB/openseries.git
cd openseries
./make.ps1 make
Mac Terminal/Linux
git clone https://github.com/CaptorAB/openseries.git
cd openseries
make
source source_me
make install
Testing and Linting / Type-checking
Ruff and Mypy checking is embedded in the pre-commit hook. Both
are also used in the project's GitHub workflows and are run when the lint
alternative is chosen in the below commands.
Any silenced error codes can be found in the
pyproject.toml
file or in in-line comments.
Defines the class OpenTimeSeries for managing and analyzing a single timeseries. The module also defines a function timeseries_chain that can be used to chain two timeseries objects together.
Defines the class OpenFrame for managing a group of timeseries, and e.g. calculate a portfolio timeseries from a rebalancing strategy between timeseries. The module also defines functions to simulate, optimize, and plot portfolios.
Merges the Pandas Dataframes of the constituent OpenTimeSeries.
trunc_frame
OpenFrame
Truncates the OpenFrame to a common period.
add_timeseries
OpenFrame
Adds a given OpenTimeSeries to the OpenFrame.
delete_timeseries
OpenFrame
Deletes an OpenTimeSeries from the OpenFrame.
relative
OpenFrame
Calculates a new series that is the relative performance of two others.
make_portfolio
OpenFrame
Calculates a portfolio timeseries based on the series and weights. Weights can be provided as a list, or a weight strategy can be set as equal weights or inverted volatility.
Aligns the series dates to a business calendar. Defaults to Sweden.
resample
OpenTimeSeries, OpenFrame
Resamples the series to a specific frequency.
resample_to_business_period_ends
OpenTimeSeries, OpenFrame
Resamples the series to month-end dates with monthly, quarterly or annual frequency.
value_nan_handle
OpenTimeSeries, OpenFrame
Fills Nan in a value series with the preceding non-Nan value.
return_nan_handle
OpenTimeSeries, OpenFrame
Replaces Nan in a return series with a 0.0 float.
to_cumret
OpenTimeSeries, OpenFrame
Converts a return series into a value series and/or resets a value series to be rebased from 1.0.
to_json
OpenTimeSeries, OpenFrame
Method to export object data to a json file.
to_xlsx
OpenTimeSeries, OpenFrame
Method to save the data in the .tsdf DataFrame to an Excel file.
value_to_ret
OpenTimeSeries, OpenFrame
Converts a value series into a percentage return series.
value_to_diff
OpenTimeSeries, OpenFrame
Converts a value series into a series of differences.
value_to_log
OpenTimeSeries, OpenFrame
Converts a value series into a logarithmic return series.
value_ret_calendar_period
OpenTimeSeries, OpenFrame
Returns the series simple return for a specific calendar period.
plot_series
OpenTimeSeries, OpenFrame
Opens a HTML Plotly Scatter plot of the series in a browser window.
plot_bars
OpenTimeSeries, OpenFrame
Opens a HTML Plotly Bar plot of the series in a browser window.
to_drawdown_series
OpenTimeSeries, OpenFrame
Converts the series into drawdown series.
rolling_return
OpenTimeSeries, OpenFrame
Returns a pandas.DataFrame with rolling returns.
rolling_vol
OpenTimeSeries, OpenFrame
Returns a pandas.DataFrame with rolling volatilities.
rolling_var_down
OpenTimeSeries, OpenFrame
Returns a pandas.DataFrame with rolling VaR figures.
rolling_cvar_down
OpenTimeSeries, OpenFrame
Returns a pandas.DataFrame with rolling CVaR figures.
calc_range
OpenTimeSeries, OpenFrame
Returns the start and end dates of a range from specific period definitions. Used by the below numerical methods and not meant to be used independently.
Numerical properties available for individual OpenTimeSeries or on all series in an OpenFrame.
Annualized volatility. Pandas .std() is the equivalent of stdev.s([...]) in MS excel.
downside_deviation
float, pandas.Series
OpenTimeSeries, OpenFrame
Downside deviation is the volatility of all negative return observations. Minimum Accepted Return (MAR) set to zero.
ret_vol_ratio
float, pandas.Series
OpenTimeSeries, OpenFrame
Ratio of arithmetic mean return and annualized volatility. It is the Sharpe Ratio with the riskfree rate set to zero.
sortino_ratio
float, pandas.Series
OpenTimeSeries, OpenFrame
The Sortino Ratio is the arithmetic mean return divided by the downside deviation. This attribute assumes that the riskfree rate and the MAR are both zero.
omega_ratio
float, pandas.Series
OpenTimeSeries, OpenFrame
The Omega Ratio compares returns above a certain target level (MAR) to the total downside risk below MAR.
var_down
float, pandas.Series
OpenTimeSeries, OpenFrame
Downside 95% Value At Risk, "VaR". The equivalent of percentile.inc([...], 1-level) over returns in MS Excel. For other confidence levels use the corresponding method.
cvar_down
float, pandas.Series
OpenTimeSeries, OpenFrame
Downside 95% Conditional Value At Risk, "CVaR". For other confidence levels use the corresponding method.
worst
float, pandas.Series
OpenTimeSeries, OpenFrame
Most negative percentage change of a single observation.
Annualized volatility. Pandas .std() is the equivalent of stdev.s([...]) in MS excel.
downside_deviation_func
float, pandas.Series
OpenTimeSeries, OpenFrame
Downside deviation is the volatility of all negative return observations. MAR and riskfree rate can be set.
ret_vol_ratio_func
float, pandas.Series
OpenTimeSeries, OpenFrame
Ratio of arithmetic mean return and annualized volatility. It is the Sharpe Ratio with the riskfree rate set to zero. A riskfree rate can be set as a float or a series chosen for the frame function.
sortino_ratio_func
float, pandas.Series
OpenTimeSeries, OpenFrame
The Sortino Ratio is the arithmetic mean return divided by the downside deviation. A riskfree rate can be set as a float or a series chosen for the frame function. MAR is set to zero.
omega_ratio_func
float, pandas.Series
OpenTimeSeries, OpenFrame
The Omega Ratio compares returns above a certain target level (MAR) to the total downside risk below MAR.
var_down_func
float, pandas.Series
OpenTimeSeries, OpenFrame
Downside 95% Value At Risk, "VaR". The equivalent of percentile.inc([...], 1-level) over returns in MS Excel. Default is 95% confidence level.
Z-score as (last return - mean return) / standard deviation of returns.
target_weight_from_var
float, pandas.Series
OpenTimeSeries, OpenFrame
A position target weight from the ratio between a VaR implied volatility and a given target volatility.
About
openseries is a project with tools to analyze financial timeseries of a single asset or a group of assets. It is solely made for daily or less frequent data.