## download_wrds.download_data_wrds_crsp()


Download data from WRDS CRSP.


Usage

``` python
download_wrds.download_data_wrds_crsp(
    dataset=None,
    start_date=None,
    end_date=None,
    type=None,
    batch_size=500,
    version="v2",
    additional_columns=None,
    add_ccm_links=False,
    adjust_volume=False,
    quiet=False
)
```


Downloads and processes stock return data from the CRSP database for a specified period. Users can choose between monthly and daily datasets. The function also includes delisting returns (see Notes) and calculates market capitalization and excess returns over the risk-free rate.


## Parameters


`dataset: str = None`  
A string specifying the CRSP dataset to download: 'crsp_monthly' or 'crsp_daily'.

`start_date: str = None`  
A character string or date in 'YYYY-MM-DD' format specifying the start date for the data. If not provided, a subset of the dataset is returned.

`end_date: str = None`  
A character string or date in 'YYYY-MM-DD' format specifying the end date for the data. If not provided, a subset of the dataset is returned.

`type: str = None`  
Deprecated. Use 'dataset' instead. If supplied, a leading 'wrds\_' prefix is stripped and a DeprecationWarning is emitted.

`batch_size: int = ``500`  
An optional integer specifying the batch size for processing daily data, with a default of 500.

`version: str = ``"v2"`  
An optional character specifying which CRSP version to use. 'v2' (the default) uses the updated second version of CRSP, and 'v1' downloads the legacy version of CRSP.

`additional_columns: list of str = None`  
Extra column names from the underlying CRSP source table to return alongside the standard output. For 'crsp_monthly' the source is 'crsp.msf_v2' (or 'crsp.msf' joined with 'crsp.msenames' under 'version="v1"'); for 'crsp_daily' the source is 'crsp.dsf_v2' (or 'crsp.dsf' for 'v1'). Pass any column from those tables (e.g., 'mthvol', 'mthvolflg' for monthly; 'dlyvol', 'dlyfacprc' for daily). When 'adjust_volume=True' for 'crsp_daily', this list must include the columns the adjustment needs ('dlyprc', 'dlyvol', 'dlyfacprc', 'primaryexch' for 'v2'; 'prc', 'vol', 'cfacpr', 'exchcd' for 'v1'); a 'ValueError' is raised otherwise.

`add_ccm_links: bool = ``False`  
A boolean indicating whether CRSP-Compustat links should be added automatically using 'download_data_wrds_ccm_links'. Defaults to False.

`adjust_volume: bool = ``False`  
A boolean indicating whether daily CRSP trading volume data should be adjusted according to Gao and Ritter (2010). Defaults to False. Note that cumulative price adjustment factors are computed from the data in memory; results may be incorrect if 'start_date' excludes early observations for some permnos.

`quiet: bool = ``False`  
If True, the messages about the WRDS connection and the download progress are suppressed. Defaults to False.


## Returns


`pl.DataFrame`  
A data frame containing CRSP stock returns, including delisting returns, along with calculated market capitalization and excess returns over the risk-free rate. The structure of the returned data frame depends on the selected dataset.


## Notes

How delisting returns enter the data depends on 'version':

- 'v2' (CIZ): CRSP already includes the delisting return in the monthly ('mthret') and daily ('dlyret') returns of the month and the day in which a stock delists, so 'ret' and 'ret_excess' include it without further adjustment. The common-stock screens of the security history ('crsp.stksecurityinfohist') are evaluated on the stock's last trading day for these observations, because the history holds no active record after it. Without this, the month or day that carries the delisting return would be dropped.
- 'v1' (legacy SIZ), monthly: the delisting return 'dlret' from 'crsp.msedelist' replaces the return of the delisting month in 'ret_adj', which 'ret_excess' is based on. If 'dlret' is missing, the return is set to -30% for performance-related delistings (codes 500, 520, 551-574, 580, and 584; Shumway, 1997), kept for code 100, and set to -100% otherwise. 'ret' is the unadjusted return.
- 'v1' (legacy SIZ), daily: the delisting return replaces the return on the delisting date, and later observations are dropped.


## References

Shumway, T. (1997). The delisting bias in CRSP data. Journal of Finance, 52(1), 327-340. https://doi.org/10.1111/j.1540-6261.1997.tb03818.x

Gao, X., and Ritter, J. R. (2010). The marketing of seasoned equity offerings. Journal of Financial Economics, 97(1), 33-52. https://doi.org/10.1016/j.jfineco.2010.03.007


## Examples

``` python
from tidyfinance import download_data_wrds_crsp
crsp_monthly = download_data_wrds_crsp(
    'crsp_monthly', '2020-11-01', '2020-12-31'
)
crsp_daily = download_data_wrds_crsp(
    'crsp_daily', '2020-12-01', '2020-12-31'
)
download_data_wrds_crsp(
    'crsp_monthly',
    '2020-11-01',
    '2020-12-31',
    additional_columns=['mthvol', 'mthvolflg'],
)
```
