A Python library for working with time series data. It comes with common financial functions built-in.
Go to file
2022-06-06 22:10:16 +05:30
.ipynb_checkpoints Remove test check if instance is of type Mapping 2022-04-10 14:10:18 +05:30
.vscode changed return TimeSeries to return self.__class__ 2022-02-21 22:48:24 +05:30
pyfacts Implemented sortino ratio 2022-06-06 22:10:16 +05:30
test_files Remove test check if instance is of type Mapping 2022-04-10 14:10:18 +05:30
tests Renamed test file 2022-06-06 21:58:07 +05:30
.DS_Store Remove test check if instance is of type Mapping 2022-04-10 14:10:18 +05:30
.gitignore Series tests pass data type as string 2022-02-22 13:05:54 +05:30
dict_iter.py test files 2022-02-20 11:36:56 +05:30
LICENSE Setup package and tested with tox 2022-02-17 16:20:48 +05:30
my_test.py Series tests pass data type as string 2022-02-22 13:05:54 +05:30
README.md improved Readme 2022-06-05 23:12:04 +05:30
requirements.txt started working on transform with aggregation 2022-05-18 21:39:57 -07:00
setup.py renamed module to PyFacts 2022-06-05 23:06:12 +05:30
test2.py Remove test check if instance is of type Mapping 2022-04-10 14:10:18 +05:30
test_series.py Series tests pass data type as string 2022-02-22 13:05:54 +05:30
test.py added sharpe ratio 2022-04-29 07:43:06 +05:30
testing.ipynb Updated test file 2022-06-06 08:17:12 +05:30
tox.ini Setup package and tested with tox 2022-02-17 16:20:48 +05:30
VERSION Setup package and tested with tox 2022-02-17 16:20:48 +05:30

PyFacts

PyFacts stands for Python library for Financial analysis and computations on time series. It is a library which makes it simple to work with time series data.

Most libraries, and languages like SQL, work with rows. Operations are performed by rows and not by dates. For instance, to calculate 1-year rolling returns in SQL, you are forced to use either a lag of 365/252 rows, leading to an approximation, or slow and cumbersome joins. PyFacts solves this by allowing you to work with dates and time intervals. Hence, to calculate 1-year returns, you will be specifying a lag of 1-year and the library will do the grunt work of finding the most appropriate observations to calculate these returns on.

The problem

Libraries and languages usually don't allow comparison based on dates. Calculating month on month or year on year returns are always cumbersome as users are forced to rely on row lags. However, data always have inconsistencies, especially financial data. Markets don't work on weekends, there are off days, data doesn't get released on a few days a year, data availability is patchy when dealing with 40-year old data. All these problems are exacerbated when you are forced to make calculations using lag.

The Solution

PyFacts aims to simplify things by allowing you to:

  • Compare time-series data based on dates and time-period-based lag
  • Easy way to work around missing dates by taking the closest data points
  • Completing series with missing data points using forward fill and backward fill
  • Use friendly dates everywhere written as a simple string

Creating a time series

Time series data can be created from a dictionary, a list of lists/tuples/dicts, or by reading a csv file.

Example:

>>> import pyfacts as pft

>>> time_series_data = [
...    ('2021-01-01', 10),
...    ('2021-02-01', 12),
...    ('2021-03-01', 14),
...    ('2021-04-01', 16),
...    ('2021-05-01', 18),
...    ('2021-06-01', 20)
...]

>>> ts = fc.TimeSeries(time_series_data)

Sample usage

>>> ts.calculate_returns(as_on='2021-04-01', return_period_unit='months', return_period_value=3, annual_compounded_returns=False)
(datetime.datetime(2021, 4, 1, 0, 0), 0.6)

>>> ts.calculate_returns(as_on='2021-04-15', return_period_unit='months', return_period_value=3, annual_compounded_returns=False)
(datetime.datetime(2021, 4, 1, 0, 0), 0.6)

Working with dates

With PyFacts, you never have to go into the hassle of creating datetime objects for your time series. PyFacts will parse any date passed to it as string. The default format is ISO format, i.e., YYYY-MM-DD. However, you can use your preferred format simply by specifying it in the options in datetime library compatible format, after importing the library. For example, to use DD-MM-YYY format:

>>> import pyfacts as pft
>>> fc.PyfactsOptions.date_format = '%d-%m-%Y'

Now the library will automatically parse all dates as DD-MM-YYYY

If you happen to have any one situation where you need to use a different format, all methods accept a date_format parameter to override the default.

To-do

Core features

  • Add setitem
  • Create emtpy TimeSeries object
  • Read from CSV
  • Write to CSV
  • Convert to dict
  • Convert to list of tuples

pyfacts features

  • Sync two TimeSeries
  • Average rolling return
  • Sharpe ratio
  • Jensen's Alpha
  • Beta
  • Sortino ratio
  • Correlation & R-squared
  • Treynor ratio
  • Max drawdown
  • Moving average

Pending implementation

  • Use limit parameter in ffill and bfill
  • Implementation of ffill and bfill may be incorrect inside expand, check and correct
  • Implement interpolation in expand