Skip to main content

Finance Data

Project description

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 or datetime.datetime object. 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 or datetime.datetime object. 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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dafin-0.0.4.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dafin-0.0.4-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file dafin-0.0.4.tar.gz.

File metadata

  • Download URL: dafin-0.0.4.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for dafin-0.0.4.tar.gz
Algorithm Hash digest
SHA256 1e58dbad9b6ce8dcb4336bb9959686fd21e55c9745e002b5b4c6dd30db000a91
MD5 16a31511f855fbbea1709442fd439aea
BLAKE2b-256 8b134187627265dc0f8d69d4d1111ff46b66f83fe1438516bfff9215f2b6bc36

See more details on using hashes here.

File details

Details for the file dafin-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dafin-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for dafin-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8cd55fc99501e9c26d85eeaa2e2d758e5db5b98a5d78a59d5a014472268bc1c8
MD5 131fb13f274ad03f80cdd8ff6907d2a3
BLAKE2b-256 149430be2e99b2bda801883f5cf07e720b86851c830e4e73d3c48873b7e9afa7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page