// Main Part 1 about a really dumb investment strategy
//===================================================
object M1 {
//two test portfolios
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", "HCP")
import io.Source
import java.time.LocalDate
// ADD YOUR CODE BELOW
//======================
def main(args: Array[String]): Unit = {
val data = get_january_data("GOOG", 2010)
// val ppp = get_prices(List("GOOG", "FB"), (2005 to 2007))
// val rrr = get_first_price("GOOG", 2007)
println(get_january_data("GOOG", 1980) == List())
println(get_january_data("GOOG", 2010).head == "2010-01-04,312.204773")
val sss = ""
}
def get_stock_data(symbol: String): List[(LocalDate, String)] = {
val content = Source.fromFile(symbol + ".csv").mkString
val dtf = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd")
content
.split("\r\n")
.filter(!_.toLowerCase.startsWith("date")) // Ignore first row (headers)
.map(p => (LocalDate.parse(p.substring(0, p.indexOf(",")), dtf), p.substring(p.indexOf(",") + 1, p.length)))
.toList
}
// (1)
def get_january_data(symbol: String, year: Int): List[String] = {
get_stock_data(symbol).filter(_._1.getYear == year).map(p => p._1.toString + "," + p._2)
}
// (2)
def get_first_price(symbol: String, year: Int): Option[Double] = {
val data = get_stock_data(symbol).filter(_._1.getYear == year)
if (data.nonEmpty) {
data
.minBy(_._1)
._2
.toDoubleOption
}
else {
None
}
}
// (3)
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = {
portfolio
.map(symbol => years.map(year => get_first_price(symbol, year)).toList)
}
// (4)
def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = ???
// (5)
def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] = ???
// (6)
def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = ???
// (7)
def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = ???
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = ???
//Test cases for the two portfolios given above
//println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
}
// This template code is subject to copyright
// by King's College London, 2022. Do not
// make the template code public in any shape
// or form, and do not exchange it with other
// students under any circumstance.