-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-ChartPrepare.R
247 lines (202 loc) · 8.28 KB
/
3-ChartPrepare.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
# 3-ChartPrepare.R
# Prepare charts for report
# load required data
load("tokenList.rdata")
load("data_r.rdata")
load("data_p.rdata")
load("coinSummary.rdata")
load("sp500.rdata")
load("data_dcc.rdata")
# format the data
date_r<-as.Date(data_r[,1], format="%m/%d/%Y")
date_p<-as.Date(data_p[,1], format="%m/%d/%Y")
date_vec<-date_r
data_r[,-1] <- lapply(data_r[,-1], function(x) as.numeric(x))
data_p[,-1] <- lapply(data_p[,-1], function(x) as.numeric(x))
data_dcc[,-1]<- lapply(data_dcc[,-1],function(x) as.numeric(x))
coinSummary_2 <- gsub('_USDT','',coinSummary$coin)
coinSummary_2 <- gsub('_USD','',coinSummary_2)
# =====================================
# # Chart1 - plot dcc with price index
# create xaxis label
# time_label_m<-strftime(seq(date_vec[1], date_vec[length(date_vec)], by = "month"), format="%b")
date_vec<-date_p
time_label_y<-strftime(seq(date_vec[1], date_vec[length(date_vec)], by = "quarter"), format="%Y")
time_label<-character()
for (i in (1:length(time_label_y))){
if (i %% 4 == 1){
time_label[i]<-time_label_y[i]
} else if (i %% 4 == 2){
time_label[i]<-"Q2"
} else if (i %% 4 == 3) {
time_label[i]<-"Q3"
} else {
time_label[i]<-"Q4"
}
}
timeFreq<-"quarter"
# yaxis label
y_label<-seq(-1, 1, by=0.2)
# plot DCC data series-daily
i<-1; j<-9
for (i in (1:length(coinSummary$coin))){
for (j in (1:length(coinSummary$coin))){
data1_p<-eval(parse(text=paste0("data_p$",coinSummary[i,1],"_p")))
data2_p<-eval(parse(text=paste0("data_p$",coinSummary[j,1],"_p")))
data1_p<-data1_p*(1/data1_p[max(which(data1_p>0)[1],424)]) # use 3/1/2017 as baseline if it is higher
data2_p<-data2_p*(1/data2_p[max(which(data2_p>0)[1],424)])
vec_length <-length(data1_p)
# create xts object for plot
if (i==j){
dcc_vec <- rep(1,vec_length)
} else if(j>i) {
dcc_vec <- eval(parse(text = paste0("data_dcc$", colnames(data_r)[i+1],"_", colnames(data_r)[j+1])))
} else {
dcc_vec <- eval(parse(text = paste0("data_dcc$", colnames(data_r)[j+1],"_", colnames(data_r)[i+1])))
}
#
postscript(file.path(folderDccPic,paste0(tokenList[i,2],"usd",tokenList[j,2],"usdDCC.eps")), fonts="serif",
width = 15, height =7.5, horizontal = TRUE, family= "serif", paper ="special")
# plot fucntion
plotDccdata(date_vec, time_label, y_label, timeFreq, dcc_vec, data1_p, data2_p, coinSummary_2, i, j)
# add sub plot of volume
dev.off()
message(i," , ", j)
}
}
# ===================================
# Chart1.5 - DCC since March 1 2018
date_vec<-date_p[startIndex2:endIndex]
time_label_y<-strftime(seq(date_vec[1], date_vec[length(date_vec)], by = "month"), format="%b")
time_label<-time_label_y
timeFreq<-"month"
# # create xaxis label
# # time_label_m<-strftime(seq(date_vec[1], date_vec[length(date_vec)], by = "month"), format="%b")
# time_label_y<-strftime(seq(date_vec[1], date_vec[length(date_vec)], by = "quarter"), format="%Y")
# time_label<-character()
# for (i in (1:length(time_label_y))){
# if (i %% 4 == 1){
# time_label[i]<-time_label_y[i]
# } else if (i %% 4 == 2){
# time_label[i]<-"Q2"
# } else if (i %% 4 == 3) {
# time_label[i]<-"Q3"
# } else {
# time_label[i]<-"Q4"
# }
# }
# yaxis label
y_label<-seq(-1, 1, by=0.2)
# plot DCC data series-daily
i<-1; j<-9
for (i in (1:length(coinSummary$coin))){
for (j in (1:length(coinSummary$coin))){
data1_p<-eval(parse(text=paste0("data_p$",coinSummary[i,1],"_p")))
data2_p<-eval(parse(text=paste0("data_p$",coinSummary[j,1],"_p")))
data1_p<-data1_p*(1/data1_p[max(which(data1_p>0)[1],424)]) # use 3/1/2017 as baseline if it is higher
data2_p<-data2_p*(1/data2_p[max(which(data2_p>0)[1],424)])
data1_p<-data1_p[startIndex2:endIndex]
data2_p<-data2_p[startIndex2:endIndex]
vec_length <-length(data1_p)
# create xts object for plot
if (i==j){
dcc_vec <- rep(1,vec_length)
} else if(j>i) {
dcc_vec <- eval(parse(text = paste0("data_dcc$", colnames(data_r)[i+1],"_", colnames(data_r)[j+1])))[startIndex2:endIndex]
} else {
dcc_vec <- eval(parse(text = paste0("data_dcc$", colnames(data_r)[j+1],"_", colnames(data_r)[i+1])))[startIndex2:endIndex]
}
#
postscript(file.path(folderDccPic,paste0(tokenList[i,2],"usd",tokenList[j,2],"usdDCCMar2018.eps")), fonts="serif",
width = 15, height =7.5, horizontal = TRUE, family= "serif", paper ="special")
# plot fucntion
plotDccdata(date_vec, time_label, y_label, timeFreq, dcc_vec, data1_p, data2_p, coinSummary_2, i, j)
# add sub plot of volume
dev.off()
message(i," , ", j)
}
}
message("DCC since March 2018 is done~!")
# ===================================
# Chart2 - 30 DCC in one Chart
postscript(file.path(folderPic,paste0("30DCCbtcLegend.eps")), fonts="serif",
width = 15, height =7.5, horizontal = TRUE, family= "serif", paper ="special")
plot30Dcc(date_vec, time_label, y_label, data_dcc, data_p, tokenList)
# add sub plot of volume
dev.off()
message("DCC Chart is done~!")
# ===================================
# Chart3 - 30 Price in one chart since Jan 2018
date_vec<-date_p[startIndex1:endIndex]
time_label_y<-strftime(seq(date_vec[1], date_vec[length(date_vec)], by = "month"), format="%b")
time_label<-time_label_y
postscript(file.path(folderPic,paste0("30PriceBaseline300Jan18Legend.eps")), fonts="serif",
width = 15, height =7.5, horizontal = TRUE, family= "serif", paper ="special")
plot30Price(date_vec, time_label, data_p, tokenList, startIndex1, endIndex)
# add sub plot of volume
dev.off()
message("Price Chart is done~!")
# ===================================
# Chart4 - 30 Price in one chart since Mar 2018
date_vec<-date_p[startIndex2:endIndex]
time_label_y<-strftime(seq(date_vec[1], date_vec[length(date_vec)], by = "month"), format="%b")
time_label<-time_label_y
postscript(file.path(folderPic,paste0("30PriceBaseline300March18Legend.eps")), fonts="serif",
width = 15, height =7.5, horizontal = TRUE, family= "serif", paper ="special")
plot30Price(date_vec, time_label, data_p, tokenList, startIndex2, endIndex)
# add sub plot of volume
dev.off()
message("Price Chart is done~!")
# ===================================
# Chart5 - Exchange Daily Rev Rank.eps
fileName<-paste0("ex_daily_rev.csv")
datatemp<-read.table(file.path(folderInput, fileName), header=TRUE, sep=",", stringsAsFactors = FALSE, comment.char = "")
datatemp [datatemp==0]<-NA
assign("exDailyRev",datatemp)
postscript(file.path(folderPic,paste0("ExchangeDailyRevRank", ".eps")), fonts="serif",
width = 15, height =7.5, horizontal = TRUE, family= "serif", paper ="special")
plotExRev(exDailyRev)
dev.off()
# ===================================
# Chart6 - BNB price and volume chart
# read data of BNB
i<-30
fileName<-paste0("binance-coin_usd_daily_all.csv")
datatemp<-loadDailyCryptoData(folderCmc, fileName, tokenList, i)
assign(paste0(tokenList$fsym[i], "_", tokenList$tsym[i]),datatemp)
# create xaxis label
date_vec<-as.Date(BNB_USD$date, format="%m/%d/%Y")
time_label_y<-strftime(seq(date_vec[1], date_vec[length(date_vec)], by = "quarter"), format="%Y")
time_label<-character()
for (i in (1:length(time_label_y))){
if ((i+2) %% 4 == 1){
time_label[i]<-time_label_y[i]
} else if ((i+2) %% 4 == 2){
time_label[i]<-"Q2"
} else if ((i+2) %% 4 == 3) {
time_label[i]<-"Q3"
} else {
time_label[i]<-"Q4"
}
}
postscript(file.path(folderPic,paste0("BNBusdPriceVolume2017-07-25", ".eps")), fonts="serif",
width = 15, height =7.5, horizontal = TRUE, family= "serif", paper ="special")
# plot BNB price with volume
plotBnbPriceVolume(date_vec, time_label, BNB_USD)
dev.off()
message("BNB Price Volume chart is done!")
# ===================================
# Chart7 - 13 crypto correlation matrix chart
data_r1<-data_r[startIndex2:endIndex,c(1:11, 31, 22, 25)]
data_r1$date <- as.Date(data_r1$date, format="%m/%d/%Y")
data_r1[, -1]<-as.numeric(unlist(data_r1[,-1]))
token_var <-tokenList$fsym[c(1:10, 30, 21, 24)]
colnames(data_r1)<-c("date", token_var)
# cross correation matrix for *_btc
cryptoData<-data_r1[,-1]
cormat<-round(cor(cryptoData),2)
postscript(file.path(folderPic,paste0("13CryptoCorrMatrixMar2018.eps")), fonts="serif",
width = 15, height =7.5, horizontal = TRUE, family= "serif", paper ="special")
plotCorrMatrix(cormat)
dev.off()
# ===================================