progs/drumb_sol.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 08 Nov 2017 13:58:11 +0000
changeset 131 1fb90f5d53ee
parent 129 b1a51285de7e
child 140 ecec79b9ab25
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     1
// Advanvced Part 3 about a really dumb investment strategy
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     2
//==========================================================
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     3
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     4
object CW6c {
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     5
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//two test portfolios
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     9
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    10
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    13
// (1) The function below should obtain the first trading price
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    14
// for a stock symbol by using the query
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    15
//
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    16
//    http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    17
// 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    18
// and extracting the first January Adjusted Close price in a year.
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    20
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
import io.Source
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
import scala.util._
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    24
def get_january_data(symbol: String, year: Int) : List[String] = 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    25
  Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    26
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    27
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    28
def get_first_price(symbol: String, year: Int) : Option[Double] = {
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    29
  val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    30
  data.map(_.split(",").toList(1).toDouble)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    33
get_first_price("GOOG", 1980)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    34
get_first_price("GOOG", 2010)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    35
get_first_price("FB", 2014)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    37
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    38
// Complete the function below that obtains all first prices
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    39
// for the stock symbols from a portfolio for the given
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    40
// range of years
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    41
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  for (year <- years.toList) yield
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    for (symbol <- portfolio) yield get_first_price(symbol, year)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    46
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    47
// test case
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
    48
val p_fb = get_prices(List("FB"), 2012 to 2014)
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    49
val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
131
1fb90f5d53ee updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    51
val tt = get_prices(List("BIDU"), 2004 to 2008)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    53
// (2) The first function below calculates the change factor (delta) between
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    54
// a price in year n and a price in year n+1. The second function calculates
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    55
// all change factors for all prices (from a portfolio).
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    56
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    57
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  (price_old, price_new) match {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    case (Some(x), Some(y)) => Some((y - x) / x)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
    case _ => None
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
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  for (i <- (0 until (data.length - 1)).toList) yield 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
    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
    67
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    68
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    69
// test case using the prices calculated above
52
7a4fe3f6b188 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
    70
val d = get_deltas(p)
82
28c8e17ab83a updated
Christian Urban <urbanc@in.tum.de>
parents: 81
diff changeset
    71
val ttd = get_deltas(tt)
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    72
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    73
// (3) Write a function that given change factors, a starting balance and a year
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    74
// calculates the yearly yield, i.e. new balanace, according to our dump investment 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    75
// strategy. Another function calculates given the same data calculates the
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    76
// compound yield up to a given year. Finally a function combines all 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    77
// calculations by taking a portfolio, a range of years and a start balance
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    78
// as arguments.
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    79
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    80
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  val somes = data(year).flatten
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  val somes_length = somes.length
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  if (somes_length == 0) balance
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  else {
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
    86
    val portion: Double = balance.toDouble / somes_length.toDouble
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
    balance + (for (x <- somes) yield (x * portion)).sum.toLong
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  if (year >= data.length) balance else {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
    val new_balance = yearly_yield(data, balance, year)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
    compound_yield(data, new_balance, year + 1)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
131
1fb90f5d53ee updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    98
yearly_yield(d, 100, 0)
1fb90f5d53ee updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    99
//compound_yield(d.take(6), 100, 0)
52
7a4fe3f6b188 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   100
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
131
1fb90f5d53ee updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
   105
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   107
//test cases for the two portfolios given above
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   108
131
1fb90f5d53ee updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
   109
//println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
1fb90f5d53ee updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
   110
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
82
28c8e17ab83a updated
Christian Urban <urbanc@in.tum.de>
parents: 81
diff changeset
   112
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   113
}