|
1 // Advanced Part 3 about a really dumb investment strategy |
|
2 //========================================================== |
|
3 |
|
4 object CW6c { |
|
5 |
|
6 |
|
7 //two test portfolios |
|
8 |
|
9 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU") |
|
10 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", |
|
11 "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") |
|
12 |
|
13 |
|
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._ |
|
21 |
|
22 //def get_january_data(symbol: String, year: Int) : List[String] = ... |
|
23 |
|
24 |
|
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 |
|
28 // generated by get_january_data then the result is None |
|
29 |
|
30 |
|
31 //def get_first_price(symbol: String, year: Int) : Option[Double] = ... |
|
32 |
|
33 |
|
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. |
|
38 |
|
39 |
|
40 //def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ... |
|
41 |
|
42 |
|
43 |
|
44 // (2) The first function below calculates the change factor (delta) between |
|
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. |
|
48 |
|
49 //def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = ... |
|
50 |
|
51 //def get_deltas(data: List[List[Option[Double]]]) : List[List[Option[Double]]] = ... |
|
52 |
|
53 |
|
54 |
|
55 // (3) Write a function that given change factors, a starting balance and a year |
|
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 |
|
62 |
|
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 = ... |
|
66 |
|
67 //def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ... |
|
68 |
|
69 |
|
70 |
|
71 //test cases for the two portfolios given above |
|
72 |
|
73 //investment(rstate_portfolio, 1978 to 2017, 100) |
|
74 //investment(blchip_portfolio, 1978 to 2017, 100) |
|
75 |
|
76 } |