joshuaulrich / quantmod

Quantitative Financial Modelling Framework
http://www.quantmod.com/
GNU General Public License v3.0
798 stars 219 forks source link

Issue to getSymbols() of Index #353

Closed creatiezo closed 2 years ago

creatiezo commented 2 years ago

If you try to use getSymbols() to Indices, like "^DJI", "^HSI", etc. the return variables are "DJI", "HSI", etc. getSymbols() removes "^" from the inputs.

Is there any way to return the variables as what I input, like "^DJI", "^HSI", etc.

joshuaulrich commented 2 years ago

getSymbols() removes the ^ because variable names in R have to start with a letter or a .. Variable names that do not begin with a letter or a . must be backquoted every time you want to use them. So you probably don't want to do that...

But if you do, here's one way:

R$ `^DJI` <- getSymbols("^DJI", auto.assign = FALSE)                            
R$ head(`^DJI`)                                                                 
           DJI.Open DJI.High  DJI.Low DJI.Close DJI.Volume DJI.Adjusted
2007-01-03 12459.54 12580.35 12404.82  12474.52  327200000     12474.52
2007-01-04 12473.16 12510.41 12403.86  12480.69  259060000     12480.69
2007-01-05 12480.05 12480.13 12365.41  12398.01  235220000     12398.01
2007-01-08 12392.01 12445.92 12337.37  12423.49  223500000     12423.49
2007-01-09 12424.77 12466.43 12369.17  12416.60  225190000     12416.60
2007-01-10 12417.00 12451.61 12355.63  12442.16  226570000     12442.16

I'm going to close this because there's nothing to fix. But feel free to continue the discussion and I'll try to help if I can.

creatiezo commented 2 years ago

@joshuaulrich thx for you help.

When I tried to find workaround yesterday, I found something interesting. I tried to getSymbols() with "^HSI", "HSI", "^DJI" and "DJI". And I thought if I try to getSymbols() with "HSI" and "DJI", it should return "not found", because I can't find "HSI" and "DJI" from Yahoo Finance website.

The problem only happens to the indices with 3 letters, like ^HSI, ^DJI, ^RUT, etc. I can't repeat it if I try to do the same thing with the indices with 4 letters, like ^GSPC, ^IXIC, etc.

I think the problem should not be caused by my proxy. Since Yahoo has block the connection from China, I can't try without proxy. I hope you could find something from your side and thx in advanced.

list.proxy <- list( proxy = "127.0.0.1", proxyport = 20809 ) getSymbols("^HSI", curl.options = list.proxy ) [1] "^HSI" head(HSI) HSI.Open HSI.High HSI.Low HSI.Close HSI.Volume HSI.Adjusted 2007-01-02 20004.84 20323.59 19990.28 20310.18 1264596800 20310.18 2007-01-03 20353.42 20554.58 20249.61 20413.39 1673968900 20413.39 2007-01-04 20415.67 20463.18 19948.02 20025.58 2130510200 20025.58 2007-01-05 19890.15 20213.64 19757.24 20211.28 1959114400 20211.28 2007-01-08 19915.00 20085.58 19844.31 20029.66 1438589300 20029.66 2007-01-09 20162.54 20173.06 19794.29 19898.08 1524412800 19898.08 getSymbols("HSI", curl.options = list.proxy ) [1] "HSI" head(HSI) HSI.Open HSI.High HSI.Low HSI.Close HSI.Volume HSI.Adjusted 2007-01-03 20413.40 20413.40 20413.40 20413.40 0 20413.40 2007-01-04 3.48 3.54 3.40 3.50 23961 3.50 2007-01-05 20211.30 20211.30 20211.30 20211.30 0 20211.30 2007-01-08 20029.70 20029.70 20029.70 20029.70 0 20029.70 2007-01-09 3.44 3.52 3.38 3.44 25678 3.44 2007-01-10 19568.30 19568.30 19568.30 19568.30 0 19568.30 getSymbols("^DJI", curl.options = list.proxy ) [1] "^DJI" head(DJI) DJI.Open DJI.High DJI.Low DJI.Close DJI.Volume DJI.Adjusted 2007-01-03 12459.54 12580.35 12404.82 12474.52 327200000 12474.52 2007-01-04 12473.16 12510.41 12403.86 12480.69 259060000 12480.69 2007-01-05 12480.05 12480.13 12365.41 12398.01 235220000 12398.01 2007-01-08 12392.01 12445.92 12337.37 12423.49 223500000 12423.49 2007-01-09 12424.77 12466.43 12369.17 12416.60 225190000 12416.60 2007-01-10 12417.00 12451.61 12355.63 12442.16 226570000 12442.16 getSymbols("DJI", curl.options = list.proxy ) [1] "DJI" head(DJI) DJI.Open DJI.High DJI.Low DJI.Close DJI.Volume DJI.Adjusted 2007-01-03 12474.5 12474.5 12474.5 12474.5 0 12474.5 2007-01-04 12480.7 12480.7 12480.7 12480.7 0 12480.7 2007-01-05 12398.0 12398.0 12398.0 12398.0 0 12398.0 2007-01-08 12423.5 12423.5 12423.5 12423.5 0 12423.5 2007-01-09 12416.6 12416.6 12416.6 12416.6 0 12416.6 2007-01-10 12442.2 12442.2 12442.2 12442.2 0 12442.2

joshuaulrich commented 2 years ago

I just tried this without the proxy, and I get similar results. Notice that the values are different for "^HSI" and "HSI", etc. I'm not sure why this happens, and I don't think it's worth the time to investigate this behavior.

creatiezo commented 2 years ago

Thx for telling there's no difference without the proxy, then I'm relieved.