Download historical stock price data with coding a webscrapper
I'm downloading historical timeseries of stocks from Yahoo. The link of *excel file download has a systematic pattern which consists of time window and stock's code7 name. I generate the pattern and force the MATLAB to visit the webpage. Similar strategy can be followed in other programming languages also.
Have a excel file with all the interested/listed companies with their nse code. If you don't find it online, I have 1000 big company list. Message me from contact form. Example, Reliance Industries Ltd., Code name: RELIANCE. In excel make it RELIANCE.NS using formula.
So, code name column is ready RELIANCE.NS
for i=1:length(data)
ticker=data{i,1}; % 'RELIANCE.NS'
d1='01-Mar-2008';
YahooDownload(ticker,d1);
pause(1)
end
%% -----------------
01/01/2020 00:00 and 01/09/20201 22:21 both this time and date has to be converted into UNIX time. Other part of the link repeats.
%% ----------------- main function-------------
function YahooDownload(ticker,d1)
freq = 'd';
d2 = datetime(clock);
ticker = char(ticker);
% Shift dates to market closing time.
% d1 = dateshift(d1, 'start', 'day');
d2 = dateshift(d2, 'start', 'hour');
%% Retrieve data from yahoo finance
% Yahoo finance uses a unix serial date number, so will have to convert to
% that. That's a UNIX timestamp -- the number of seconds since January 1, 1970.
unix_epoch = datenum(1970,1,1,0,0,0);
d1u=floor(num2str((datenum(d1)-unix_epoch)*86400));
d2u=floor(num2str((datenum(d2)- unix_epoch)*86400));
site=strcat('https://query1.finance.yahoo.com/v7/finance/download/',ticker,'?','period1=',d1u,'&period2=',d2u,'&interval=1',freq,'&events=history');
filename=sprintf('%s.csv',ticker);
web(filename,site);
end
%-----------------------
----------------------------------------
Disclaimer: Stocks mentioned (if any) here are not recommendation. We may have already invested in some of the name mentioned here. Read full disclaimer here
----------------------------------------
Comments
Post a Comment