'This program fetches historical stock data from the yahoo finance website. It uses the @UIDIALOG functions to provide an interactive way of providing information on which stocks to retreive, as well as some other options. logmode +addin '--------------------------------------------------------Defaults--------------------------------------------------------------------------- !freq=3 'Frequency choice. 1 = daily, 2 = weekly, 3 = monthly. %start = "2005/01/01" 'Start date of data to fetch. Set at an arbitrarily early date. Note that when the actual data are fetched, the workfile start date will be the minimum of the earliest date for which data are available, or the value of %start %end = @datestr(@now,"yyyy/MM/DD") 'end date of data to fetch. Set at today's date, using the @now function. !doopen=1 'bool for whether to fetch the open price !doclose=1 'bool for whether to fetch the close price !doadjcl=0 'bool for whether to fetch the adjusted closing price. !dohigh=1 'bool for whether to fetch the high price !dolow=1 'bool for whether to fetch the low price !dovol=1 'bool for whether to fetch the volume !doGUI = 1 'bool for whether to show the dialog or not '------------------------------------------------------Command Line-------------------------------------------------------------------------- if @len(%args) then %stocks = %args !dogui = 0 endif if @len(@option(1)) then !doopen = @hasoption("o") !doclose = @hasoption("c") !doadjcl = @hasoption("a") !dohigh = @hasoption("h") !dolow = @hasoption("l") !dovol = @hasoption("v") %start = @equaloption("start") %end = @equaloption("end") !freq = @val(@equaloption("freq")) endif '------------------------------------------------------Put up Dialog------------------------------------------------------------------------------ 'Note that I am using the line continuation character " _ " to break up this single command into multiple lines. This makes it much easier to read. if !dogui = 1 then !result = @uidialog( _ "edit",%stocks,"Enter a list of stock symbols","text","Choose which data to fetch:", _ "check",!doopen,"Open","check",!doclose,"Close","check",!doadjcl,"Adj. Close","check",!dohigh,"High","check",!dolow,"Low","check",!dovol,"Volume", _ "radio",!freq,"Enter a frequency type","Daily Weekly Monthly", _ "text","Enter a start and end date. Note that the dates may be truncated due to data availability", _ "edit",%start,"Start date (yyyy/mm/dd)","edit",%end,"End date (yyyy/mm/dd)" _ ) 'check if the user hit cancel on the dialog, if they did, stop the program. if !result=-1 then stop endif endif 'check that the user entered at least one stock symbol. if @length(%stocks)=0 then @uiprompt("You must enter at least one stock name") stop endif 'check that the user entered at least one statistic to fetch. if !doopen + !doclose + !doadjcl + !dolow + !dohigh + !dovol = 0 then @uiprompt("You must select at least one data type") stop endif '-------------------------------------------------------------Option parsing---------------------------------------------------------------------- 'convert the frequency radio choice into a string containing the yahoo code (i.e. d=daily, w=weekly, m=monthly) %freqs = "d w m" %freq = @word(%freqs,!freq) 'convert the start date into three parts, one for the year, one for the month, one for the day. %sy = @left(%start,4) %sm = @mid(%start,6,2) %sd = @mid(%start,9,2) %sm = @str(@val(%sm)-1) 'Note that yahoo uses a zero base for months, so January = 00, February = 1 etc....., so we have to take one off the month the user entered. 'convert the end date into three parts, one for the year, one for the month, one for the day. %ey = @left(%end,4) %em = @mid(%end,6,2) %ed = @mid(%end,9,2) %em = @str(@val(%em)-1) 'Note that yahoo uses a zero base for months, so January = 0, February = 1 etc....., so we have to take one off the month the user entered. 'build up a string containing a list of all of the variables we want to fetch from yahoo. Both the wfopen and import commands (used below) let you specify a list of variables to open/fetch by using the @keep keyword. %keepstr = "@keep date" 'start off the string by using the @keep keyword, and saying that we'll fetch the date string if !doopen=1 then %keepstr = %keepstr + " open" 'if they've chosen to fetch open prices, add the open variable to the keep string endif if !doclose=1 then %keepstr = %keepstr + " close" 'if they've chosen to fetch close prices, add the close variable to the keep string endif if !doadjcl=1 then %keepstr = %keepstr + " ""adj close""" 'if they've chosen to fetch close prices, add the close variable to the keep string endif if !dolow=1 then %keepstr = %keepstr + " low" 'if they've chosen to fetch low prices, add the low variable to the keep string endif if !dohigh=1 then %keepstr = %keepstr + " high" 'if they've chosen to fetch high prices, add the high variable to the keep string endif if !dovol=1 then %keepstr = %keepstr + " volume" 'if they've chosen to fetch volume, add the volume variable to the keep string endif '----------------------------------------------------------------------------Fetch the data------------------------------------------------------------------------------ setmaxerrs 100 'first of all fetch the first stock from the stock list by using wfopen to open the data from the yahoo url %stock = @word(%stocks,1) %url = "http://ichart.finance.yahoo.com/table.csv?s=" + %stock + "&d=" + %em + "&e=" + %ed + "&f=" + %ey + "&g=" + %freq + "&a=" + %sm + "&b=" + %sd + "&c=" + %sy + "&ignore=.csv" if @left(%stock,1) = "^" then 'handle the case where a stock name starts with "^", which is an invalid EViews name. %stock = "_" + @mid(%stock,2) endif wfopen(wf=stocks) {%url} {%keepstr} @rename * {%stock}_* 'note the use of @rename to append the name of the stock to the variable names if @left(@lasterrstr,6) = " Web s" then %msg = "Yahoo returned an error - perhaps " + %stock + " is an invalid stock symbol." @uiprompt(%msg) stop endif 'If more than one stock was entered, loop through them one at a time, importing them into the workfile that was created above. if @wcount(%stocks) > 1 then for !i=2 to @wcount(%stocks) %stock = @word(%stocks,!i) %url = "http://ichart.finance.yahoo.com/table.csv?s=" + %stock + "&d=" + %em + "&e=" + %ed + "&f=" + %ey + "&g=" + %freq + "&a=" + %sm + "&b=" + %sd + "&c=" + %sy + "&ignore=.csv" if @left(%stock,1) = "^" then %stock = "_" + @mid(%stock,2) endif import(resize) {%url} {%keepstr} @rename * {%stock}_* 'again, note the use of @rename to append the name of the stocks to the variable names if @left(@lasterrstr,6) = " Web s" then %msg = "Yahoo returned an error - perhaps " + %stock + " is an invalid stock symbol." @uiprompt(%msg) stop endif next endif 'rename the adjusted closes to get rid of the _ rename *_adj_close *_adjclose '-------------------------------------------------------------------Create graphs of the data-------------------------------------------------------------- %graphs = "" 'This is a string that will contain the name of the graphs that are created if (!doopen) then group open *_open 'create a group with all the open variables freeze(openg) open.line 'make a graph from the group %graphs = %graphs + " openg" 'append the name of the created graph to the list of graph names endif if (!doadjcl) then group adjclose *_adjclose freeze(adjcloseg) adjclose.line %graphs = %graphs + " adjcloseg" endif if (!doclose) then group close *_close freeze(closeg) close.line %graphs = %graphs + " closeg" endif if (!dolow) then group low *_low freeze(lowg) low.line %graphs = %graphs + " lowg" endif if (!dohigh) then group high *_high freeze(highg) high.line %graphs = %graphs + " highg" endif if (!dovol) then group volume *_volume freeze(volg) volume.line %graphs = %graphs + " volg" endif 'if they created more than one graph, create a master graph containing all the smaller graphs. if !doclose + !doadjcl + !doopen + !dolow + !dohigh >1 then graph stocks.merge {%graphs} ' show stocks endif