3
getSymbols("2222.SR")
[1] "2222.SR"
OPEN1<-Op(2222.SR)
Error: unexpected symbol in "OPEN1<-Op(2222.SR"

I was expecting R to give me the opening price but for some reason it's giving me this error although it was able to fetch that data from yahoo. BTW I'm using quantmod.

I'm a beginner at this so I have no idea what's wrong :(. This is a stock in the Saudi stock market from yahoo finance. I tried the same thing with another Saudi stock and still it didn't work I also tried other functions like for the closing price and got the same results:

getSymbols("7010.SR")
[1] "7010.SR"

Warning message:

7010.SR contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.

OPEN2<-Op(7010.SR)
Error: unexpected symbol in "OPEN2<-Op(7010.SR"

I also tried it with an American traded stock and it worked.

phiver
  • 23,048
  • 14
  • 44
  • 56
BubaZeed
  • 31
  • 2

1 Answers1

1

The problem you have is that if you use getSymbols("2222.SR") it creates an object called 2222.SR in your environment. But because is starts with numbers in the object name, you can't just call it with Op("2222.SR"). That will give you this error. You need to use backticks to access this object or tell getSymbols not to auto assign an object name.

Code with backticks:

library(quantmod)

# with the use of backticks
getSymbols("2222.SR")

OPEN1 <- Op(`2222.SR`)
head(OPEN1)
           2222.SR.Open
2019-12-11     29.09091
2019-12-12     31.98347
2019-12-15     30.66116
2019-12-16     30.99173
2019-12-17     31.44628
2019-12-18     30.99173

Code without backticks:

aramco <- getSymbols("2222.SR", auto.assign = FALSE)

OPEN1 <- Op(aramco)

head(OPEN1)
           2222.SR.Open
2019-12-11     29.09091
2019-12-12     31.98347
2019-12-15     30.66116
2019-12-16     30.99173
2019-12-17     31.44628
2019-12-18     30.99173
phiver
  • 23,048
  • 14
  • 44
  • 56