135
|
1 |
// Advanced Part 3 about a really dumb investment strategy
|
129
|
2 |
//==========================================================
|
|
3 |
|
|
4 |
object CW6c {
|
|
5 |
|
|
6 |
|
|
7 |
//two test portfolios
|
|
8 |
|
144
|
9 |
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
|
|
10 |
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI",
|
129
|
11 |
"DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP")
|
|
12 |
|
|
13 |
|
135
|
14 |
// (1.a) The function below takes a stock symbol and a year as arguments.
|
|
15 |
// It should read the corresponding CSV-file and read the January
|
|
16 |
// data from the given year. The data should be collected in a list of
|
|
17 |
// strings for each line in the CSV-file.
|
|
18 |
|
|
19 |
import io.Source
|
|
20 |
import scala.util._
|
132
|
21 |
|
|
22 |
//def get_january_data(symbol: String, year: Int) : List[String] = ...
|
|
23 |
|
135
|
24 |
|
132
|
25 |
// (1.b) From the output of the get_january_data function, the next function
|
|
26 |
// should extract the first line (if it exists) and the corresponding
|
|
27 |
// first trading price in that year as Option[Double]. If no line is
|
135
|
28 |
// generated by get_january_data then the result is None
|
|
29 |
|
129
|
30 |
|
|
31 |
//def get_first_price(symbol: String, year: Int) : Option[Double] = ...
|
|
32 |
|
|
33 |
|
135
|
34 |
// (1.c) Complete the function below that obtains all first prices
|
|
35 |
// for the stock symbols from a portfolio (list of strings) and
|
|
36 |
// for the given range of years. The inner lists are for the
|
|
37 |
// stock symbols and the outer list for the years.
|
129
|
38 |
|
|
39 |
|
135
|
40 |
//def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ...
|
|
41 |
|
129
|
42 |
|
|
43 |
|
|
44 |
// (2) The first function below calculates the change factor (delta) between
|
135
|
45 |
// a price in year n and a price in year n + 1. The second function calculates
|
|
46 |
// all change factors for all prices (from a portfolio). The input to this
|
|
47 |
// function are the nested lists created by get_prices above.
|
129
|
48 |
|
135
|
49 |
//def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = ...
|
129
|
50 |
|
135
|
51 |
//def get_deltas(data: List[List[Option[Double]]]) : List[List[Option[Double]]] = ...
|
|
52 |
|
129
|
53 |
|
|
54 |
|
|
55 |
// (3) Write a function that given change factors, a starting balance and a year
|
135
|
56 |
// calculates the yearly yield, i.e. new balance, according to our dump investment
|
|
57 |
// strategy. Another function calculates given the same data calculates the
|
|
58 |
// compound yield up to a given year. Finally a function combines all
|
|
59 |
// calculations by taking a portfolio, a range of years and a start balance
|
|
60 |
// as arguments.
|
|
61 |
|
129
|
62 |
|
135
|
63 |
//def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int) : Long = ...
|
|
64 |
|
|
65 |
//def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int) : Long = ...
|
129
|
66 |
|
135
|
67 |
//def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ...
|
129
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
//test cases for the two portfolios given above
|
|
72 |
|
135
|
73 |
//investment(rstate_portfolio, 1978 to 2017, 100)
|
|
74 |
//investment(blchip_portfolio, 1978 to 2017, 100)
|
129
|
75 |
|
|
76 |
}
|