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