-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimulateForecasts.R
109 lines (88 loc) · 4.34 KB
/
simulateForecasts.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
volatilityAdjustedForecast <- function(price.xts, raw.forecast){
price.xts <- if (is.OHLC(price.xts)) {
Cl(price.xts)
}
else price.xts
volatility.ema <- emaVolatility(price.xts)
volatility.adjusted.forecast <- (raw.forecast)/(volatility.ema * price.xts)
# Get rid of infinities when there is no volatility etc.
volatility.adjusted.forecast <- replace(volatility.adjusted.forecast,
which(volatility.adjusted.forecast==-Inf),
c(-100))
volatility.adjusted.forecast <- replace(volatility.adjusted.forecast,
which(volatility.adjusted.forecast==Inf),
c(100))
return(volatility.adjusted.forecast)
}
scaledForecast <- function(volatility.adjusted.forecast){
# apply.fromstart function doesn't work and is too slow. Consider using roll or RcppROll package for true scalar
# forecast.scalar <- 10/apply.fromstart(abs(volatility.adjusted.ema), "mean")
weighted.forecast.scalar <- 10/SMA(abs(volatility.adjusted.forecast),n=system.config$volatility.lookback*12)
scaled.forecast <- volatility.adjusted.forecast * weighted.forecast.scalar
# mean(abs(scaled.forecast), na.rm=T) # should be ~10 before capping
return(scaled.forecast)
}
cappedForecast <- function(scaled.forecast){
forecast.max <- system.config$forecast.cap
forecast.min <- -1 * forecast.max
capped.forecast <- xts(x=pmax(forecast.min,pmin(forecast.max, scaled.forecast)), order.by = index(scaled.forecast))
return(capped.forecast)
}
cappedScaledForecast <- function(price.xts=NULL, forecast.name=NULL, ...){
price.xts <- if (is.OHLC(price.xts)) {
Cl(price.xts)
}
else price.xts
raw.forecast <- do.call(forecast.name, args=list(price.xts))
volatility.adjusted.forecast <- volatilityAdjustedForecast(price.xts, raw.forecast)
scaled.forecast <- scaledForecast(volatility.adjusted.forecast)
capped.forecast <- cappedForecast(scaled.forecast)
return(capped.forecast)
}
rawForecastWeights <- function(){
forecast.return.path <- "data/clean/forecast_returns.RDS"
raw.forecast.weights <- rawWeights(return.path=forecast.return.path)
saveRDS(raw.forecast.weights, relativePath("/data/clean/raw_forecast_weights.RDS"))
plotWeights(weights.var=raw.forecast.weights, weights.name="raw_forecast_weights")
return()
}
smoothedForecastWeights <- function(){
raw.forecast.weights.path <- "/data/clean/raw_forecast_weights.RDS"
smoothed.forecast.weights <- smoothedWeights(raw.weights.path=raw.forecast.weights.path)
saveRDS(smoothed.forecast.weights, relativePath("/data/clean/smoothed_forecast_weights.RDS"))
plotWeights(weights.var=smoothed.forecast.weights, weights.name="smoothed_forecast_weights")
return(smoothed.forecast.weights)
}
simulateForecasts <- function(){
start.time <- Sys.time()
portfolio.forecasts <- system.config$portfolio.forecasts
portfolio.pairs <- system.config$portfolio.pairs
results.matrix <-
foreach(forecast.name=portfolio.forecasts, .combine='merge') %dopar% {
poolForecasts(forecast.name=forecast.name)
}
results.matrix <- na.omit(results.matrix)
forecast.returns <- Return.calculate(results.matrix)[2:nrow(results.matrix),]
saveRDS(forecast.returns, file=relativePath("/data/clean/forecast_returns.RDS"))
execution.time <- round(difftime(Sys.time(), start.time, units="mins"),2)
print(paste0("Finished simulating forecast returns in ",execution.time," mins."))
}
poolForecasts <- function(forecast.name=NULL){
portfolio.pairs <- system.config$portfolio.pairs
pooled.results <- foreach(pair=portfolio.pairs, .combine="merge", .multicombine=FALSE
# , .export="system.config"
, .verbose=TRUE
) %dopar% {
# forecast.name="no_forecast_long"
# system.config$live
# result.name <- paste(pair, forecast.name, sep="_")
price.xts <- getPairData(pair=pair)
capped.scaled.forecast <- simulateBacktest(pair=pair, forecast.name=forecast.name)
names(capped.scaled.forecast) <- pair
return(capped.scaled.forecast)
}
file.name <- paste0("/data/clean/",forecast.name,"_forecast_returns.RDS")
saveRDS(pooled.results, file=relativePath(file.name))
pooled.forecast <- rowSumXts(pooled.results, name=forecast.name)/length(portfolio.pairs)
return(pooled.forecast)
}