2017-10-10 61 views
1

我想創建多個圖表,並在循環中使用quantmod的chart_Series插入垂直線。當我運行代碼時,它不會生成圖表或錯誤消息。我不確定我做錯了什麼。以下是我正在使用的代碼和示例數據。任何援助將不勝感激。謝謝。chart_Series在一個循環中的xts對象quantmod

library(quantmod) 
xt <- xts(rep(FALSE, nrow(B825)), index(B825)) 
Buy.Date <- c('2017-08-25') 
xt[Buy.Date,] <- TRUE 

for(i in 1: ncol(B825)) { 
    chart_Series(B825[,i]) 
    add_TA(xt, on =-1, col = "white", border = "white") 
} 

      STNG ASC 
2017-05-18 4.0188 7.70 
2017-05-19 4.1185 7.70 
2017-05-22 4.1484 7.80 
2017-05-23 4.1883 7.70 
2017-05-24 3.9989 7.50 
2017-05-25 3.9590 7.45 
2017-05-26 3.7795 7.45 
2017-05-30 3.6997 7.35 
2017-05-31 3.7097 7.20 
2017-06-01 3.7695 7.20 
2017-06-02 3.6299 7.20 
2017-06-05 3.6000 7.20 
2017-06-06 3.6000 7.25 
2017-06-07 3.5900 7.15 
2017-06-08 3.6698 7.55 
2017-06-09 3.8094 7.70 
2017-06-12 3.7894 7.85 
2017-06-13 3.8493 7.95 
2017-06-14 3.7894 7.75 
2017-06-15 3.6399 7.60 
2017-06-16 3.7595 7.85 
2017-06-19 3.7496 7.90 
2017-06-20 3.6000 7.85 
2017-06-21 3.6099 7.65 
2017-06-22 3.7595 7.85 
2017-06-23 3.9889 8.15 
2017-06-26 4.0088 8.40 
2017-06-27 4.0587 8.50 
2017-06-28 4.0487 8.35 
2017-06-29 4.0786 8.20 
2017-06-30 3.9590 8.15 
2017-07-03 4.0388 8.45 
2017-07-05 3.9490 8.10 
2017-07-06 3.7595 7.85 
2017-07-07 3.7695 7.85 
2017-07-10 3.7695 7.65 
2017-07-11 3.7894 7.85 
2017-07-12 3.7496 7.95 
2017-07-13 3.7994 8.00 
2017-07-14 3.8892 8.00 
2017-07-17 3.8493 8.00 
2017-07-18 3.7296 7.85 
2017-07-19 3.7296 8.00 
2017-07-20 3.7496 7.90 
2017-07-21 3.7196 7.80 
2017-07-24 3.6797 7.85 
2017-07-25 3.6698 7.95 
2017-07-26 3.6897 7.95 
2017-07-27 3.7496 7.95 
2017-07-28 3.7097 7.95 
2017-07-31 3.6997 7.95 
2017-08-01 3.6598 7.95 
2017-08-02 3.6199 7.95 
2017-08-03 3.5501 7.85 
2017-08-04 3.5302 7.70 
2017-08-07 3.5401 7.55 
2017-08-08 3.5202 7.40 
2017-08-09 3.5401 7.25 
2017-08-10 3.4105 7.20 
2017-08-11 3.3507 7.25 
2017-08-14 3.4005 7.45 
2017-08-15 3.3806 7.30 
2017-08-16 3.2111 7.45 
2017-08-17 3.3806 7.20 
2017-08-18 3.5002 7.30 
2017-08-21 3.3606 7.20 
2017-08-22 3.4005 7.30 
2017-08-23 3.4404 7.35 
2017-08-24 3.6997 7.65 
2017-08-25 3.9689 7.95 
2017-08-28 3.8991 8.05 
2017-08-29 3.9789 8.05 
2017-08-30 3.9889 8.00 
2017-08-31 4.0587 8.10 
2017-09-01 4.0088 8.10 
2017-09-05 3.7097 7.75 
2017-09-06 3.5002 7.65 
2017-09-07 3.5202 7.70 
2017-09-08 3.5202 7.55 
2017-09-11 3.4404 7.55 
2017-09-12 3.4404 7.45 
2017-09-13 3.4005 7.40 
2017-09-14 3.5701 7.35 
2017-09-15 3.4903 7.35 
2017-09-18 3.3806 7.55 
2017-09-19 3.4604 7.95 
2017-09-20 3.5701 8.00 
2017-09-21 3.4304 7.70 
2017-09-22 3.5900 8.00 
2017-09-25 3.5300 8.05 
2017-09-26 3.4100 8.00 
2017-09-27 3.5300 8.20 
2017-09-28 3.4900 8.20 
2017-09-29 3.4300 8.25 
2017-10-02 3.5900 8.50 
2017-10-03 3.5800 8.85 
2017-10-04 3.6200 8.75 
2017-10-05 3.7000 8.95 
2017-10-06 3.6400 8.90 
2017-10-09 3.5400 8.80 

UPDATE

str(B825) 

An ‘xts’ object on 2017-05-19/2017-10-10 containing: 
    Data: num [1:100, 1:2] 4.12 4.15 4.19 4 3.96 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : NULL 
    ..$ : chr [1:2] "STNG" "ASC" 
    Indexed by objects of class: [Date] TZ: UTC 
    xts Attributes: 
NULL 

回答

0
for(i in 1: ncol(B825)) { 
    windows() 
    print(chart_Series(B825[,i], name=names(B825)[i])) 
    print(add_TA(xt, on =-1, col = "red", border = "white")) 
} 

enter image description here

+0

謝謝您的答覆。但是,我想循環訪問數據以生成兩個單獨的圖表,而不是單個圖表。 – user3734010