progs/drumb_sol.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 09 Nov 2016 13:36:13 +0000
changeset 24 66b97f9a40f8
parent 18 87e55eb309ed
child 26 a7afc2540a88
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Advanvced Part 3 about Mr T. Drumb investing into stocks
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==========================================================
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
//two test portfolios
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CBG", "CCI", 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
import io.Source
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
import scala.util._
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
def get_yahoo_page(url: String): Option[List[String]] = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  Try(Some(Source.fromURL(url)("ISO-8859-1").getLines.toList)).
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
    getOrElse { None }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def get_first_price(symbol: String, year: Int): Option[Double] = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  //println(s"download..${symbol} at ${year}")
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  val year_string = year.toString
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  val date_string = s"&a=0&b=1&c=${year_string}&d=1&e=1&f=${year_string}"
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  val url = """http://ichart.yahoo.com/table.csv?s=""" + symbol + date_string
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  val data = get_yahoo_page(url)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  data.map(_.last.split(",").toList(6).toDouble)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  for (year <- years.toList) yield
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
    for (symbol <- portfolio) yield get_first_price(symbol, year)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
get_prices(List("GOOG", "AAPL"), 2010 to 2012)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  (price_old, price_new) match {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    case (Some(x), Some(y)) => Some((y - x) / x)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
    case _ => None
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  for (i <- (0 until (data.length - 1)).toList) yield 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  val somes = data(year).flatten
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  val somes_length = somes.length
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  if (somes_length == 0) balance
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  else {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    val portion: Double = balance / somes_length
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
    balance + (for (x <- somes) yield (x * portion)).sum.toLong
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  if (year >= data.length) balance else {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
    val new_balance = yearly_yield(data, balance, year)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    compound_yield(data, new_balance, year + 1)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
val d = get_deltas(p)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
yearly_yield(d, 100, 0)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100))
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100))
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74