1. Prepare a signal file¶
The protocol's one input is a CSV file with three columns: the stock, the month and the signal's value. The same file works for the web form and for the package.
permno,dates,MYSIG
10006,197501,-2.18
10006,197502,-2.76
The columns¶
| Column | Header | Content |
|---|---|---|
| Stock | permno |
CRSP PERMNO, an integer |
| Month | dates |
YYYYMM, for example 197501 |
| Signal | the acronym, for example MYSIG |
A number |
How the package reads the file:
- The header names
permnoanddatesare required, in lower case. The remaining column is the signal, and its name is the default acronym. - A month that is not a valid
YYYYMM(month13, say) stops the run with an error. - A value that is not a number is read as missing. A row whose
permnocannot be read is dropped. - If a stock has two rows for the same month, one is kept. Do not rely on which: write one row per stock and month.
- Stock-months that are not in the database's monthly panel, or that have no market capitalization there, are ignored.
Timing: the value for month t is used at the end of month t¶
The protocol uses the file's values as they are and does not lag them. Portfolios are formed at the end of a month in which the signal has values and earn returns from the next month. The Fama-MacBeth regressions relate a month's returns to the previous month's signal.
So a value dated 202406 must be computable from what was known at the end of June 2024.
For accounting data this is the usual convention: data for fiscal years ending in calendar
year y − 1 are dated June of year y. The package's own annual Compustat columns are stored
this way, on the June row.
Direction: high values are bought¶
The long/short strategy buys the portfolio with the highest values and sells the one with the lowest. If your signal predicts low returns (asset growth, for example), multiply it by −1.
Frequency: the data decide when the strategy trades¶
A portfolio is bought in a month in which the signal has values and is held, without rebalancing, until the next such month.
- A signal with values every month trades every month.
- A signal with values each June only trades once a year, and is held from July through the following June. Do not copy the June value into the other eleven months unless you want the strategy rebalanced, and charged for it, every month.
Writing a file from the database¶
With the package installed and a database built, a signal made from the panel's own columns takes a few lines. Gross profitability (gross profits over assets), from annual data:
import assayinganomalies as aa
df = aa.load({"gp", "at"}, panel=True) # (date, permno) panel; annual columns on the June row
sig = (df["gp"] / df["at"]).where(df["at"] > 0).dropna().rename("GPA").reset_index()
sig["dates"] = sig["date"].dt.year * 100 + sig["date"].dt.month # month-end -> YYYYMM
sig[["permno", "dates", "GPA"]].to_csv("gpa.csv", index=False)
The panel column dictionary lists the columns. A Compustat
item the build does not carry is added without a rebuild, for example
aa add-compustat-vars --annual xrd; it is then loaded like any other column.
Data of your own that is keyed by Compustat gvkey gets a permno through the database's
CRSP-Compustat link: aa.merge_on_link(my_frame, link, date_col=...)
(tutorial, chapter 12).
Checks worth making before a run¶
- The file has the three columns, and the dates are
YYYYMMintegers. - The first month with values is the one you expect. The report's sample starts there.
- The number of stocks per month is reasonable. The report's first figure shows coverage, but a run takes two hours.
- No look-ahead: the value dated month t uses nothing that was published later.
Next: Run the protocol yourself, or submit the file.