<?php require('config.php'); require('SRC/macros.php');  
list ($home,$url,$users,$assign,$msg,$host,$sql,$MYCookie,$applet,$os)=courseAll();
$user_nbr=getaUserNbr($$MYCookie);
list ($user_name,$user_home,$user_url,$user_profile)=userid($user_nbr);

$downld_error="";                               # ----- Default
if ($user_nbr<=99) { $o=0; $ex="0.00"; }        # Need login for estimation
else               { $o=1; $ex="0.01"; }
                                                # wget depends on OS
if (substr($os,0,6)=="Sun5.8") $wget='/usr/local/bin/wget';
else $wget='wget';
                                                # ----- Define data
if (!isset($resubmit) || $user_nbr<=99) {       # predefined symbol
  if (!isset($file) || $file=="") {
    $s='csco';
    if (!isset($a)) {
      $a=date("m");$b=date("d");$c=date("Y")-1;
      $d=$a;$e=$b;$f=date("Y");
    }
    $URLfile=$url.$users."DATA/table.csv";
    $DIRfile=$home.$users."DATA/table.csv";
    if (file_exists($DIRfile)) {
      if (!(date("d",filemtime($DIRfile)) == date("d"))) {
        $cmd="rm -f ".$home.$users."DATA/table.csv";
        system($cmd,$stat);
      }
    }
  } else {
    $URLfile=$url."downld/quotes/".$file;
    $DIRfile="../downld/quotes/".$file;
  }
  if (strlen($a)<2) $a="0".$a;                  # correct input fields
  if (strlen($b)<2) $b="0".$b;
  if (strlen($d)<2) $d="0".$d;
  if (strlen($e)<2) $e="0".$e;

} else {                                        # open, correct date format
  if (strlen($a)<2) $a="0".$a;
  if (strlen($b)<2) $b="0".$b;
  if ($c<50)        $c=$c+2000;
  else if ($c<100)  $c=$c+1900;
  if (strlen($d)<2) $d="0".$d;
  if (strlen($e)<2) $e="0".$e;
  if ($f<50)        $f=$f+2000;
  else if ($f<100)  $f=$f+1900;

  $file=$s."-".substr($c,2,2).$a.$b."-".substr($f,2,2).$d.$e.".csv";
  $URLfile=$url.$users."DATA/".$file;
  $DIRfile=$home.$users."DATA/".$file;
}

if (!file_exists($DIRfile)) {                    # ----- Download from yahoo
  $cmd=$wget." -O ".$DIRfile.
       " 'http://table.finance.yahoo.com/table.csv?".
       "s=$s&d=$d&e=$e&f=$f&a=$a&b=$b&c=$c&g=d&ignore=.csv';".
         " chmod ugo+rw $DIRfile";
  system($cmd,$stat);
  if ($stat) {
    $downld_error="<blink><b>ERROR downloading requested data: using ".
       "default instead.</b></blink><br>";
    $s='csco'; $a='05'; $b='12'; $c='2003'; $d='05'; $e='11'; $f='2004';
    $URLfile="$url"."downld/quotes/csco-900426-040511.csv";
    $DIRfile="../downld/quotes/csco-900426-040511.csv";
  }
}

if ($op=="download csv format") {               # ----- Display raw data
  echo "$downld_error <pre>";
  $cmd="cat $DIRfile | grep -v '<!--'";
  passthru($cmd); echo "</pre>"; exit();
} else if ($op=="download for Matlab") {
  echo "$downld_error <pre>";
  $cmd="cat $DIRfile ".
       "|sed s/..-...-..,//  |sed s/.-...-..,//|sed s/,/\ /g |grep -v Date |grep -v '<!--'";
  passthru($cmd); echo "</pre>"; exit();
} else if ($op=="print estimation to console") { 
  $o=1; $ex="0.01";
} else if ($op=="print solution to console") { 
  $o=2; $ex="0.01";
}
                                                # ----- Display plot
myheader("Market data",'..',$myPermit);

echo "Daily prices of shares for a majority of public companies around 
  the world are available.
  Use the <a href=\"http://finance.yahoo.com/l\">symbol search utility</a> 
  from yahoo to identify a ticker (e.g. CSCO for Cisco Systems) and 
  <b>choose a short (1 year) time interval</b> first to limit the amount 
  of data. The selector <b>defines the output format</b>.
  More information under <a href=MKT_help_tag.php>this link</a>.";
if ($user_nbr<=99) 
  echo " You need to 
  <a href=\"../login.php\">login</a> to enjoy an unrestricted access;
  open learners may however follow the links below the applet to display 
  relevant examples spanning over the period 1990--2004.";
echo "<br>
<form action=marketdata.php method=post>
  <input type=hidden name=resubmit value=1>
  <input type=hidden name=file value=$file>
  <table border=0 cellspacing=0 align=left>
    <td><a href=\"http://finance.yahoo.com/l\">Symbol:</a>
        <input type=text size=8 name=s value=\"".$s."\"></td>
    <td>From:
        <input type=text size=2 name=b value=\"".$b."\">/
        <input type=text size=2 name=a value=\"".$a."\">/
        <input type=text size=4 name=c value=\"".$c."\"></td>
    <td>To:
        <input type=text size=2 name=e value=\"".$e."\">/
        <input type=text size=2 name=d value=\"".$d."\">/
        <input type=text size=4 name=f value=\"".$f."\"></td>
    <td><select name=op>
          <option selected>plot data in window
          <option>print estimation to console
          <option>print solution to console
          <option>download csv format
          <option>download for Matlab
        </select></td>
    <td><input type=submit value=Go></td>
  </table><br clear=both>
</form>";

if (strlen($downld_error)>0) 
  echo $downld_error;
                                                # ----- MKTSolution
echo "<center>
<applet codebase=\"../applet/MKT/\" CODE=MKTSolution width=780 height=300>
  <param name=URL value=\"$URLfile\"> 
  <param name=symbol value=\"$s\">    <param name=exercise value=\"$ex\">
  <param name=fromDay value=\"$b\">   <param name=toDay value=\"$e\">
  <param name=fromMonth value=\"$a\"> <param name=toMonth value=\"$d\">
  <param name=fromYear value=\"$c\">  <param name=toYear value=\"$f\">
  <param name=bgColor value=DCDCDC>   <param name=dkColor value=FFF5EE>
  <param name=appletWidth value=780>  <param name=appletHeight value=300>
  <param name=canvasWidth value=550>  <param name=canvasHeight value=300>
  <param name=leftMargin value=80>    <param name=rightMargin value=40>
  <param name=bottomMargin value=40>  <param name=topMargin value=40>
  <param name=output value=\"$o\">
</applet></center><br>
";

echo "<a href=MKT_help_tag.php>MKTSolution applet:</a>
  select one or several quantites in the table and press <b>Draw</b>.
  Click inside the plot area, check the browser status field and the 
  <a href=../SRC/faq.php#7>java console</a> to visualize the trading date 
  and measure; the data can also be downloaded using the selectors above.";

$a=05; $b=01; $c=1990; $d=05; $e=30; $f=2004;
$lnk="&a=$a&b=$b&c=$c&d=$d&e=$e&f=$f&g=d&q=q&y=0&x=.csv";
echo "Here below is a list of symbols with a link to the market data  
      from the last 14 years... 
<ol> 
<li><a href=marketdata.php?file=tnx-900501-040430.csv&s=tnx".$lnk.">^TNX</a> 10 years US treasury bond yield (carefull: bond yield, not price)
<li><a href=marketdata.php?file=tyx-900501-040430.csv&s=tyx".$lnk.">^TYX</a> 30 years US treasury bond yield (carefull: bond yield, not price)
<li><a href=marketdata.php?file=ssebi-900501-040430.csv&s=ssebi".$lnk.">^SSEBI</a> Shanghai exchange bond index 
<li><a href=marketdata.php?file=rxtb-900501-040430.csv&s=rxtb".$lnk.">^RXTB</a> Stockholm exchange bond index 
<li><a href=marketdata.php?file=lxd-960515-040430.csv&s=^lxd".$lnk.">^LXD</a> Nasdaq stock market index
<li><a href=marketdata.php?file=adi-900501-040430.csv&s=adi".$lnk.">ADI</a> Analogue Devices (electronics) 
<li><a href=marketdata.php?file=ba-900501-040430.csv&s=ba".$lnk.">BA</a> Boeing (airplanes) 
<li><a href=marketdata.php?file=coke-900501-040430.csv&s=coke".$lnk.">COKE</a> Coca-Cola (beverages) 
<li><a href=marketdata.php?file=csco-900501-040430.csv&s=csco".$lnk.">CSCO</a> Cisco (Internet) 
<li><a href=marketdata.php?file=cvx-900501-040430.csv&s=cvx".$lnk.">CVX</a> Chevron Texaco (energy) 
<li><a href=marketdata.php?file=dis-900501-040430.csv&s=dis".$lnk.">DIS</a> Disney (entertainment) 
<li><a href=marketdata.php?file=dna-900501-040430.csv&s=dna".$lnk.">DNA</a> Genentech (biotech) 
<li><a href=marketdata.php?file=ebay-980924-040430.csv&s=ebay".$lnk.">EBAY</a> Ebay (auctions) 
<li><a href=marketdata.php?file=gm-900501-040430.csv&s=gm".$lnk.">GM</a> General motors (cars) 
<li><a href=marketdata.php?file=ibm-900501-040430.csv&s=ibm".$lnk.">IBM</a> IBM (computers) 
<li><a href=marketdata.php?file=intc-900501-040430.csv&s=intc".$lnk.">INTC</a> Intel (micro-chips) 
<li><a href=marketdata.php?file=kft-010614-040430.csv&s=kft".$lnk.">KFT</a> Kraft (food) 
<li><a href=marketdata.php?file=lmt-900501-040430.csv&s=lmt".$lnk.">LMT</a> Lokheed Martin (warfare) 
<li><a href=marketdata.php?file=mo-900501-040430.csv&s=mo".$lnk.">MO</a> Philip Morris (food) 
<li><a href=marketdata.php?file=jpm-900501-040430.csv&s=jpm".$lnk.">JPM</a> JP Morgan (bank) 
<li><a href=marketdata.php?file=hit-900501-040430.csv&s=hit".$lnk.">HIT</a> Hitachi (consumer electronics) 
<li><a href=marketdata.php?file=hmc-900501-040430.csv&s=hmc".$lnk.">HMC</a> Honda (cars) 
<li><a href=marketdata.php?file=sne-900501-040430.csv&s=sne".$lnk.">SNE</a> Sony (consumer electronics) 
<li><a href=marketdata.php?file=abb-010406-040430.csv&s=abb".$lnk.">ABB</a> ABB (electrical engineering) 
<li><a href=marketdata.php?file=azn-930517-040430.csv&s=azn".$lnk.">AZN</a> Astra Zenecca (pharma) 
<li><a href=marketdata.php?file=ericy-900501-040430.csv&s=ericy".$lnk.">ERICY</a> Ericsson (consumer electronics) 
<li><a href=marketdata.php?file=nok-950425-040430.csv&s=nok".$lnk.">NOK</a> Nokia (consumer electronics) 
<li><a href=marketdata.php?file=phg-900501-040430.csv&s=phg".$lnk.">PHG</a> Philips (consumer electronics) 
<li><a href=marketdata.php?file=volvy-900501-040430.csv&s=volvy".$lnk.">VOLVY</a> Volvo (cars) 
</ol> ";

echo "<b>Note:</b> daily quotes (Date, Open, High, Low, Close, Volume, Adjusted Close) 
appear with the last quote first in the csv format. 
With Matlab, you can download the file with suppressed days using the command <tt>load</tt>
and then reverse the numbering using the command <tt>flipud</tt>.";

?>
<br><p>
<?php myfooter($user_nbr,'..',$myPermit); ?>
