Package: dafin
dafindafin 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.installdafin is available onPyPI, so you can install it by running the following command:pipinstalldafinUsageUsage Manual forReturnsDataClassTheReturnsDataclass 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 ClassYou can initialize theReturnsDataclass 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 theReturnsDataclass:fromdafinimportReturnsData# Create an instance of ReturnsData with a list of asset symbols and specify the column for price datadata_instance=ReturnsData(['AAPL','GOOGL'],col_price="Close")# Verify the instance is createdprint(isinstance(data_instance,ReturnsData))# This should print: TrueParameters: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 "Adj Close".path_cache: The path where cache files are stored. This parameter is optional and defaults to DEFAULT_CACHE_DIR.2. Retrieving Daily Returns DataTheget_returnsmethod 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 theget_returnsmethod:# 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 rangereturns_data=data_instance.get_returns('2022-01-01','2022-01-10')# Print the retrieved dataprint(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.ExamplefrompathlibimportPathfromdafinimportReturnsData,Performancedefmain():"""Fetch returns data for assets, risk-free assets, and benchmark. Then compute and print the performance summary.Doctest:>>> isinstance(path, Path)True"""# Define assets, risk-free assets, and benchmark tickersassets=["AAPL","AMZN","SPY"]assets_rf=["BND"]assets_benchmark=["SPY"]# Define date rangedate_start="2020-01-01"date_end="2022-12-31"# Define path for saving resultspath=Path('experiments')# Fetch returns data for the defined assetsreturns=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 metricsperformance=Performance(returns_assets=returns,returns_rf=returns_rf,returns_benchmark=returns_benchmark)# Save performance metrics and associated figures to the specified pathperformance.save_figs(path)performance.save_data(path)performance.save_results(path)# Print the performance summaryprint(performance.summary)if__name__=="__main__":main()