Skip to content

Latest commit

 

History

143 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FinanceRoutines

Documentation Build Status Code Coverage
CI Testing codecov

FinanceRoutines.jl is a package that contains useful functions to download and process academic financial data.

The package provides functions to:

  • Import CRSP and Compustat from the WRDS Postgres server
  • Import Fama-French 3-factor, 5-factor, and momentum series from Ken French's website
  • Import GSW yield curves from the Federal Reserve and compute bond returns
  • Estimate rolling betas for stocks
  • Calculate equal-weighted and value-weighted portfolio returns
  • Run event studies (CARs, BHARs) with standard abnormal return models
  • Run data quality diagnostics on financial DataFrames

Installation

FinanceRoutines.jl is a registered package. You can install from the my julia registry loulouJL via the julia package manager:

> using Pkg, LocalRegistry
> pkg"registry add https://github.com/LouLouLibs/loulouJL.git"
> Pkg.add("FinanceRoutines")

If you don't want to add a new registry, you can install it directly from github:

> import Pkg; Pkg.add("https://github.com/louloulibs/FinanceRoutines.jl#main")

Examples

Import data from WRDS

First import the monthly stock file and the compustat funda file

using FinanceRoutines
using DataFrames

# Set up a wrds connection (requires your WRDS credentials)
wrds_conn = FinanceRoutines.open_wrds_pg()

Then we can import the monthly stock file. The new version of FinanceRoutines.jl supports pulling from the new CIZ file format.

df_msf_v2 = import_MSF_v2(wrds_conn) # CHECK YOUR TWO STEP AUTHENTICATOR
# 3826457×11 DataFrame
#      Row │ permno  mthcaldt    mthret     mthretx    shrout   mthprc       mthcap         mthprevcap     siccd  naics    datem
#          │ Int64   Date        Decimal?   Decimal?   Int64?   Decimal?     Decimal?       Decimal?       Int64  String?  MonthlyD…
# ─────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#        1 │  10000  1986-01-31   0.707317   0.707317     3680     4.375          16100           9430      3990  missing  1986-01
#        2 │  10000  1986-02-28  -0.257143  -0.257143     3680     3.25           11960          16100      3990  missing  1986-02
#        3 │  10000  1986-03-31   0.365385   0.365385     3680     4.4375         16330          11960      3990  missing  1986-03
#        4 │  10000  1986-04-30  -0.098592  -0.098592     3793     4              15172          16330      3990  missing  1986-04

On the other side, the package also allows pulling from the compustat funda file:

df_funda = import_Funda(wrds_conn);
build_Funda!(df_funda)

Last there is a function to get the link table and merge crsp with compustat:

# Merge both files
df_linktable = FinanceRoutines.import_ccm_link(wrds_conn)
 # merge gvkey on monthly stock file
df_msf = link_MSF(df_linktable,  
    select(df_msf_v2, :permno, :mthcaldt=>:date, :datem, :mthret=>:ret, :mthcap))
df_msf = innerjoin(df_msf, df_funda, on = [:gvkey, :datey], matchmissing=:notequal)

Import Fama-French factors

Download directly from Ken French's website. Supports 3-factor, 5-factor, and momentum at daily/monthly/annual frequency.

# 3-factor model
df_FF3 = import_FF3()
df_FF3_daily = import_FF3(frequency=:daily)

# 5-factor model (adds profitability RMW and investment CMA)
df_FF5 = import_FF5()

# Momentum factor
df_mom = import_FF_momentum()

Estimate treasury bond returns

The function downloads yield curves from the NY Fed GSW and estimate returns based on the curves

df_GSW = import_gsw_parameters(date_range=(Date("1960-01-01"), Dates.today()) )
FinanceRoutines.add_yields!(df_GSW, [1, 10]) # maturities is in years
# or compute the yields yourself using functions
transform!(df_GSW, 
    AsTable(:) => ByRow(row -> 
        begin 
            gsw_params = GSWParameters(row)
            ismissing(gsw_params) ? missing : gsw_yield(5, gsw_params)
        end) => :yield_5y,
    )

See the doc and tests for more options.

Portfolio returns

# Equal-weighted portfolio returns by date
df_ew = calculate_portfolio_returns(df_msf, :ret, :datem; weighting=:equal)

# Value-weighted by market cap, grouped by size quintile
df_vw = calculate_portfolio_returns(df_msf, :ret, :datem;
    weighting=:value, weight_col=:mktcap, groups=:size_quintile)

Data diagnostics

report = diagnose(df_msf)
report[:missing_rates]      # fraction missing per column
report[:duplicate_keys]     # duplicate (permno, date) pairs
report[:suspicious_values]  # extreme returns, negative prices

Event studies (experimental)

Note: event_study is experimental and has not been extensively validated against established implementations. Use with caution and verify results independently.

events = DataFrame(permno=[10001, 10002], event_date=[Date("2010-06-15"), Date("2011-03-20")])

# Market-adjusted CARs and BHARs (default)
results = event_study(events, df_msf)

# Market model with custom windows
results = event_study(events, df_msf;
    event_window=(-5, 5), estimation_window=(-252, -21),
    model=:market_model)

Common operations in asset pricing

Look in the documentation for a guide on how to estimate betas: over the whole sample and using rolling regressions. The package exports calculate_rolling_betas.

Other references to work with financial data

The package the closest to this one is

  • WrdsMerger.jl; WrdsMerger is probably in a more stable state than this package.
  • WRDS.jl; WRDS specific wrappers to interact with the Postgres database.

Other packages or sources of code I have used to process the WRDS data

Releases

Packages

Used by

Contributors

Languages