Import Data
EPFR Stock Factor data
For efficiency while importing EPFR stock factor data, we will create the function below called fetch.fcn(), which combines functions from library('EPFR.r') as well as Functions discussed earlier in this section, fetch.ftp.QuantFactor(), ftp.QuantFactor.format(), ftp.QuantFactor.name(). This will seamlessly fetch and format data directly from the users EPFR ftp connection or from a local directory, for a desired period. This function also performs any necessary compounding or summing of the specified factor over the desired period.
fetch.fcn <- function(x, y, n, w = "", classif) {
z <- yyyymm.lag(x, 1)
z <- yyyymmdd.ex.yyyymm(z)
z <- fetch.ftp.QuantFactor(y, z, n, w, classif)
if (!(y %in% c("fundct", "Herfindahl", "FloAlpha"))){
z <- compound.sf(z, ifelse(y == "FloMo", F, T))
}
z
}The function fetch.fcn() requires inputs being the period of data x we are looking for, the stock factor name we want to test y. The number of tailing periods or the lookback period n.
x <- "202301" #period of data
y <- "FloMo" #stock factor name - FloMo, FloTrend, FloDiff, FloDiff2, ActWtTrend, ActWtDiff, ActWtDiff2, AllocMo, AllocTrend, AllocDiff, Herfindahl, AllocSkew
n <- 15 #lookback periodAn optional input the user can add is their customized location of the StockFlows5.0 folder within their EPFR ftp connection. If there is no additional string before /StockFlows5.0/, this variable can be left as empty.
Another option if the user is having issues connecting to ftp via r, they can manually download history from the /StockFlows5.0/QuantitativeFactors folder in ftp. The user would then just want to include the full path of where the files sit in their local device as w. For example if the full location of files we want to backtest are 'C:\\EPFR\\StockFlows5.0\\QuantitativeFactors\\Daily\\FloMo\\Aggregate', we only write:
To fetch the data we can now execute fetch.fcn() and below shows a snippet of how this function formats the data before compounding/summing over period:
| FloMo.20221212 | FloMo.20221213 | FloMo.20221214 | FloMo.20221215 | FloMo.20221216 | |
|---|---|---|---|---|---|
| 7041046 | 0.052453 | -0.047630 | 0.216057 | -0.016471 | -0.078056 |
| 7041220 | 0.142779 | -0.006029 | 0.147090 | 0.002294 | -0.041535 |
| 7049454 | 0.108854 | -0.029265 | 0.240663 | 0.101264 | -0.176245 |
| 7054419 | 0.066045 | -0.030855 | 0.203190 | -0.061885 | 0.637253 |
| 7056752 | 0.115637 | -0.023468 | 0.147528 | 0.009846 | -0.130408 |
| 7057091 | 0.130964 | -0.005219 | 0.120446 | -0.045665 | 0.020170 |
| 7063679 | 0.023733 | 0.056491 | 0.104855 | -0.111659 | 0.463849 |
Returns data
There should be a single return file for each calendar year the user is backtesting this strategy. The columns in the file should consist of the return data over a single month for each security included in the universe. EPFR is not able to provide this data, so users must find this data though their own third-party resources.
For the purpose of creating backtest results in this notebook, we have used an internal file containing historical daily stock returns, stored locally in the folder EPFR/returns.
Below shows a snippet of what this file contains:
EPFR/StockRets/Sp500_2022.csv
| Ret.1 | Ret.2 | Ret.3 | Ret.4 | Ret.5 | Ret.6 | Ret.7 | Ret.8 | Ret.9 | Ret.10 | Ret.11 | Ret.12 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | 15.17 | 12.57 | -1.79 | -5.01 | 13.91 | -10.81 | -20.12 | 8.45 | -2.43 | 16.72 | 16.63 | 4.71 |
| 5 | -0.72 | -6.25 | 2.49 | -7.00 | 4.44 | -10.76 | 4.37 | 7.19 | -10.55 | 20.14 | 6.70 | -1.76 |
| 6 | 9.61 | -8.82 | 5.95 | -11.74 | 8.12 | -19.52 | 30.88 | 2.10 | 0.33 | -0.77 | -4.83 | 14.28 |
| 8 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
| 18 | -2.38 | 7.64 | 4.40 | -0.07 | 0.05 | -5.75 | 2.44 | 10.29 | -1.64 | -0.61 | 10.62 | -2.38 |
| 28 | 1.04 | 1.28 | 1.06 | -4.27 | -4.20 | 2.29 | -2.04 | -3.06 | -3.64 | -4.25 | 9.37 | 3.29 |
Format: columns are named as ‘Ret.’ followed by reference to month by number (1 = January, 12 = December, etc.)