VNDS package

Pham Dinh Khanh

2017-12-29

VNDS là một package đầu tiên trên R cho phép bạn lấy dữ liệu giá chứng khoán của tất cả các mã niêm yết trên sàn HNX, HOSE, UPCOM của thị trường chứng khoán Việt Nam. Mục đích của package này là cung cấp một công cụ khai thác dữ liệu chứng khoán cho những nhà phân tích tài chính bằng ngôn ngữ R. Và không chỉ dừng ở đó, mục tiêu của package hướng tới là phân tích biểu đồ, cung cấp những thống kê mô tả về phân phối, lợi tức của chứng khoán và đưa ra các mô hình tối ưu hóa danh mục dựa trên các mô hình rủi ro thị trường.

KHAI THÁC DỮ LIỆU QUA HÀM getSymbols

Hàm getSymbols là một hàm khai thác dữ liệu có cú pháp đơn giản như sau: getSymbols(ma chung khoan, ngay bat dau, ngay ket thuc)

Trong đó

library(VNDS)
library(rvest)
library(httr)
library(stringr)
library(magrittr)
library(dplyr)
#lay du lieu chung khoan VND tu ngay "2017-09-10" den ngay "2017-12-29"
getSymbols("VND","2017-09-10","2017-12-29")
#> [[1]]
#>                  DATE CHANGE.PERCENT1 CHANGE.PERCENT2  OPEN  HIGH   LOW
#> 2017-12-29 2017-12-29            0.30     0.011494253 26.30 26.90 26.10
#> 2017-12-28 2017-12-28            1.70     0.069672131 24.70 26.10 24.40
#> 2017-12-27 2017-12-27            0.40     0.016666667 24.30 24.80 24.20
#> 2017-12-26 2017-12-26           -0.10     0.004149378 24.30 24.30 23.80
#> 2017-12-25 2017-12-25            0.45     0.019027484 23.65 24.30 23.60
#> 2017-12-22 2017-12-22            0.10     0.004246285 23.35 23.75 23.25
#> 2017-12-21 2017-12-21           -0.15     0.006329114 23.70 23.85 23.50
#> 2017-12-20 2017-12-20            0.10     0.004237288 23.75 24.10 23.40
#> 2017-12-19 2017-12-19            0.95     0.041942605 22.95 23.70 22.75
#> 2017-12-18 2017-12-18            0.65     0.029545455 22.50 22.90 22.20
#> 2017-12-15 2017-12-15            0.05     0.002277904 21.95 22.10 21.75
#> 2017-12-14 2017-12-14            0.60     0.028103044 21.40 21.95 21.40
#> 2017-12-13 2017-12-13           -0.05     0.002336449 21.60 21.80 20.95
#> 2017-12-12 2017-12-12           -0.20     0.009259259 21.80 21.90 20.20
#> 2017-12-11 2017-12-11           -1.15     0.050549451 22.30 22.50 21.60
#> 2017-12-08 2017-12-08           -0.05     0.002192982 22.70 23.10 22.50
#> 2017-12-07 2017-12-07           -0.60     0.025641026 23.50 23.55 22.60
#> 2017-12-06 2017-12-06           -0.10     0.004255319 23.40 23.60 22.70
#> 2017-12-05 2017-12-05           -1.00     0.040816327 24.50 24.50 23.50
#> 2017-12-04 2017-12-04            0.70     0.029411765 24.10 24.95 23.95
#> 2017-12-01 2017-12-01            0.30     0.012765957 23.75 23.95 23.55
#> 2017-11-30 2017-11-30            0.75     0.032967033 23.10 23.70 23.00
#> 2017-11-29 2017-11-29           -0.05     0.002192982 23.25 23.30 22.70
#> 2017-11-28 2017-11-28           -0.05     0.002188184 23.00 23.10 22.25
#> 2017-11-27 2017-11-27            0.65     0.029279279 22.40 23.30 22.20
#> 2017-11-24 2017-11-24            0.20     0.009090909 22.00 22.30 21.60
#> 2017-11-23 2017-11-23           -0.35     0.015659955 22.55 22.60 21.70
#> 2017-11-22 2017-11-22            0.10     0.004494382 22.25 22.50 22.10
#> 2017-11-21 2017-11-21            0.65     0.030092593 21.70 22.40 21.60
#> 2017-11-20 2017-11-20            0.00     0.000000000 21.85 21.85 21.40
#> 2017-11-17 2017-11-17            0.30     0.014117647 21.30 21.95 21.30
#> 2017-11-16 2017-11-16            0.55     0.026570048 20.70 21.35 20.55
#> 2017-11-15 2017-11-15            0.10     0.004854369 20.70 20.90 20.45
#> 2017-11-14 2017-11-14           -0.05     0.002421308 20.90 20.90 20.30
#> 2017-11-13 2017-11-13            1.05     0.053571429 19.60 20.70 19.55
#> 2017-11-10 2017-11-10           -0.15     0.007594937 19.75 19.85 19.60
#> 2017-11-09 2017-11-09            0.35     0.018041237 19.40 19.80 19.20
#> 2017-11-08 2017-11-08            0.10     0.005181347 19.30 19.40 19.00
#> 2017-11-07 2017-11-07           -0.10     0.005154639 19.40 19.50 19.10
#> 2017-11-06 2017-11-06            0.40     0.021052632 19.20 19.40 19.00
#> 2017-11-03 2017-11-03            0.10     0.005291005 19.00 19.10 18.70
#> 2017-11-02 2017-11-02           -0.10     0.005263158 18.80 19.45 18.60
#> 2017-11-01 2017-11-01           -0.50     0.025641026 19.65 19.65 18.20
#> 2017-10-31 2017-10-31           -0.70     0.034653465 20.25 20.30 19.40
#> 2017-10-30 2017-10-30           -0.05     0.002469136 20.25 20.60 20.20
#> 2017-10-27 2017-10-27           -0.05     0.002463054 20.40 20.40 20.15
#> 2017-10-26 2017-10-26           -0.10     0.004901961 20.50 20.80 20.00
#> 2017-10-25 2017-10-25            0.20     0.009900990 20.30 20.50 20.30
#> 2017-10-24 2017-10-24            0.20     0.010000000 20.00 20.40 20.00
#> 2017-10-23 2017-10-23           -0.55     0.026763990 20.40 20.75 19.80
#> 2017-10-20 2017-10-20           -0.25     0.012019231 20.75 20.80 20.50
#> 2017-10-19 2017-10-19           -0.15     0.007159905 20.90 21.10 20.70
#> 2017-10-18 2017-10-18            0.15     0.007211538 21.10 21.35 20.90
#> 2017-10-17 2017-10-17            0.40     0.019607843 20.40 20.80 20.40
#> 2017-10-16 2017-10-16           -0.40     0.019230769 20.80 20.90 20.35
#> 2017-10-13 2017-10-13           -0.15     0.007159905 20.95 20.95 20.75
#> 2017-10-12 2017-10-12            0.00     0.000000000 21.10 21.20 20.95
#> 2017-10-11 2017-10-11           -0.10     0.004750594 21.10 21.20 20.90
#> 2017-10-10 2017-10-10           -0.10     0.004728132 21.15 21.30 20.90
#> 2017-10-09 2017-10-09            0.00     0.000000000 21.35 21.35 20.95
#> 2017-10-06 2017-10-06            0.25     0.012106538 20.95 21.15 20.85
#> 2017-10-05 2017-10-05            0.00     0.000000000 21.00 21.10 20.65
#> 2017-10-04 2017-10-04            0.15     0.007317073 20.50 20.80 20.40
#> 2017-10-03 2017-10-03           -0.55     0.026128266 21.25 21.25 19.70
#> 2017-10-02 2017-10-02           -0.30     0.014051522 21.35 21.60 21.00
#> 2017-09-29 2017-09-29           -0.05     0.002336449 21.60 21.60 21.20
#> 2017-09-28 2017-09-28           -0.20     0.009259259 21.70 21.75 21.30
#> 2017-09-27 2017-09-27            0.10     0.004651163 21.60 21.85 21.45
#> 2017-09-26 2017-09-26            0.00     0.000000000 21.40 21.70 21.35
#> 2017-09-25 2017-09-25           -0.40     0.018264840 21.80 21.90 21.50
#> 2017-09-22 2017-09-22           -0.05     0.002277904 22.00 22.00 21.80
#> 2017-09-21 2017-09-21            0.20     0.009195402 21.75 22.30 21.70
#> 2017-09-20 2017-09-20           -0.25     0.011363636 21.90 22.00 21.65
#> 2017-09-19 2017-09-19           -0.15     0.006772009 22.00 22.20 21.80
#> 2017-09-18 2017-09-18            0.05     0.002262443 22.30 22.40 21.80
#> 2017-09-15 2017-09-15           -0.30     0.013392857 22.45 22.45 21.95
#> 2017-09-14 2017-09-14            0.50     0.022831050 21.85 22.40 21.85
#> 2017-09-13 2017-09-13            0.90     0.042857143 21.00 21.95 21.00
#> 2017-09-12 2017-09-12            0.10     0.004784689 20.80 21.00 20.80
#> 2017-09-11 2017-09-11            0.00     0.000000000 21.10 21.10 20.80
#>            CLOSE AVERAGE CLOSE.ADJUST MATCH.VOLUME RECONCILE.VOLUME
#> 2017-12-29 26.40   26.50        26.40      1787920               NA
#> 2017-12-28 26.10   25.68        26.10      3208580               NA
#> 2017-12-27 24.40   24.55        24.40      1834450               NA
#> 2017-12-26 24.00   23.98        24.00       696510               NA
#> 2017-12-25 24.10   24.02        24.10      1082430               NA
#> 2017-12-22 23.65   23.54        23.65       825360               NA
#> 2017-12-21 23.55   23.66        23.55       677420               NA
#> 2017-12-20 23.70   23.73        23.70      1646090               NA
#> 2017-12-19 23.60   23.35        23.60      2031720               NA
#> 2017-12-18 22.65   22.58        22.65      1759750               NA
#> 2017-12-15 22.00   21.89        22.00       836400               NA
#> 2017-12-14 21.95   21.78        21.95       874390           219000
#> 2017-12-13 21.35   21.28        21.35       715040               NA
#> 2017-12-12 21.40   21.14        21.40      1585620               NA
#> 2017-12-11 21.60   22.06        21.60      1207890               NA
#> 2017-12-08 22.75   22.74        22.75       719860               NA
#> 2017-12-07 22.80   22.91        22.80       823270               NA
#> 2017-12-06 23.40   23.13        23.40      2025180               NA
#> 2017-12-05 23.50   23.98        23.50      1818830               NA
#> 2017-12-04 24.50   24.46        24.50      1236940               NA
#> 2017-12-01 23.80   23.74        23.80       816650               NA
#> 2017-11-30 23.50   23.32        23.50      1518630               NA
#> 2017-11-29 22.75   22.91        22.75       870830               NA
#> 2017-11-28 22.80   22.67        22.80       607610               NA
#> 2017-11-27 22.85   22.94        22.85      1408330            20000
#> 2017-11-24 22.20   21.90        22.20       696370               NA
#> 2017-11-23 22.00   22.29        22.00      1564980               NA
#> 2017-11-22 22.35   22.30        22.35       916450               NA
#> 2017-11-21 22.25   21.99        22.25      1696230               NA
#> 2017-11-20 21.60   21.64        21.60       702840               NA
#> 2017-11-17 21.55   21.66        21.55      1302190               NA
#> 2017-11-16 21.25   21.09        21.25       696600               NA
#> 2017-11-15 20.70   20.69        20.70       536450          5298010
#> 2017-11-14 20.60   20.61        20.60       581450           430000
#> 2017-11-13 20.65   20.07        20.65       660050           108310
#> 2017-11-10 19.60   19.70        19.60       292150           900000
#> 2017-11-09 19.75   19.58        19.75       423400               NA
#> 2017-11-08 19.40   19.26        19.40       293860               NA
#> 2017-11-07 19.30   19.36        19.30       185400               NA
#> 2017-11-06 19.40   19.14        19.40       302320               NA
#> 2017-11-03 19.00   18.88        19.00       282390               NA
#> 2017-11-02 18.90   19.07        18.90       365320               NA
#> 2017-11-01 19.00   18.86        19.00       633640               NA
#> 2017-10-31 19.50   19.79        19.50       387580               NA
#> 2017-10-30 20.20   20.39        20.20       374290               NA
#> 2017-10-27 20.25   20.24        20.25       286070               NA
#> 2017-10-26 20.30   20.32        20.30       322940               NA
#> 2017-10-25 20.40   20.43        20.40       380560               NA
#> 2017-10-24 20.20   20.13        20.20       332500               NA
#> 2017-10-23 20.00   20.23        20.00       455840               NA
#> 2017-10-20 20.55   20.65        20.55       260610               NA
#> 2017-10-19 20.80   20.87        20.80       176540               NA
#> 2017-10-18 20.95   21.07        20.95       749340               NA
#> 2017-10-17 20.80   20.67        20.80       220360               NA
#> 2017-10-16 20.40   20.49        20.40       513240               NA
#> 2017-10-13 20.80   20.83        20.80       205110               NA
#> 2017-10-12 20.95   21.04        20.95       359990               NA
#> 2017-10-11 20.95   21.03        20.95       413890               NA
#> 2017-10-10 21.05   21.03        21.05       371460               NA
#> 2017-10-09 21.15   21.15        21.15       423120               NA
#> 2017-10-06 20.90   20.97        20.90       433610               NA
#> 2017-10-05 20.65   20.85        20.65       306850               NA
#> 2017-10-04 20.65   20.59        20.65       214340               NA
#> 2017-10-03 20.50   20.73        20.50       574090               NA
#> 2017-10-02 21.05   21.34        21.05       347690               NA
#> 2017-09-29 21.35   21.36        21.35       414550               NA
#> 2017-09-28 21.40   21.58        21.40       450830               NA
#> 2017-09-27 21.60   21.59        21.60       328060               NA
#> 2017-09-26 21.50   21.49        21.50       417760               NA
#> 2017-09-25 21.50   21.63        21.50       510810               NA
#> 2017-09-22 21.90   21.93        21.90       457490               NA
#> 2017-09-21 21.95   21.97        21.95       664220               NA
#> 2017-09-20 21.75   21.80        21.75       375240               NA
#> 2017-09-19 22.00   22.03        22.00       836960               NA
#> 2017-09-18 22.15   22.10        22.15      1285110               NA
#> 2017-09-15 22.10   22.18        22.10      1081030               NA
#> 2017-09-14 22.40   22.25        22.40       647850          1800000
#> 2017-09-13 21.90   21.50        21.90       884400          3000000
#> 2017-09-12 21.00   20.91        21.00       660860               NA
#> 2017-09-11 20.90   20.94        20.90      1069340               NA
#> 
#> $call
#> getSymbols(symbol = "VND", from = "2017-09-10", to = "2017-12-29")
#sau khi chay lenh tren mot data frame ten VND se duoc tao ra
head(VND)
#>                  DATE CHANGE.PERCENT1 CHANGE.PERCENT2  OPEN  HIGH   LOW
#> 2017-12-29 2017-12-29            0.30     0.011494253 26.30 26.90 26.10
#> 2017-12-28 2017-12-28            1.70     0.069672131 24.70 26.10 24.40
#> 2017-12-27 2017-12-27            0.40     0.016666667 24.30 24.80 24.20
#> 2017-12-26 2017-12-26           -0.10     0.004149378 24.30 24.30 23.80
#> 2017-12-25 2017-12-25            0.45     0.019027484 23.65 24.30 23.60
#> 2017-12-22 2017-12-22            0.10     0.004246285 23.35 23.75 23.25
#>            CLOSE AVERAGE CLOSE.ADJUST MATCH.VOLUME RECONCILE.VOLUME
#> 2017-12-29 26.40   26.50        26.40      1787920               NA
#> 2017-12-28 26.10   25.68        26.10      3208580               NA
#> 2017-12-27 24.40   24.55        24.40      1834450               NA
#> 2017-12-26 24.00   23.98        24.00       696510               NA
#> 2017-12-25 24.10   24.02        24.10      1082430               NA
#> 2017-12-22 23.65   23.54        23.65       825360               NA