main_testing1/drumb.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 22:01:53 +0000
changeset 463 0315d9983cd0
parent 402 de59aa20a1dc
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
402
de59aa20a1dc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 363
diff changeset
     1
// Main Part 1 about a really dumb investment strategy
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
     2
//===================================================
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
     4
object M1 {
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
     6
  //two test portfolios
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
     8
  val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
     9
  val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI",
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    10
    "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP")
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    12
  import io.Source
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    13
  import java.time.LocalDate
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    16
  // ADD YOUR CODE BELOW
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    17
  //======================
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    19
    def main(args: Array[String]): Unit = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    20
      val data = get_january_data("GOOG", 2010)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    21
  //    val ppp = get_prices(List("GOOG", "FB"), (2005 to 2007))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    22
  //    val rrr = get_first_price("GOOG", 2007)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    23
  
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    24
      println(get_january_data("GOOG", 1980) == List())
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    25
      println(get_january_data("GOOG", 2010).head == "2010-01-04,312.204773")
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    26
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    27
      val sss = ""
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    28
    }
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    30
  def get_stock_data(symbol: String): List[(LocalDate, String)] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    31
    val content = Source.fromFile(symbol + ".csv").mkString
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    32
    val dtf = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd")
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    33
    content
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    34
      .split("\r\n")
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    35
      .filter(!_.toLowerCase.startsWith("date")) // Ignore first row (headers)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    36
      .map(p => (LocalDate.parse(p.substring(0, p.indexOf(",")), dtf), p.substring(p.indexOf(",") + 1, p.length)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    37
      .toList
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    38
  }
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    40
  // (1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    41
  def get_january_data(symbol: String, year: Int): List[String] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    42
    get_stock_data(symbol).filter(_._1.getYear == year).map(p => p._1.toString + "," + p._2)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    43
  }
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    46
  // (2)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    47
  def get_first_price(symbol: String, year: Int): Option[Double] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    48
    val data = get_stock_data(symbol).filter(_._1.getYear == year)
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    50
    if (data.nonEmpty) {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    51
      data
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    52
        .minBy(_._1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    53
        ._2
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    54
        .toDoubleOption
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    55
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    56
    else {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    57
      None
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    58
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    59
  }
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    62
  // (3)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    63
  def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    64
    portfolio
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    65
      .map(symbol => years.map(year => get_first_price(symbol, year)).toList)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    66
  }
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    69
  // (4)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    70
  def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = ???
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    73
  // (5)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    74
  def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] = ???
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    76
  // (6)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    77
  def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = ???
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    80
  // (7)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    81
  def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = ???
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    83
  def investment(portfolio: List[String], years: Range, start_balance: Long): Long = ???
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    86
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    87
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    88
  //Test cases for the two portfolios given above
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    89
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    90
  //println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    91
  //println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
402
de59aa20a1dc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 363
diff changeset
    92
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
402
de59aa20a1dc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 363
diff changeset
    98
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
    99
// This template code is subject to copyright 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   100
// by King's College London, 2022. Do not 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   101
// make the template code public in any shape 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   102
// or form, and do not exchange it with other 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   103
// students under any circumstance.