|
1 // Core Part about a really dumb investment strategy |
|
2 //=================================================== |
|
3 |
|
4 object CW6b { |
|
5 |
|
6 //two test portfolios |
|
7 |
|
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") |
|
11 |
|
12 |
|
13 // (1) The function below takes a stock symbol and a year as arguments. |
|
14 // It should read the corresponding CSV-file and then extract the January |
|
15 // data from the given year. The data should be collected in a list of |
|
16 // strings (one entry for each line in the CSV-file). |
|
17 |
|
18 import io.Source |
|
19 import scala.util._ |
|
20 |
|
21 def get_january_data(symbol: String, year: Int) : List[String] = ??? |
|
22 |
|
23 |
|
24 // (2) From the output of the get_january_data function, the next function |
|
25 // should extract the first line (if it exists) and the corresponding |
|
26 // first trading price in that year with type Option[Double]. If no line |
|
27 // is generated by get_january_data then the result is None; and Some if |
|
28 // there is a price. |
|
29 |
|
30 |
|
31 def get_first_price(symbol: String, year: Int) : Option[Double] = ??? |
|
32 |
|
33 |
|
34 // (3) 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 // (4) The function below calculates the change factor (delta) between |
|
45 // a price in year n and a price in year n + 1. |
|
46 |
|
47 def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = ??? |
|
48 |
|
49 |
|
50 |
|
51 // (5) The next function calculates all change factors for all prices (from a |
|
52 // portfolio). The input to this function are the nested lists created by |
|
53 // get_prices above. |
|
54 |
|
55 def get_deltas(data: List[List[Option[Double]]]) : List[List[Option[Double]]] = ??? |
|
56 |
|
57 |
|
58 |
|
59 // (6) Write a function that given change factors, a starting balance and an index, |
|
60 // calculates the yearly yield, i.e. new balance, according to our dumb investment |
|
61 // strategy. Index points to a year in the data list. |
|
62 |
|
63 def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ??? |
|
64 |
|
65 |
|
66 // (7) Write a function compound_yield that calculates the overall balance for a |
|
67 // range of years where in each year the yearly profit is compounded to the new |
|
68 // balances and then re-invested into our portfolio. For this use the function and |
|
69 // results generated under (6). The function investment calls compound_yield |
|
70 // with the appropriate deltas and the first index. |
|
71 |
|
72 def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ??? |
|
73 |
|
74 def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ??? |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 //Test cases for the two portfolios given above |
|
80 |
|
81 //println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100)) |
|
82 //println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100)) |
|
83 |
|
84 |
|
85 } |