-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze.R
371 lines (319 loc) · 10.1 KB
/
analyze.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
analyze <- function() {
#######################
###### TEST DATA ######
s <- 1 # 1304 台聚
y <- 3 # 2018
r <- 13 # ratio
#######################
#
# ratio list
ratio <- c(
"負債佔資產比率", "長期資金佔固定資產比率", "流動比率", "速動比率",
"利息保障倍數", "應收帳款週轉率", "平均收現日數", "存貨週轉率", "平均售貨日數",
"固定資產週轉率", "總資產週轉率", "資產報酬率", "股東權益報酬率", "純益率", "每股盈餘",
"現金流量比率", "現金再投資比率"
)
# reset key value
s <- y <- r <- ""
#
################################# Input Menu #################################
#
###### stock ######
while ((stock[s] %in% stock) == FALSE) {
li <- paste0(1:length(stock), sep = ".", stock)
s <- readline(cat("請輸入要分析的股票代號:", li, sep = "\n")) # input
if (s == "exit") {
break
} else {
s <- as.numeric(s)
}
###### year ######
while ((stock[s] %in% stock) == TRUE &
(year[y] %in% year) == FALSE) {
li <- paste0(1:(length(year) - 2), sep = ".", year[3:length(year)])
y <- readline(cat("請輸入要分析的年度:", li, sep = "\n")) # input
if (y == "exit") {
break
} else {
y <- as.numeric(y) + 2
}
###### ratio ######
while ((stock[s] %in% stock) == TRUE &
(year[y] %in% year) == TRUE &
(ratio[r] %in% ratio) == FALSE) {
li <- paste0(1:length(ratio), sep = ".", ratio)
r <- readline(cat("請輸入要分析的比率:", li, sep = "\n")) # input
if (r == "exit") {
break
} else {
r <- as.numeric(r)
}
}
}
}
##############################################################################
#
###### magic for analyze ######
magic_black_box <- function(mAcc) {
m1 <- m2 <- c("", 0) # max change (account title, value)
t2 <- "" # result
for (m in mAcc) {
# calculate
if (m %in% mAcc_income) { # check mAcc in income
rp <- income_dta[which(mAcc_income == m), (y - 1), s]
rc <- income_dta[which(mAcc_income == m), y, s]
} else if (m %in% mAcc_balances) { # check mAcc in balance
rp <- balance_dta[which(mAcc_balances == m), (y - 1), s]
rc <- balance_dta[which(mAcc_balances == m), y, s]
} else if (m %in% mAcc_cash) { # check mAcc in cash
rp <- cash_dta[which(mAcc_cash == m), (y - 1), s]
rc <- cash_dta[which(mAcc_cash == m), y, s]
} else {
rp <- rc <- 0
}
n <- rc - rp
# add to t2
if (n > 0) {
t <- paste(m, "與前年相比增加了", percent(n / rp, accuracy = 0.01), "%")
} else if (n < 0) {
t <- paste(m, "與前年相比減少了", percent(abs(n) / rp, accuracy = 0.01), "%")
} else {
t <- paste(m, "與前年相同")
}
t2 <- paste(t2, t, sep = "\n")
# find 2 max change account title & value
if(rp==0){
break
}
else if (abs(n) / rp > as.numeric(m1[2])) {
temp <- m1
m1 <- c(m, abs(n) / rp)
m2 <- temp
} else if (abs(n) / rp > as.numeric(m2[2])) {
m2 <- c(m, abs(n) / rp)
}
}
# add max change to t2
t2 <- paste(t2, "\n 其中變動最大的為", m1[1], "變動第二大的為", m2[1])
return(t2)
}
##############################################################################
#
ratio_cr <-
ratio_dta1[r, y - 1, s] - ratio_dta1[r, (y - 2), s] # ratio change rate
# add to t1
if (ratio_cr > 0) {
t1 <-
paste(ratio[r], "增加了", percent(ratio_cr, accuracy = 0.01))
} else if (ratio_cr < 0) {
t1 <-
paste(ratio[r], "減少了", percent(abs(ratio_cr), accuracy = 0.01))
} else {
t1 <- paste(ratio[r], "不變")
}
############################################################################
#
# 負債佔資產比率
if (r == 1) {
mAcc <- c(
# 分子
" 其他流動負債", " 流動負債合計", " 非流動負債合計",
" 負債總額"
# 分母
, " 應收票據淨額", " 應收帳款淨額", " 其他應收款淨額",
" 存貨", " 預付款項", " 流動資產合計", " 非流動資產合計", " 資產總額", " 應付帳款"
)
t2 <- magic_black_box(mAcc)
}
# 長期資金佔固定資產比率
if (r == 2) {
mAcc <- c(
# 分子
" 普通股股本", " 股本合計", " 資本公積合計", " 保留盈餘合計",
" 其他權益合計", " 權益總額"
# 分母
, " 不動產、廠房及設備", " 投資性不動產淨額"
)
t2 <- magic_black_box(mAcc)
}
# 流動比率
if (r == 3) {
mAcc <- c(
# 分子
" 應收票據淨額", " 應收帳款淨額", " 其他應收款淨額"
# 分母
, " 短期借款",
" 應付短期票券",
" 應付帳款",
" 其他流動負債"
)
t2 <- magic_black_box(mAcc)
}
# 速動比率
if (r == 4) {
mAcc <- c(
# 分子
" 應收票據淨額", " 應收帳款淨額", " 其他應收款淨額",
" 存貨", " 預付款項",
# 分母
" 短期借款",
" 應付短期票券",
" 應付帳款",
" 其他流動負債"
)
t2 <- magic_black_box(mAcc)
}
# 利息保障倍數
if (r == 5) {
mAcc <- c(
# 分子
" 銷貨收入淨額", " 銷貨成本", " 營業費用合計"
# 分母
, " 支付之利息"
)
t2 <- magic_black_box(mAcc)
}
# 應收帳款週轉率
if (r == 6) {
mAcc <- c(
# 分子
" 銷貨收入淨額"
# 分母
, " 應收票據淨額", " 應收帳款淨額", "應收帳款-關係人淨額")
)
t2 <- magic_black_box(mAcc)
}
# 平均收現日數
if (r == 7) {
mAcc <- c(
# 分子
" 銷貨收入淨額"
# 分母
, " 應收票據淨額", " 應收帳款淨額", "應收帳款-關係人淨額")
)
t2 <- magic_black_box(mAcc)
}
# 存貨週轉率
if (r == 8) {
mAcc <- c(
# 分子
" 銷貨成本"
# 分母
, " 存貨"
)
t2 <- magic_black_box(mAcc)
}
# 平均售貨日數
if (r == 9) {
mAcc <- c(
# 分子
" 銷貨成本"
# 分母
, " 存貨"
)
t2 <- magic_black_box(mAcc)
}
# 固定資產週轉率
if (r == 10) {
mAcc <- c(
# 分子
" 銷貨收入淨額"
# 分母
, " 不動產、廠房及設備"
)
t2 <- magic_black_box(mAcc)
}
# 總資產週轉率
if (r == 11) {
mAcc <- c(
# 分子
" 銷貨收入淨額"
# 分母
, " 流動資產合計", " 非流動資產合計", " 資產總額"
)
t2 <- magic_black_box(mAcc)
}
# 資產報酬率
if (r == 12) {
mAcc <- c(
# 分子
"營業收入合計", "營業成本合計", " 營業費用合計", "營業利益(損失)",
" 營業外收入及支出合計", "稅前淨利(淨損)", "所得稅費用(利益)合計",
"繼續營業單位本期淨利(淨損)", " 停業單位損益合計", " 利息費用"
# 分母
, " 流動資產合計", " 非流動資產合計", " 資產總額"
)
t2 <- magic_black_box(mAcc)
}
# 股東權益報酬率
if (r == 13) {
mAcc <- c(
# 分子
"營業收入合計", "營業成本合計", " 營業費用合計", "營業利益(損失)",
" 營業外收入及支出合計", "稅前淨利(淨損)", "所得稅費用(利益)合計",
"繼續營業單位本期淨利(淨損)", " 停業單位損益合計"
# 分母
, " 歸屬於母公司業主之權益合計", " 非控制權益", " 權益總額"
)
t2 <- magic_black_box(mAcc)
}
# 純益率
if (r == 14) {
mAcc <- c(
# 分子
"營業收入合計", "營業成本合計", " 營業費用合計", "營業利益(損失)",
" 營業外收入及支出合計", "稅前淨利(淨損)", "所得稅費用(利益)合計",
"繼續營業單位本期淨利(淨損)", " 停業單位損益合計"
# 分母
, " 銷貨收入淨額"
)
t2 <- magic_black_box(mAcc)
}
# 每股盈餘
if (r == 15) {
mAcc <- c(
# 分子
"營業收入合計", "營業成本合計", " 營業費用合計", "營業利益(損失)",
" 營業外收入及支出合計", "稅前淨利(淨損)", "所得稅費用(利益)合計",
"繼續營業單位本期淨利(淨損)", " 停業單位損益合計"
# 分母
, " 普通股股本"
)
t2 <- magic_black_box(mAcc)
}
# 現金流量比率
if (r == 16) {
mAcc <- c(
# 分子
"本期淨利(淨損)", " 折舊費用",
" 攤銷費用",
" 利息費用",
" 應收帳款(增加)減少",
" 存貨(增加)減少"
# 分母
, " 短期借款",
" 應付短期票券",
" 應付帳款",
" 其他流動負債"
)
t2 <- magic_black_box(mAcc)
}
# 現金再投資比率
if (r == 17) {
mAcc <- c(
# 分子
"本期淨利(淨損)", " 折舊費用",
" 攤銷費用",
" 利息費用",
" 應收帳款(增加)減少",
" 存貨(增加)減少", " 發放現金股利"
# 分母
, " 不動產、廠房及設備",
" 投資性不動產淨額"
)
t2 <- magic_black_box(mAcc)
}
############################################################################
# show result <- stock + year + t1 + t2
cat(paste(stock[s], "在", year[y], "年的", t1, "是因為", t2))
}