-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockJSON.R
More file actions
92 lines (69 loc) · 2.98 KB
/
mockJSON.R
File metadata and controls
92 lines (69 loc) · 2.98 KB
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
library("jsonlite")
#import("jsonlite")
#export("generateJSON")
#### Function that generates a mok JSON file for testing ##########
### Execute function ######
#generateJSON(historicalData, constraintsData, optimizationType, target, dataDirectory)
dataDirectory <- "location of file"
historicalData <- paste0(dataDirectory, "allHistory.csv")
# Historical data to be used. It will take a csv file with historical data of a number of funds.
# Leftmost column must be dates and first row must include some sort of asset identifier.
# It must contain at least 5 consecutive years of daily data.
constraintsData <- paste0(dataDirectory,"constraints.csv")
# The constraints file must be a file where on the leftmost column there is a list of the names of
# assets and next to each name (second column) a percentage maximum allocation (i.e. first row could
# read (GoldFund, 20)).
optimizationType <- "custom"
# One of four possibilities "risk", "riskClass", "return", "custom".
# "risk" optimizes the portfolio to a given volatility level (which will later be chosen as "target")
# "riskClass" optimizes the portfolio to a volatility level out of 1 to 5. 1 corresponding to very low volatility 5 corresponding to 15%.
# "return" optimizes to a chosen level of return. The optimizer will internally cap it at the maximum possible.
# "custom" will not optimize. It will just calculate all statistics for a portfolio with the percentages of allocation entered in the
# constraintsData table.
target <- 7.5
# Double. Must correspond to the chosen method of optimization above.
#### Generating JSON ###########
#generateJSON <-function(csvFile, constraintFile, typeOptimization, target,
# resultLocation){
all_history <-
read.csv(
file = historicalData,
header = TRUE,
sep = ",",
dec = ".",
stringsAsFactors = FALSE,
na.strings = c("NA")
)
n <- ncol(all_history)-1
history_list <- list()
for(i in 1:n){
history_list[[i]] <- data.frame(all_history[ , c(1, i+1)])
names(history_list[[i]])<-c("date", "value")
}
percfunds <- read.csv(file=constraintsData,
header = FALSE)
composition_list <- list()
for(i in 1:n){
composition_list[[i]] <- list()
composition_list[[i]]$AssetTypes <- list()
composition_list[[i]]$AssetRegions <- list()
}
fundsList <- list(
isin = as.vector(percfunds$V1),
#name = as.vector(percfunds$V2),
percent = percfunds$V2,
values = history_list
)
mock <- list(
optimizationMode = optimizationType,
fundPercentageMode = "custom", #"custom"
riskPercentage = c(target),
riskClass = c(target),
return = c(target),
optimizationPeriod = c(5),
LB_active = 0,
funds = fundsList
# isinHistoric = isinHistoricList
#constraint = constraintList,
)
write_json(mock, simplyfyVector=TRUE, pretty=TRUE, path = paste0(dataDirectory, paste0("mockfunds")))