2

1st file given in the URL named "EQUITY_L.csv" source given in the footnote below

I am trying to access the URL "https://in.finance.yahoo.com/quote/3MINDIA.NS/history?p=3MINDIA.NS" using the symbol variable given in the file in the footnote below from the NSE website. I am able to fetch data from the multiple files by the code but only first 100 rows are fetched in the data table or each URL of total 250. I am unable to resolve the problem. I also had a look at the tables available in each URL and there is only a single table. How to fetch the entire 250 rows in each URL? I had a look at the quantmod function but it has US specific indices. Could you help with this..

read.csv(file="C:/Users/Documents/EQUITY_L.csv", header=TRUE, sep=",")

MyData$SYMBOL
Symbol<-MyData$SYMBOL
tbls_ls<-list()
for(i in 1:12) {
webpage<-paste0("https://in.finance.yahoo.com/quote/",Symbol[i],".NS","/history?p=",Symbol[i],".NS")
Webpage<-read_html(webpage)
tbls_ls[i] <- Webpage %>%
        html_nodes("table") %>%
        .[1] %>%
        html_table(fill = TRUE)}
Catool
  • 25
  • 7
  • Most likely there is a rate limit associated with the yahoo finance API and you need the instructions. Your code can't work as written, for example where does MyData come from? From what your wrote I believe you would need to assign the results of read.csv to MyData. – Elin Sep 08 '18 at 14:39
  • Possible duplicate of [Get data from Yahoo! Finance to R](https://stackoverflow.com/questions/48568159/get-data-from-yahoo-finance-to-r) – Elin Sep 08 '18 at 14:58
  • Hi, My data comes from the "EQUITY_L.csv" file from the ticker column in that sheet. Basically I am trying to extract the past prices from tickers of those stocks of NSE index , which is the data that forms the variable Symbol. – Catool Sep 08 '18 at 15:35

1 Answers1

1

Hmmm...it seems like you have a few issues here and recent edits to the post seems to have removed some of them. The issue with no row names will occur if you are using packages which coerce your data.frame to a tibble, which doesn't have row names.

I think if you just use quantmod it will take care of many of your problems here. It will return an xts object where row names are stored as vector of dates. The default settings return 2942 observations, or daily returns from 2007-01-02 if the security in question has existed that long and contains no missing values.

Modifying your code above:

MyData <- read.csv(file="C:/Users/Documents/EQUITY_L.csv", header=TRUE, sep=",")
Symbol<-MyData$SYMBOL

And then adding this

library(quantmod)
getSymbols(Symbol)

That's it!

Justin
  • 1,360
  • 12
  • 15
  • 1
    Quantmod served the purpose. Thanks getSymbols('Symbol',src='yahoo') did the job – Catool Sep 09 '18 at 10:18
  • How is using Quantmod as a solution not just repeating the answers from the duplicate question? – Elin Sep 09 '18 at 16:04
  • Excellent @Catool, yes the default argument for `getSymbols` is `src=yahoo`, though other data sources are available too. Please don't forget to accept the solution and close it if you are satisfied with the results, so that your answer does not remain in the `unanswered` r que. – Justin Sep 09 '18 at 18:35
  • @Elin Your duplicate answer comment was not there when I started writing my solution, which took some time, mainly because I did do the work to recreate his results upon which I discovered the tibble issue. – Justin Sep 09 '18 at 18:44
  • The tibble issue is definitely value added. – Elin Sep 09 '18 at 18:45
  • Thanks, yeah its understandable how this sometimes confuse people because row names are so fundamental to r data.frame and matrix objects. – Justin Sep 09 '18 at 19:00
  • The question is still unanswered as I am finding it hard to loop the function in for look and it gets timed out. I had to download all the xts files for 1632 stocks. – Catool Sep 10 '18 at 14:39
  • I used function(x){getSymbols(x,src='yahoo')} and then used it in function(x){getSymbols(x,src='yahoo')} and then used it in for loop for(i in 1:1632) {function(Symbol[i])} , however, when i pass the variable in the loop . It gets timed out @Justin – Catool Sep 10 '18 at 14:51
  • More specifically, your now using the function wrapper and for loop over what appears to be 1632 symbols. Why are you doing that? What is your goal? As we are answering your questions voluntarily, try to make it as easy as possible for us to help you. this document here is very helpful in this regard: https://stackoverflow.com/help/how-to-ask .Thank you. – Justin Sep 10 '18 at 18:40
  • 1
    @Justin I wanted to do some financial analysis for these stocks. I have started a new thread. Thanks – Catool Sep 11 '18 at 02:57
  • Can you provide the link? I'll check it out. – Justin Sep 11 '18 at 03:11
  • @Justin https://stackoverflow.com/questions/52268028/download-failed-while-using-quantmod-function-getsymbols-in-for-loop – Catool Sep 11 '18 at 14:07