README.md (8106B)
1 # FinanceRoutines 2 3 | **Documentation** | **Build Status** | **Code Coverage** | 4 |:-------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------:|:------------------------------------------------------------------------------------------------:| 5 | [![][docs-stable-img]][docs-stable-url] [![][docs-latest-img]][docs-latest-url] | [](https://github.com/louloulibs/FinanceRoutines.jl/actions/workflows/CI.yml?query=branch%3Amain) | [](https://codecov.io/gh/louloulibs/FinanceRoutines.jl) 6 7 8 `FinanceRoutines.jl` is a package that contains useful functions to download and process academic financial data. 9 10 The package provides functions to: 11 12 - Import CRSP and Compustat from the WRDS Postgres server 13 - Import Fama-French 3-factor, 5-factor, and momentum series from Ken French's website 14 - Import GSW yield curves from the [Federal Reserve](https://www.federalreserve.gov/pubs/feds/2006/200628/200628abs.html) and compute bond returns 15 - Estimate rolling betas for stocks 16 - Calculate equal-weighted and value-weighted portfolio returns 17 - Run event studies (CARs, BHARs) with standard abnormal return models 18 - Run data quality diagnostics on financial DataFrames 19 20 ## Installation 21 22 `FinanceRoutines.jl` is a registered package. 23 You can install from the my julia registry [`loulouJL`](https://github.com/LouLouLibs/loulouJL) via the julia package manager: 24 ```julia 25 > using Pkg, LocalRegistry 26 > pkg"registry add https://github.com/LouLouLibs/loulouJL.git" 27 > Pkg.add("FinanceRoutines") 28 ``` 29 30 If you don't want to add a new registry, you can install it directly from github: 31 ```julia 32 > import Pkg; Pkg.add("https://github.com/louloulibs/FinanceRoutines.jl#main") 33 ``` 34 35 36 ## Examples 37 38 ### Import data from WRDS 39 40 First import the monthly stock file and the compustat funda file 41 ```julia 42 using FinanceRoutines 43 using DataFrames 44 45 # Set up a wrds connection (requires your WRDS credentials) 46 wrds_conn = FinanceRoutines.open_wrds_pg() 47 ``` 48 49 Then we can import the monthly stock file. 50 The new version of `FinanceRoutines.jl` supports pulling from the new `CIZ` file format. 51 ```julia 52 df_msf_v2 = import_MSF_v2(wrds_conn) # CHECK YOUR TWO STEP AUTHENTICATOR 53 # 3826457×11 DataFrame 54 # Row │ permno mthcaldt mthret mthretx shrout mthprc mthcap mthprevcap siccd naics datem 55 # │ Int64 Date Decimal? Decimal? Int64? Decimal? Decimal? Decimal? Int64 String? MonthlyD… 56 # ─────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 57 # 1 │ 10000 1986-01-31 0.707317 0.707317 3680 4.375 16100 9430 3990 missing 1986-01 58 # 2 │ 10000 1986-02-28 -0.257143 -0.257143 3680 3.25 11960 16100 3990 missing 1986-02 59 # 3 │ 10000 1986-03-31 0.365385 0.365385 3680 4.4375 16330 11960 3990 missing 1986-03 60 # 4 │ 10000 1986-04-30 -0.098592 -0.098592 3793 4 15172 16330 3990 missing 1986-04 61 ``` 62 63 On the other side, the package also allows pulling from the compustat funda file: 64 ```julia 65 df_funda = import_Funda(wrds_conn); 66 build_Funda!(df_funda) 67 ``` 68 69 Last there is a function to get the link table and merge crsp with compustat: 70 ```julia 71 # Merge both files 72 df_linktable = FinanceRoutines.import_ccm_link(wrds_conn) 73 # merge gvkey on monthly stock file 74 df_msf = link_MSF(df_linktable, 75 select(df_msf_v2, :permno, :mthcaldt=>:date, :datem, :mthret=>:ret, :mthcap)) 76 df_msf = innerjoin(df_msf, df_funda, on = [:gvkey, :datey], matchmissing=:notequal) 77 ``` 78 79 ### Import Fama-French factors 80 81 Download directly from Ken French's website. Supports 3-factor, 5-factor, and momentum at daily/monthly/annual frequency. 82 83 ```julia 84 # 3-factor model 85 df_FF3 = import_FF3() 86 df_FF3_daily = import_FF3(frequency=:daily) 87 88 # 5-factor model (adds profitability RMW and investment CMA) 89 df_FF5 = import_FF5() 90 91 # Momentum factor 92 df_mom = import_FF_momentum() 93 ``` 94 95 ### Estimate treasury bond returns 96 97 The function downloads yield curves from the [NY Fed GSW](https://www.federalreserve.gov/pubs/feds/2006/200628/200628abs.html) and estimate returns based on the curves 98 99 ```julia 100 df_GSW = import_gsw_parameters(date_range=(Date("1960-01-01"), Dates.today()) ) 101 FinanceRoutines.add_yields!(df_GSW, [1, 10]) # maturities is in years 102 # or compute the yields yourself using functions 103 transform!(df_GSW, 104 AsTable(:) => ByRow(row -> 105 begin 106 gsw_params = GSWParameters(row) 107 ismissing(gsw_params) ? missing : gsw_yield(5, gsw_params) 108 end) => :yield_5y, 109 ) 110 ``` 111 112 See the [doc](https://louloulibs.github.io/FinanceRoutines.jl/) and tests for more options. 113 114 115 ### Portfolio returns 116 117 ```julia 118 # Equal-weighted portfolio returns by date 119 df_ew = calculate_portfolio_returns(df_msf, :ret, :datem; weighting=:equal) 120 121 # Value-weighted by market cap, grouped by size quintile 122 df_vw = calculate_portfolio_returns(df_msf, :ret, :datem; 123 weighting=:value, weight_col=:mktcap, groups=:size_quintile) 124 ``` 125 126 ### Data diagnostics 127 128 ```julia 129 report = diagnose(df_msf) 130 report[:missing_rates] # fraction missing per column 131 report[:duplicate_keys] # duplicate (permno, date) pairs 132 report[:suspicious_values] # extreme returns, negative prices 133 ``` 134 135 ### Event studies (experimental) 136 137 > **Note:** `event_study` is experimental and has not been extensively validated against established implementations. Use with caution and verify results independently. 138 139 ```julia 140 events = DataFrame(permno=[10001, 10002], event_date=[Date("2010-06-15"), Date("2011-03-20")]) 141 142 # Market-adjusted CARs and BHARs (default) 143 results = event_study(events, df_msf) 144 145 # Market model with custom windows 146 results = event_study(events, df_msf; 147 event_window=(-5, 5), estimation_window=(-252, -21), 148 model=:market_model) 149 ``` 150 151 ### Common operations in asset pricing 152 153 Look in the documentation for a guide on how to estimate betas: over the whole sample and using rolling regressions. 154 The package exports `calculate_rolling_betas`. 155 156 157 ## Other references to work with financial data 158 159 The package the closest to this one is 160 161 - [WrdsMerger.jl](https://github.com/junder873/WRDSMerger.jl); WrdsMerger is probably in a more stable state than this package. 162 - [WRDS.jl](https://github.com/elenev/WRDS.jl); WRDS specific wrappers to interact with the Postgres database. 163 164 Other packages or sources of code I have used to process the WRDS data 165 166 - [WRDS demo on momentum](https://wrds-www.wharton.upenn.edu/documents/1442/wrds_momentum_demo.html) (python) 167 - Tidy Finance [Book](https://www.tidy-finance.org) and [repo](https://github.com/tidy-finance/website) (R) 168 - French data [package](https://nareal.github.io/frenchdata/articles/basic_usage.html) (R) 169 - Ian Gow's Empirical Research in Accounting [Book](https://iangow.github.io/far_book/) (R) 170 - Replication [Open Source AP](https://github.com/OpenSourceAP/CrossSection/tree/master) (stata) 171 172 173 174 175 [docs-stable-img]: https://img.shields.io/badge/docs-stable-blue.svg 176 [docs-stable-url]: https://louloulibs.github.io/FinanceRoutines.jl/ 177 [docs-latest-img]: https://img.shields.io/badge/docs-latest-blue.svg 178 [docs-latest-url]: https://louloulibs.github.io/FinanceRoutines.jl/ 179