@@ -52,7 +52,12 @@ bsGSfit <- llply(dateID, function(dt) {
52
52
if (! dir.exists(pth )) dir.create(pth )
53
53
54
54
# # predict dateID onwards from data < dateID
55
- smp = filter(LADDT , Date < dt & Date > = (dt - years(1 )))
55
+ # # for example : in order to predict today price, I used the data from
56
+ # # 1 years (365 days or 366 days for leap years.) ago from yesterday.
57
+ # '@ ymd("2016-2-29") %m-% years(1)
58
+ # # http://stackoverflow.com/questions/8490799/how-to-account-for-leap-years
59
+
60
+ smp = filter(LADDT , Date < dt & Date > = (dt %m - % years(1 )))
56
61
gsfit = compStocks(smp , family = families [1 ], xy.matrix = ' h2' , yv.lm = c(TRUE , FALSE ),
57
62
yv = c(' open1' , ' open2' , ' high1' , ' high2' , ' low1' , ' low2' , ' close1' , ' close2' , ' daily.mean1' , ' daily.mean2' , ' daily.mean3' , ' mixed1' , ' mixed2' , ' mixed3' ),
58
63
pred.type = ' class' , .print = TRUE , .save = TRUE , pth = pth )
@@ -110,21 +115,96 @@ bsGSfit <- llply(dateID, function(dt) {
110
115
}
111
116
})
112
117
118
+ # # ================================= Save Basic Model ========================================
119
+ # # Arrange the best fit and save the model summary.
120
+ files <- list.files(' ./data' , pattern = ' [0-9]{8}' )
121
+ # '@ pth <- paste0('./data/', files, '/fitgaum.mse1.rds')
122
+
123
+ # # list all daily minimum standard error models.
124
+ pre.mse1 <- ldply(files , function (x ) {
125
+ y = read_rds(path = paste0(' ./data/' , x , ' /fitgaum.mse1.rds' ))
126
+ y %> % data.frame (Date = x , . ) %> % tbl_df %> % filter(mse == min(mse ))
127
+ }) %> % tbl_df
128
+
129
+ # # filter and only get first unique element since the mse is same.
130
+ # '@ unique(pre.mse1[c('Date', 'model', 'mse')])
131
+ pre.mse1 <- pre.mse1 [! duplicated(pre.mse1 $ Date , pre.mse1 $ mse ), ]
132
+ saveRDS(pre.mse1 , file = ' ./data/pre.mse1.rds' )
133
+ # > pre.mse1
134
+ # A tibble: 517 × 4
135
+ # Date .id model mse
136
+ # <fctr> <chr> <fctr> <dbl>
137
+ # 1 20150102 fitgaum119 mse9 0.1209190
138
+ # 2 20150105 fitgaum135 mse9 0.1211786
139
+ # 3 20150106 fitgaum132 mse3 0.1210193
140
+ # 4 20150107 fitgaum81 mse8 0.1221952
141
+ # 5 20150108 fitgaum144 mse3 0.1220447
142
+ # 6 20150109 fitgaum27 mse3 0.1210785
143
+ # 7 20150112 fitgaum129 mse3 0.1211022
144
+ # 8 20150113 fitgaum17 mse3 0.1214222
145
+ # 9 20150114 fitgaum28 mse3 0.1215031
146
+ # 10 20150115 fitgaum18 mse3 0.1208796
147
+
148
+ # # list all daily minimum standard error models' formula.
149
+ pre.gsform <- ldply(files , function (x ) {
150
+ y = read_rds(path = paste0(' ./data/' , x , ' /fitgaum.form.rds' ))
151
+ z = paste0(substr(x , 1 , 4 ), ' -' , substr(x , 5 , 6 ), ' -' , substring(x , nchar(x ) - 1 )) %> %
152
+ ymd
153
+ data.frame (Date = z , form = y ) %> % tbl_df
154
+ }) %> % tbl_df
155
+
156
+ # # filter and only get first unique element since the mse is same.
157
+ pre.gsform <- pre.gsform [! duplicated(pre.gsform $ Date ), ]
158
+ saveRDS(pre.gsform , file = ' ./data/pre.gsform.rds' )
159
+
160
+ # # ================================= Weighted Model ==========================================
161
+ # # Application bayesian as weighted function and MCMC for prediction.
162
+
163
+ # # Read summary of the best fit model
164
+ # # From below table, we noted that the dynamic model required compare
165
+ # # to using constant model across the days as we can know from column `.id`.
166
+ pre.mse1 <- read_rds(path = ' ./data/pre.mse1.rds' )
167
+ # > pre.mse1
168
+ # A tibble: 517 × 4
169
+ # Date .id model mse
170
+ # <fctr> <chr> <fctr> <dbl>
171
+ # 1 20150102 fitgaum119 mse9 0.1209190
172
+ # 2 20150105 fitgaum135 mse9 0.1211786
173
+ # 3 20150106 fitgaum132 mse3 0.1210193
174
+ # 4 20150107 fitgaum81 mse8 0.1221952
175
+ # 5 20150108 fitgaum144 mse3 0.1220447
176
+ # 6 20150109 fitgaum27 mse3 0.1210785
177
+ # 7 20150112 fitgaum129 mse3 0.1211022
178
+ # 8 20150113 fitgaum17 mse3 0.1214222
179
+ # 9 20150114 fitgaum28 mse3 0.1215031
180
+ # 10 20150115 fitgaum18 mse3 0.1208796
181
+
182
+ # # Read best fit model.
183
+ pre.gsform <- read_rds(path = ' ./data/pre.gsform.rds' )
184
+
185
+
186
+ # # ============================== Save Weighted Model ========================================
187
+ # # Arrange the best fit and save the data.
188
+ # #
189
+ llply(seq(nrow(pre.mse1 )), function (i ) {
190
+ y = read_rds(path = paste0(' ./data/' , pre.mse1 $ Date [i ], ' /' , pre.mse1 $ .id [i ], ' .rds' ))
191
+ j = filter(y $ mse , mse == pre.mse1 $ mse [i ]) %> % . $ model %> % str_replace_all(' mse' , ' ' ) %> %
192
+ as.numeric
193
+ y $ yhat [j ] %> % unlist
194
+ })
113
195
196
+ files <- list.files(' ./data/20150102/' , pattern = ' fitgaum+[0-9]{1,}.rds$' )
114
197
198
+ wt <- ldply(files , function (x ) {
199
+ y = read_rds(path = paste0(' ./data/' , x , ' /' , x ))
200
+ y %> % data.frame (Date = x , . ) %> % tbl_df %> % filter(mse == min(mse ))
201
+ }) %> % tbl_df
115
202
116
203
117
204
118
-
119
- # # ================================= Weighted Model ===========================================
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
205
+ # # ==================================== MCMC Model ===========================================
206
+ # # Application of MCMC to simulate the Profit and Loss of opt.Kelly() and optimal.f()
207
+ # # staking model.
128
208
129
209
130
210
0 commit comments