1 // Advanvced Part 3 about Mr T. Drumb investing into stocks |
1 // Advanvced Part 3 about really dump investing strategy |
2 //========================================================== |
2 //======================================================= |
3 |
3 |
4 //two test portfolios |
4 //two test portfolios |
5 |
5 |
6 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU") |
6 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU") |
7 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CBG", "CCI", |
7 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CBG", "CCI", |
8 "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") |
8 "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") |
9 |
9 |
10 |
10 |
11 def get_yahoo_page(url: String): Option[List[String]] = ... |
11 // (1) The function below should obtain the first trading price |
|
12 // for a stock symbol by using the query |
|
13 // |
|
14 // http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> |
|
15 // |
|
16 // and extracting the first January Adjusted Close price in a year. |
12 |
17 |
13 def get_first_price(symbol: String, year: Int): Option[Double] = ... |
18 def get_first_price(symbol: String, year: Int): Option[Double] = ... |
14 |
19 |
|
20 // Complete the function below that obtains all first prices |
|
21 // for the stock symbols from a portfolio for the given |
|
22 // range of years |
|
23 |
15 def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = ... |
24 def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = ... |
16 |
25 |
17 |
26 // test case |
18 //val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012) |
27 //val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012) |
19 |
28 |
|
29 |
|
30 // (2) The first function below calculates the change factor (delta) between |
|
31 // a price in year n and a price in year n+1. The second function calculates |
|
32 // all change factors for all prices (from a portfolio). |
20 |
33 |
21 def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = ... |
34 def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = ... |
22 |
35 |
23 def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] = ... |
36 def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] = ... |
24 |
37 |
|
38 // test case using the prices calculated above |
25 //val d = get_deltas(p) |
39 //val d = get_deltas(p) |
26 |
40 |
27 |
41 |
|
42 // (3) Write a function that given change factors, a starting balance and a year |
|
43 // calculates the yearly yield, i.e. new balanace, according to our dump investment |
|
44 // strategy. Another function calculates given the same data calculates the |
|
45 // compound yield up to a given year. Finally a function combines all |
|
46 // calculations by taking a portfolio, a range of years and a start balance |
|
47 // as arguments. |
28 |
48 |
29 def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... |
49 def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... |
30 |
50 |
|
51 //test case |
31 //yearly_yield(d, 100, 0) |
52 //yearly_yield(d, 100, 0) |
32 |
53 |
33 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... |
54 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... |
34 |
55 |
35 |
|
36 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = ... |
56 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = ... |
37 |
57 |
38 |
58 |
|
59 //test cases for the two portfolios given above |
39 //investment(rstate_portfolio, 1978 to 2016, 100) |
60 //investment(rstate_portfolio, 1978 to 2016, 100) |
40 //investment(blchip_portfolio, 1978 to 2016, 100) |
61 //investment(blchip_portfolio, 1978 to 2016, 100) |
41 |
62 |