progs/drumb_sol.scala
changeset 129 b1a51285de7e
parent 82 28c8e17ab83a
child 131 1fb90f5d53ee
--- a/progs/drumb_sol.scala	Tue Nov 07 14:17:21 2017 +0000
+++ b/progs/drumb_sol.scala	Wed Nov 08 02:06:54 2017 +0000
@@ -1,10 +1,13 @@
-// Advanvced Part 3 about really dumb investing strategy
-//=======================================================
+// Advanvced Part 3 about a really dumb investment strategy
+//==========================================================
+
+object CW6c {
+
 
 //two test portfolios
 
-val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
-val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CBG", "CCI", 
+val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
+val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
                             "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
 
 // (1) The function below should obtain the first trading price
@@ -14,22 +17,25 @@
 // 
 // and extracting the first January Adjusted Close price in a year.
 
+
 import io.Source
 import scala.util._
 
-def get_yahoo_page(url: String): Option[List[String]] = {
-  Try(Some(Source.fromURL(url)("ISO-8859-1").getLines.toList)).
-    getOrElse { None }
+def get_january_data(symbol: String, year: Int) : List[String] = 
+  Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
+
+
+get_trading_data("FB", 2015)
+get_trading_data("GOOG", 2010).head
+
+def get_first_price(symbol: String, year: Int) : Option[Double] = {
+  val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
+  data.map(_.split(",").toList(1).toDouble)
 }
 
-def get_first_price(symbol: String, year: Int): Option[Double] = {
-  //println(s"download..${symbol} at ${year}")
-  val year_string = year.toString
-  val date_string = s"&a=0&b=1&c=${year_string}&d=1&e=1&f=${year_string}"
-  val url = """http://ichart.yahoo.com/table.csv?s=""" + symbol + date_string
-  val data = get_yahoo_page(url)
-  data.map(_.last.split(",").toList(6).toDouble)
-}
+get_first_price("GOOG", 1980)
+get_first_price("GOOG", 2010)
+get_first_price("FB", 2014)
 
 
 // Complete the function below that obtains all first prices
@@ -43,7 +49,7 @@
 
 // test case
 val p_fb = get_prices(List("FB"), 2012 to 2014)
-val p = get_prices(List("GOOG", "AAPL"), 2005 to 2012)
+val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
 
 val tt = get_prices(List("IBM", "BIDU"), 2004 to 2008)
 
@@ -51,7 +57,7 @@
 // a price in year n and a price in year n+1. The second function calculates
 // all change factors for all prices (from a portfolio).
 
-def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = {
+def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
   (price_old, price_new) match {
     case (Some(x), Some(y)) => Some((y - x) / x)
     case _ => None
@@ -85,13 +91,15 @@
   }
 }
 
-yearly_yield(ttd, 100, 0)
+yearly_yield(d, 100, 0)
 yearly_yield(ttd, 100, 1)
 yearly_yield(ttd, 100, 2)
-yearly_yield(ttd, 100, 3) assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2)
-  assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2)
-  assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2)
-  assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2)
+yearly_yield(ttd, 100, 3) 
+
+assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2)
+assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2)
+assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2)
+assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2)
 
 
 
@@ -123,8 +131,8 @@
 
 //test cases for the two portfolios given above
 
-println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100))
-println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100))
+println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
+println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
 
 
 investment(List("IBM", "BIDU"), 2004 to 2008, 100)
@@ -181,3 +189,4 @@
 yearly_yield(rs_d, 126, 4)
 yearly_yield(rs_d, 169, 5)
 
+}