'Trying to extract the date of 52 weeks high and low for stocks

Both maxMATX and maxZIM return no observation, which I am very confused about.

Here is the code

library(tseries)
\#teries have all the Financial Data , hence we need to load it

data.ZIM\<- get.hist.quote("ZIM")
data.MATX\<- get.hist.quote("MATX")

data.ZIM\<-data.ZIM\[Sys.Date()-0:364\]
data.MATX\<-data.MATX\[Sys.Date()-0:364\]

head(data.ZIM)
head(data.MATX)

min(data.ZIM$Close)
max(data.ZIM$Close)

minZIM=data.ZIM\[data.ZIM$Close==24.34\]
maxZIM=data.ZIM\[data.ZIM$Close==88.62\]

data.ZIM\[data.ZIM$Close==88.62\]

minZIM
maxZIM

min(data.MATX$Close)
max(data.MATX$Close)

minMATX=data.MATX\[data.MATX$Close==60.07,\]
maxMATX=data.MATX\[data.MATX$Close==121.47,\]

minMATX
maxMATX

I was trying to extract the data from Tseries and I have faced difficulty when trying to print the row (or specifically I was trying to find the date of which the 52 weeks low and high was happening ).

r


Solution 1:[1]

Use which.min and which.max to find indexes of minimum and maximum close and use those to look up the time.

library(tseries)

data.ZIM <- get.hist.quote("ZIM", start = Sys.Date() - 364)

tmin <- time(data.ZIM)[which.min(data.ZIM$Close)]; tmin
## [1] "2021-03-31"

data.ZIM[tmin]
##             Open  High   Low Close
## 2021-03-31 24.75 24.99 24.15 24.34

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1