424
|
1 |
// Main Part 1 about a really dumb investment strategy
|
463
|
2 |
//===================================================
|
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
|
463
|
4 |
object M1 {
|
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
//two test portfolios
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI",
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
"DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
import io.Source
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
import scala.util._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
|
463
|
15 |
// ADD YOUR CODE BELOW
|
|
16 |
//======================
|
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
|
463
|
19 |
// (1)
|
|
20 |
def get_january_data(symbol: String, year: Int) : List[String] = {
|
|
21 |
Try(Source.fromFile(s"${symbol}.csv").getLines.toList.filter(_.startsWith(year.toString))).getOrElse(Nil)
|
|
22 |
}
|
|
23 |
// (2)
|
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
def get_first_price(symbol: String, year: Int) : Option[Double] = {
|
463
|
25 |
val list = get_january_data(symbol, year)
|
|
26 |
if (list == Nil) None else {
|
|
27 |
Some(list.head.split(",").toList(1).toDouble)
|
|
28 |
}
|
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
|
463
|
32 |
// (3)
|
|
33 |
def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = {
|
|
34 |
for (n <- years.toList) yield{
|
|
35 |
for (m <- portfolio) yield{
|
|
36 |
get_first_price(m, n)
|
|
37 |
}
|
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
463
|
43 |
// (4)
|
|
44 |
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
|
|
45 |
if (price_old != None && price_new != None) Some((price_new.get - price_old.get) / price_old.get) else None
|
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
|
463
|
50 |
// (5)
|
|
51 |
def get_deltas(data: List[List[Option[Double]]]) : List[List[Option[Double]]] = {
|
|
52 |
for (n <- data.tail) yield{
|
|
53 |
for (m <- n) yield{
|
|
54 |
get_delta(data(data.tail.indexOf(n))(n.indexOf(m)),m)
|
|
55 |
}
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
// (6)
|
|
60 |
def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = {
|
|
61 |
if(data.length == 0) balance else{
|
|
62 |
val equal = balance / data(index).flatten.length
|
|
63 |
val list = for(n <- data(index).flatten) yield n * equal
|
|
64 |
(balance + list.sum).toLong
|
|
65 |
}
|
|
66 |
}
|
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
|
463
|
68 |
// (7)
|
|
69 |
def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = {
|
|
70 |
val deltas = get_deltas(data)
|
|
71 |
if (deltas.length == 0) balance else{
|
|
72 |
if(deltas.length - 1 == index) yearly_yield(deltas, balance, index) else compound_yield(data, yearly_yield(deltas, balance, index), index + 1)
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = {
|
|
77 |
val list = get_prices(portfolio, years)
|
|
78 |
compound_yield(list, start_balance, 0)
|
|
79 |
}
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
//Test cases for the two portfolios given above
|
|
85 |
|
|
86 |
//println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
|
|
87 |
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
|
424
|
88 |
|
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
|
424
|
94 |
|
463
|
95 |
// This template code is subject to copyright
|
|
96 |
// by King's College London, 2022. Do not
|
|
97 |
// make the template code public in any shape
|
|
98 |
// or form, and do not exchange it with other
|
|
99 |
// students under any circumstance.
|