1 // Advanvced Part 3 about really dumb investing strategy |
|
2 //======================================================= |
|
3 |
|
4 //two test portfolios |
|
5 |
|
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", |
|
8 "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") |
|
9 |
|
10 |
|
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. |
|
17 |
|
18 def get_first_price(symbol: String, year: Int): Option[Double] = ... |
|
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 |
|
24 def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = ... |
|
25 |
|
26 // test case |
|
27 //val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012) |
|
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). |
|
33 |
|
34 def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = ... |
|
35 |
|
36 def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] = ... |
|
37 |
|
38 // test case using the prices calculated above |
|
39 //val d = get_deltas(p) |
|
40 |
|
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. |
|
48 |
|
49 def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... |
|
50 |
|
51 //test case |
|
52 //yearly_yield(d, 100, 0) |
|
53 |
|
54 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... |
|
55 |
|
56 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = ... |
|
57 |
|
58 |
|
59 //test cases for the two portfolios given above |
|
60 //investment(rstate_portfolio, 1978 to 2016, 100) |
|
61 //investment(blchip_portfolio, 1978 to 2016, 100) |
|
62 |
|