EMT – Reading Stock Data from Yahoo

The new HTML connection can be used to read stock data directly from the page at Yahoo. The following function will do this.

>function getstock(stock,date1,date2) ...
$s="http://ichart.finance.yahoo.com/table.csv?s="+stock;
${y,m,d}=date(date1); s=s+''&a=''+(m-1)+''&b=''+d+''&c=''+y;
${y,m,d}=date(date2); s=s+''&d=''+(m-1)+''&e=''+d+''&f=''+y;
$s=s+''&g=d&ignore.csv'';
$v=[];
$urlopen(s);
$heading=strtokens(urlgetline(),",");
$repeat
$   line=strtokens(urlgetline(),",");
$   if length(line)<2 then break; endif;
$   date=day(line[1]);
$   for k=2:cols(line);
$       date=date|line[k]();
$   end;
$   v=v_date;
$   until urleof();
$end;
$urlclose();
$return {v,heading};
$endfunction

Let us try with the Google stock from 2010 to 2013.

>{v,hd}=getstock("GOOG",day(2010,1,1),day(2013,12,31));
>hd
 Date
 Open
 High
 Low
 Close
 Volume
 Adj Close
>dates=fliplr(v[,1]'); dates=dates-dates[1]; values=fliplr(v[,7]');
>figure(2,1);  ...
>figure(1); plot2d(dates,values); ...
>figure(2); plot2d(head(dates,-2),differences(log(values)));  ...
>figure(0):

test

Here is function, which makes this even easier.

>function plotsince (stock,year) ...
${v,hd}=getstock(stock,day(year,1,1),daynow());
$dates=fliplr(v[,1]'); dates=dates; values=fliplr(v[,7]');
$years=(dates-dates[1])/365.25+2010;
$figure(2,1);
$figure(1); plot2d(years,values);
$figure(2); plot2d(head(years,-2),differences(log(values)));
$figure(0);
$settitle(stock);
$endfunction
>plotsince("AlV.DE",2010):

test

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.