dafin
dafin is an open-source Python package designed for collecting, storing, and visualizing financial data from any source. It offers user-friendly APIs that present data in Pandas dataframes, making it easy to visualize with standard Matplotlib methods.
install
dafin is available on PyPI, so you can install it by running the following command:
pip install dafin
Usage
Usage Manual for ReturnsData Class
The ReturnsData class is designed for managing and retrieving asset return data. It allows users to initialize the data with asset symbols, specify a column for price data, and provide a cache path. The class also offers a method to retrieve daily return data for a specific date range.
1. Initializing the ReturnsData Class
You can initialize the ReturnsData class by passing a list of asset symbols or a single asset symbol as a string. You can also specify the column name for price data and the path where cache files will be stored.
Here is an example of how to create an instance of the ReturnsData class:
from dafin import ReturnsData
# Create an instance of ReturnsData with a list of asset symbols and specify the column for price data
data_instance = ReturnsData(['AAPL', 'GOOGL'], col_price="Close")
# Verify the instance is created
print(isinstance(data_instance, ReturnsData)) # This should print: True
Parameters:
assets: A list of asset symbols or a single asset symbol as a string. This parameter is required.col_price: The name of the column for price data. This parameter is optional and defaults to "Close".path_cache: The path where cache files are stored. This parameter is optional and defaults to DEFAULT_CACHE_DIR.
2. Retrieving Daily Returns Data
The get_returns method allows you to retrieve the daily returns data for a specified date range. If no date range is provided, it will return all available data.
Here is an example of how to use the get_returns method:
# Assuming that the instance has a 'returns' attribute as a DataFrame and the 'normalize_date' function is defined
# Retrieve daily returns data for a specific date range
returns_data = data_instance.get_returns('2022-01-01', '2022-01-10')
# Print the retrieved data
print(returns_data) # This will print the DataFrame with the daily returns data between '2022-01-01' and '2022-01-10'
Parameters:
date_start: The start date as a string ordatetime.datetimeobject. This parameter is optional and defaults to None, in which case all available data will be returned.date_end: The end date as a string ordatetime.datetimeobject. This parameter is optional and defaults to None, in which case all available data will be returned.
Example
from pathlib import Path
from dafin import Performance, ReturnsData
def main():
"""
Fetch returns data for assets, risk-free assets, and benchmark. Then compute and print the performance summary.
"""
# Define assets, risk-free assets, and benchmark tickers
assets = ["AAPL", "AMZN", "SPY"]
assets_rf = ["BND"]
assets_benchmark = ["SPY"]
# Define date range
date_start = "2015-01-01"
date_end = "2019-12-31"
# Define path for saving results
path = Path("experiments")
# Fetch returns data for the defined assets
returns = ReturnsData(assets=assets).get_returns(
date_start=date_start, date_end=date_end
)
returns_rf = ReturnsData(assets=assets_rf).get_returns(
date_start=date_start, date_end=date_end
)
returns_benchmark = ReturnsData(assets=assets_benchmark).get_returns(
date_start=date_start, date_end=date_end
)
# Calculate performance metrics
performance = Performance(
returns_assets=returns,
returns_rf=returns_rf,
returns_benchmark=returns_benchmark,
)
# Save performance metrics and associated figures to the specified path
performance.save_figs(path)
performance.save_data(path)
performance.save_results(path)
# Print the performance summary
print(performance.summary)
if __name__ == "__main__":
main()
Metadata
Release files for dafin 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dafin-0.0.4.tar.gz | 13.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dafin-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.5 kB
Release files / dafin-0.0.4.tar.gz
| Download URL | dafin-0.0.4.tar.gz |
|---|---|
| Size | 13.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1e58dbad9b6ce8dcb4336bb9959686fd21e55c9745e002b5b4c6dd30db000a91
|
|
BLAKE2b-256 checksum How to use checksums |
8b134187627265dc0f8d69d4d1111ff46b66f83fe1438516bfff9215f2b6bc36
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.11
|
Release files / dafin-0.0.4-py3-none-any.whl
| Download URL | dafin-0.0.4-py3-none-any.whl |
|---|---|
| Size | 13.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8cd55fc99501e9c26d85eeaa2e2d758e5db5b98a5d78a59d5a014472268bc1c8
|
|
BLAKE2b-256 checksum How to use checksums |
149430be2e99b2bda801883f5cf07e720b86851c830e4e73d3c48873b7e9afa7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.11
|