I have the following code in R:
library(quantmod)
mySymbol = "^STOXX50E"
getSymbols(mySymbol, from="2004-01-01", to=Sys.Date())
chartSeries(Cl(STOXX50E))
which simply download the time series for the inder Eurostoxx and then plots the closing price. It works as expected. Anyway, I was wondering how I can avoid to write explicitely "STOXX50E" everytime I want to reference to this variable. For example, I would like to be able to reference the variable that contains the data with a generic name like "INDEX" so that I don't need to change all the calls when I want to launch the code with another inder.
For example, if i want to download and plot the closing price for the S&P500 I have to do:
library(quantmod)
mySymbol = "^GSPC"
getSymbols(mySymbol, from="2004-01-01", to=Sys.Date())
chartSeries(Cl(GSPC))
so I have to change the variable name not only on the second line but also on the last. I would rather something more generic like:
library(quantmod)
mySymbol = "^GSPC"
getSymbols(mySymbol, from="2004-01-01", to=Sys.Date())
chartSeries(Cl(mySymbol))
So that once I have set the name for mySymbol I don't have to change all the rest of the code. But this doesn't work. How can I accomplish this?