forked from simonyanchen/ChinaEquityRiskModel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactor.R
429 lines (364 loc) · 15 KB
/
Factor.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#Factor functions
#Cumulative return over one year (averaged),
#Skipping the most recent two weeks to mitigate the price reversal effect
Factor.Momentum <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
CHG_PCT <- Utils.CleanData("CHG_PCT", Ref.Date, FALSE, 1)
DATE <- CHG_PCT$DATE
TEMP <- as.data.frame(zoo::rollmean(subset(CHG_PCT, select = -DATE), 53, na.pad = TRUE, align = "right"))
#Skipping the most recent two weeks
TEMP <- rbind(TEMP[1,],head(TEMP,-1))
Momentum <- cbind.data.frame(DATE, TEMP)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Momentum <- Momentum[Index,]
Momentum <- Utils.Normalize(Momentum)
return(Momentum)
}
#Combination of the following descriptors:
#Book to Price (17%)
#Earnings to Price (19%)
#Cash Flow to Price (7%)
#Sales / EV (15%)
#EBITDA / EV (21%)
#Forecast Earnings to Price (21%)
Factor.Value <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
BP_RATIO <- Utils.CleanData("BP_RATIO", Ref.Date, FALSE, 0)
EP_RATIO <- Utils.CleanData("EP_RATIO", Ref.Date, FALSE, 0)
CFP_RATIO <- Utils.CleanData("CFP_RATIO", Ref.Date, FALSE, 0)
SE_RATIO <- Utils.CleanData("SE_RATIO", Ref.Date, FALSE, 0)
EE_RATIO <- Utils.CleanData("EE_RATIO", Ref.Date, FALSE, 0)
EEP_RATIO <- Utils.CleanData("EEP_RATIO", Ref.Date, FALSE, 0)
DATE <- BP_RATIO$DATE
Value <- subset(BP_RATIO, select = -DATE) * 0.17 +
subset(EP_RATIO, select = -DATE) * 0.19 +
subset(CFP_RATIO, select = -DATE) * 0.07 +
subset(SE_RATIO, select = -DATE) * 0.15 +
subset(EE_RATIO, select = -DATE) * 0.21 +
subset(EEP_RATIO, select = -DATE) * 0.21
Value <- cbind.data.frame(DATE, Value)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Value <- Value[Index,]
Value <- Utils.Normalize(Value)
return(Value)
}
#Most recently announced net dividend divided by the current market price
Factor.DivYld <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
DVD_YIELD <- Utils.CleanData("DVD_YIELD", Ref.Date, TRUE, 0)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DVD_YIELD$DATE)
DivYld <- DVD_YIELD[Index,]
DivYld <- Utils.Normalize(DivYld)
return(DivYld)
}
#Combination of the following descriptors:
#Log(Market Capitalization) (32%)
#Log(Sales) (33%)
#Log(Total Assets) (35%)
Factor.Size <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
MKT_CAP <- Utils.CleanData("MKT_CAP", Ref.Date, TRUE, 0)
SALES <- Utils.CleanData("SALES", Ref.Date, TRUE, 0)
TOT_ASSET <- Utils.CleanData("TOT_ASSET", Ref.Date, TRUE, 0)
DATE <- MKT_CAP$DATE
Size <- log(subset(MKT_CAP, select = -DATE)) * 0.32 +
log(subset(SALES, select = -DATE)) * 0.33 +
log(subset(TOT_ASSET, select = -DATE)) * 0.35
Size <- cbind.data.frame(DATE, Size)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Size <- Size[Index,]
Size <- Utils.Normalize(Size)
return(Size)
}
#The exponential weighted average (EWMA) of the ratio of shares traded to shares outstanding
Factor.Trade <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
VOL_RATIO <- Utils.CleanData("VOL_RATIO", Ref.Date, FALSE, 2)
DATE <- VOL_RATIO$DATE
VOL_RATIO <- cbind.data.frame(DATE, lapply(subset(VOL_RATIO, select = -DATE),
(function(x) TTR::EMA(x, n = 504, ratio = log(2)/180))))
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Trade <- VOL_RATIO[Index,]
Trade <- Utils.Normalize(Trade)
return(Trade)
}
#Combination of the following descriptors:
Factor.EarnVariab <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
INCOME <- Utils.CleanData("INCOME", Ref.Date, FALSE, 5)
CASH_FLOW <- Utils.CleanData("CASH_FLOW", Ref.Date, FALSE, 5)
SALES <- Utils.CleanData("SALES", Ref.Date, FALSE, 5)
TOT_ASSET <- Utils.CleanData("TOT_ASSET", Ref.Date, TRUE, 5)
DATE <- INCOME$DATE
sd_Income <- as.data.frame(zoo::rollapply(subset(INCOME, select = -DATE), 265, sd, fill = NA, align = "right"))
sd_CF <- as.data.frame(zoo::rollapply(subset(CASH_FLOW, select = -DATE), 265, sd, fill = NA, align = "right"))
sd_Sales <- as.data.frame(zoo::rollapply(subset(SALES, select = -DATE), 265, sd, fill = NA, align = "right"))
md_Asset <- as.data.frame(zoo::rollapply(subset(TOT_ASSET, select = -DATE), 265, function(x) median(x, na.rm = TRUE), fill = NA, align = "right"))
EarnVariab <- cbind.data.frame(DATE, (sd_Income * 0.34 + sd_CF * 0.33 + sd_Sales * 0.33) / md_Asset)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
EarnVariab <- EarnVariab[Index,]
EarnVariab <- Utils.Normalize(EarnVariab)
return(EarnVariab)
}
#Combination of the following descriptors:
#Return on Equity (28%)
#Return on Asset (28%)
#Return on Capital Employed (28%)
#EBITDA Margin (16%)
Factor.Profit <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
INCOME <- Utils.CleanData("INCOME", Ref.Date, FALSE, 0)
BOOK_VAL <- Utils.CleanData("BOOK_VAL", Ref.Date, FALSE, 0)
TOT_ASSET <- Utils.CleanData("TOT_ASSET", Ref.Date, FALSE, 0)
DATE <- INCOME$DATE
#Temp
Profit <- subset(INCOME, select = -DATE) / subset(BOOK_VAL, select = -DATE) * 0.5 +
subset(INCOME, select = -DATE) / subset(TOT_ASSET, select = -DATE) * 0.5
Profit <- cbind.data.frame(DATE, Profit)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Profit <- Profit[Index,]
Profit <- Utils.Normalize(Profit)
return(Profit)
}
#Combination of the following descriptors:
#Rolling Volatility (29%)
#Rolling CAPM Beta (24%)
#Historical Sigma (25%)
#Cumulative Range (22%)
Factor.Volatility <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
CHG_PCT <- Utils.CleanData("CHG_PCT", Ref.Date, FALSE, 1)
PX_LAST <- Utils.CleanData("PX_LAST", Ref.Date, TRUE, 1)
BETA <- Utils.CleanData("BETA", Ref.Date, TRUE, 0)
SIGMA <- Utils.CleanData("SIGMA", Ref.Date, TRUE, 0)
DATE <- CHG_PCT$DATE
Vol <- as.data.frame(zoo::rollapply(subset(CHG_PCT, select = -DATE), 53, sd, fill = NA, align = "right")) * sqrt(53) / 100
Range <- as.data.frame(zoo::rollmax(subset(CHG_PCT, select = -DATE), 53, sd, fill = NA, align = "right") /
-zoo::rollmax(-subset(PX_LAST, select = -DATE), 53, sd, fill = NA, align = "right"))
Volatility1 <- Vol * 0.29 + Range * 0.22
Volatility1 <- cbind.data.frame(DATE, Volatility1)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Volatility1 <- Volatility1[Index,]
#Normalized
Volatility1 <- Utils.Normalize(Volatility1)
DATE <- BETA$DATE
Volatility2 <- subset(BETA, select = -DATE) * 0.24 +
subset(SIGMA, select = -DATE) * 0.25
Volatility2 <- cbind.data.frame(DATE, Volatility2)
Index <- match(Period, DATE)
Volatility2 <- Volatility2[Index,]
#Normalized
Volatility2 <- Utils.Normalize(Volatility2)
DATE <- Period
Volatility <- cbind.data.frame(DATE, subset(Volatility1, select = -DATE) + subset(Volatility2, select = -DATE))
Volatility <- Utils.Normalize(Volatility)
return(Volatility)
}
#Combination of the following descriptors:
#Total Asset Growth (26%)
#Sales Growth (19%)
#Earnings Growth (23%)
#Forecast of Earnings Growth (11%)
#Forecast of Sales Growth (21%)
Factor.Growth <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
TOT_ASSET <- Utils.CleanData("TOT_ASSET", Ref.Date, FALSE, 5)
SALES <- Utils.CleanData("SALES", Ref.Date, FALSE, 5)
INCOME <- Utils.CleanData("INCOME", Ref.Date, FALSE, 5)
TAG <- as.data.frame(zoo::rollapply(subset(TOT_ASSET, select = -DATE), 265, function(x) (tail(x,1) - head(x,1)), fill = NA, align = "right")) /
as.data.frame(zoo::rollsum(subset(TOT_ASSET, select = -DATE), 265, na.pad = TRUE, align = "right"))
SG <- as.data.frame(zoo::rollapply(subset(SALES, select = -DATE), 265, function(x) (tail(x,1) - head(x,1)), fill = NA, align = "right")) /
as.data.frame(zoo::rollsum(subset(SALES, select = -DATE), 265, na.pad = TRUE, align = "right"))
IG <- as.data.frame(zoo::rollapply(subset(INCOME, select = -DATE), 265, function(x) (tail(x,1) - head(x,1)), fill = NA, align = "right")) /
as.data.frame(zoo::rollsum(subset(INCOME, select = -DATE), 265, na.pad = TRUE, align = "right"))
DATE <- TOT_ASSET$DATE
Growth1 <- TAG * 0.26 + SG * 0.19 + IG * 0.23
Growth1 <- cbind.data.frame(DATE, Growth1)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Growth1 <- Growth1[Index,]
BEST_EPS1 <- Utils.CleanData("BEST_EPS1", Ref.Date, FALSE, 0)
BEST_EPS2 <- Utils.CleanData("BEST_EPS2", Ref.Date, FALSE, 0)
BEST_SALES1 <- Utils.CleanData("BEST_SALES1", Ref.Date, FALSE, 0)
BEST_SALES2 <- Utils.CleanData("BEST_SALES2", Ref.Date, FALSE, 0)
BEG <- as.data.frame(subset(BEST_EPS2, select = -DATE)/subset(BEST_EPS1, select = -DATE) - 1)
BSG <- as.data.frame(subset(BEST_SALES2, select = -DATE)/subset(BEST_SALES1, select = -DATE) - 1)
DATE <- BEST_EPS1$DATE
Growth2 <- BEG * 0.11 + BSG * 0.21
Growth2 <- cbind.data.frame(DATE, Growth2)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Growth2 <- Growth2[Index,]
DATE <- Period
Growth <- cbind.data.frame(DATE, subset(Growth1, select = -DATE) + subset(Growth2, select = -DATE))
Growth <- Utils.Normalize(Growth)
return(Growth)
}
Factor.Leverage <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
DEBT <- Utils.CleanData("DEBT", Ref.Date, FALSE, 0)
BOOK_VAL <- Utils.CleanData("BOOK_VAL", Ref.Date, FALSE, 0)
MKT_CAP <- Utils.CleanData("MKT_CAP", Ref.Date, FALSE, 0)
TOT_ASSET <- Utils.CleanData("TOT_ASSET", Ref.Date, FALSE, 0)
DATE <- TOT_ASSET$DATE
BLeverage <- as.data.frame(subset(DEBT, select = -DATE) / (subset(BOOK_VAL, select = -DATE) + subset(DEBT, select = -DATE)))
MLeverage <- as.data.frame(subset(DEBT, select = -DATE) / (subset(MKT_CAP, select = -DATE) + subset(DEBT, select = -DATE)))
DTRAtio <- as.data.frame(subset(DEBT, select = -DATE) / subset(TOT_ASSET, select = -DATE))
Leverage <- 0.34 * BLeverage + 0.33 * MLeverage + 0.33 * DTRAtio
Leverage <- cbind.data.frame(DATE, Leverage)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Leverage <- Leverage[Index,]
Leverage <- Utils.Normalize(Leverage)
return(Leverage)
}
#Liquidity captures stocks' trading characteristics in terms of price impact, bid-ask spread and trade frequency.
Factor.Liquidity <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
CHG_PCT <- Utils.CleanData("CHG_PCT", Ref.Date, FALSE, 1)
TURNOVER <- Utils.CleanData("TURNOVER", Ref.Date, FALSE, 1)
DATE <- CHG_PCT$DATE
Liquidity <- cbind.data.frame(DATE, lapply(abs(subset(CHG_PCT, select = -DATE)) / sqrt(subset(TURNOVER, select = -DATE)),
(function(x) TTR::EMA(x, n = 53, ratio = log(2)/90))))
Period <- Factor.Period(Ref.Date)
Index <- match(Period, DATE)
Trade <- Liquidity[Index,]
Trade <- Utils.Normalize(Trade)
return(Trade)
}
#Response Variable: Return
Factor.Return <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
#Bloomberg Data
CHG_PCT <- Utils.CleanData("CHG_PCT", Ref.Date, FALSE, 0)
Period <- Factor.Period(Ref.Date)
Index <- match(Period, CHG_PCT$DATE)
Return <- CHG_PCT[Index,]
return(Return)
}
#Utils: Regression Period
Factor.Period <-
function(Ref.Date = NULL)
{
if(is.null(Ref.Date))
Ref.Date <- Sys.Date()
#Find Last Friday
Ref.Date <- Ref.Date - (as.POSIXlt(Ref.Date)$wday + 2) %% 7
filepath <- paste("Factor/Period", format(Ref.Date,"%Y%m%d"), ".RData", sep = "")
if(file.exists(filepath)){
load(filepath)
}else{
S.Date <- seq(Ref.Date, by = "-1 years", length = 2)[2]
E.Date <- Ref.Date
Options <- structure(c("WEEKLY","NON_TRADING_WEEKDAYS","PREVIOUS_VALUE"),
names = c("periodicitySelection","nonTradingDayFillOption","nonTradingDayFillMethod"))
DATE <- bdh("SHSZ300 Index","PX_LAST", start.date = S.Date, end.date = E.Date, options = Options)$date
save(DATE, file = filepath)
}
return(DATE)
}
#Utils: Names
Factor.Names <-
function()
{
Names <- list()
#Style Factor
Names[["Style"]] <-
c("Momentum","Value","DivYld","Size","Trade","EarnVariab","Profit","Volatility","Growth","Leverage","Liquidity")
#Industry Factor
Names[["Industry"]] <-
c(
"Banks", "Real Estate",
"Transportation", "Capital Goods",
"Utilities", "Health Care Equipment & Servic",
"Software & Services", "Technology Hardware & Equipmen",
"Materials", "Food & Staples Retailing",
"Consumer Services", "Consumer Durables & Apparel",
"Diversified Financials", "Pharmaceuticals, Biotechnology",
"Automobiles & Components", "Food Beverage & Tobacco",
"Media", "Commercial & Professional Serv",
"Energy", "Retailing",
"Semiconductors & Semiconductor", "Telecommunication Services",
"Insurance", "Household & Personal Products"
)
return(Names)
}