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