1 // Advanvced Part 3 about really dumb investing strategy |
1 // Advanvced Part 3 about a really dumb investment strategy |
2 //======================================================= |
2 //========================================================== |
|
3 |
|
4 object CW6c { |
|
5 |
3 |
6 |
4 //two test portfolios |
7 //two test portfolios |
5 |
8 |
6 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU") |
9 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU") |
7 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CBG", "CCI", |
10 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", |
8 "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") |
11 "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") |
9 |
12 |
10 // (1) The function below should obtain the first trading price |
13 // (1) The function below should obtain the first trading price |
11 // for a stock symbol by using the query |
14 // for a stock symbol by using the query |
12 // |
15 // |
13 // http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> |
16 // http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> |
14 // |
17 // |
15 // and extracting the first January Adjusted Close price in a year. |
18 // and extracting the first January Adjusted Close price in a year. |
16 |
19 |
|
20 |
17 import io.Source |
21 import io.Source |
18 import scala.util._ |
22 import scala.util._ |
19 |
23 |
20 def get_yahoo_page(url: String): Option[List[String]] = { |
24 def get_january_data(symbol: String, year: Int) : List[String] = |
21 Try(Some(Source.fromURL(url)("ISO-8859-1").getLines.toList)). |
25 Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString)) |
22 getOrElse { None } |
26 |
|
27 |
|
28 get_trading_data("FB", 2015) |
|
29 get_trading_data("GOOG", 2010).head |
|
30 |
|
31 def get_first_price(symbol: String, year: Int) : Option[Double] = { |
|
32 val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None |
|
33 data.map(_.split(",").toList(1).toDouble) |
23 } |
34 } |
24 |
35 |
25 def get_first_price(symbol: String, year: Int): Option[Double] = { |
36 get_first_price("GOOG", 1980) |
26 //println(s"download..${symbol} at ${year}") |
37 get_first_price("GOOG", 2010) |
27 val year_string = year.toString |
38 get_first_price("FB", 2014) |
28 val date_string = s"&a=0&b=1&c=${year_string}&d=1&e=1&f=${year_string}" |
|
29 val url = """http://ichart.yahoo.com/table.csv?s=""" + symbol + date_string |
|
30 val data = get_yahoo_page(url) |
|
31 data.map(_.last.split(",").toList(6).toDouble) |
|
32 } |
|
33 |
39 |
34 |
40 |
35 // Complete the function below that obtains all first prices |
41 // Complete the function below that obtains all first prices |
36 // for the stock symbols from a portfolio for the given |
42 // for the stock symbols from a portfolio for the given |
37 // range of years |
43 // range of years |
41 for (symbol <- portfolio) yield get_first_price(symbol, year) |
47 for (symbol <- portfolio) yield get_first_price(symbol, year) |
42 |
48 |
43 |
49 |
44 // test case |
50 // test case |
45 val p_fb = get_prices(List("FB"), 2012 to 2014) |
51 val p_fb = get_prices(List("FB"), 2012 to 2014) |
46 val p = get_prices(List("GOOG", "AAPL"), 2005 to 2012) |
52 val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012) |
47 |
53 |
48 val tt = get_prices(List("IBM", "BIDU"), 2004 to 2008) |
54 val tt = get_prices(List("IBM", "BIDU"), 2004 to 2008) |
49 |
55 |
50 // (2) The first function below calculates the change factor (delta) between |
56 // (2) The first function below calculates the change factor (delta) between |
51 // a price in year n and a price in year n+1. The second function calculates |
57 // a price in year n and a price in year n+1. The second function calculates |
52 // all change factors for all prices (from a portfolio). |
58 // all change factors for all prices (from a portfolio). |
53 |
59 |
54 def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = { |
60 def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = { |
55 (price_old, price_new) match { |
61 (price_old, price_new) match { |
56 case (Some(x), Some(y)) => Some((y - x) / x) |
62 case (Some(x), Some(y)) => Some((y - x) / x) |
57 case _ => None |
63 case _ => None |
58 } |
64 } |
59 } |
65 } |
83 val portion: Double = balance.toDouble / somes_length.toDouble |
89 val portion: Double = balance.toDouble / somes_length.toDouble |
84 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
90 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
85 } |
91 } |
86 } |
92 } |
87 |
93 |
88 yearly_yield(ttd, 100, 0) |
94 yearly_yield(d, 100, 0) |
89 yearly_yield(ttd, 100, 1) |
95 yearly_yield(ttd, 100, 1) |
90 yearly_yield(ttd, 100, 2) |
96 yearly_yield(ttd, 100, 2) |
91 yearly_yield(ttd, 100, 3) assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2) |
97 yearly_yield(ttd, 100, 3) |
92 assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2) |
98 |
93 assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2) |
99 assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2) |
94 assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2) |
100 assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2) |
|
101 assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2) |
|
102 assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2) |
95 |
103 |
96 |
104 |
97 |
105 |
98 //test case |
106 //test case |
99 yearly_yield(d, 100, 0) |
107 yearly_yield(d, 100, 0) |
121 |
129 |
122 investment(List("GOOG", "AAPL"), 2005 to 2009, 100) |
130 investment(List("GOOG", "AAPL"), 2005 to 2009, 100) |
123 |
131 |
124 //test cases for the two portfolios given above |
132 //test cases for the two portfolios given above |
125 |
133 |
126 println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100)) |
134 println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100)) |
127 println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100)) |
135 println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100)) |
128 |
136 |
129 |
137 |
130 investment(List("IBM", "BIDU"), 2004 to 2008, 100) |
138 investment(List("IBM", "BIDU"), 2004 to 2008, 100) |
131 |
139 |
132 val p_fb = get_prices(List("FB"), 2011 to 2016) |
140 val p_fb = get_prices(List("FB"), 2011 to 2016) |