| author | Christian Urban <christian dot urban at kcl dot ac dot uk> | 
| Wed, 25 Jan 2017 14:36:39 +0000 | |
| changeset 105 | 0f9f774c7697 | 
| parent 82 | 28c8e17ab83a | 
| child 129 | 1b0f1573c27c | 
| permissions | -rw-r--r-- | 
| 39 | 1  | 
// Advanvced Part 3 about really dumb investing strategy  | 
| 26 | 2  | 
//=======================================================  | 
| 18 | 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  | 
||
| 26 | 10  | 
// (1) The function below should obtain the first trading price  | 
11  | 
// for a stock symbol by using the query  | 
|
12  | 
//  | 
|
13  | 
// http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>>  | 
|
14  | 
//  | 
|
15  | 
// and extracting the first January Adjusted Close price in a year.  | 
|
| 18 | 16  | 
|
17  | 
import io.Source  | 
|
18  | 
import scala.util._  | 
|
19  | 
||
20  | 
def get_yahoo_page(url: String): Option[List[String]] = {
 | 
|
21  | 
  Try(Some(Source.fromURL(url)("ISO-8859-1").getLines.toList)).
 | 
|
22  | 
    getOrElse { None }
 | 
|
23  | 
}  | 
|
24  | 
||
25  | 
def get_first_price(symbol: String, year: Int): Option[Double] = {
 | 
|
26  | 
  //println(s"download..${symbol} at ${year}")
 | 
|
27  | 
val year_string = year.toString  | 
|
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  | 
||
| 26 | 34  | 
|
35  | 
// Complete the function below that obtains all first prices  | 
|
36  | 
// for the stock symbols from a portfolio for the given  | 
|
37  | 
// range of years  | 
|
38  | 
||
| 18 | 39  | 
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] =  | 
40  | 
for (year <- years.toList) yield  | 
|
41  | 
for (symbol <- portfolio) yield get_first_price(symbol, year)  | 
|
42  | 
||
| 26 | 43  | 
|
44  | 
// test case  | 
|
| 62 | 45  | 
val p_fb = get_prices(List("FB"), 2012 to 2014)
 | 
| 
52
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
46  | 
val p = get_prices(List("GOOG", "AAPL"), 2005 to 2012)
 | 
| 18 | 47  | 
|
| 82 | 48  | 
val tt = get_prices(List("IBM", "BIDU"), 2004 to 2008)
 | 
| 18 | 49  | 
|
| 26 | 50  | 
// (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  | 
|
52  | 
// all change factors for all prices (from a portfolio).  | 
|
53  | 
||
| 18 | 54  | 
def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = {
 | 
55  | 
  (price_old, price_new) match {
 | 
|
56  | 
case (Some(x), Some(y)) => Some((y - x) / x)  | 
|
57  | 
case _ => None  | 
|
58  | 
}  | 
|
59  | 
}  | 
|
60  | 
||
61  | 
def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] =  | 
|
62  | 
for (i <- (0 until (data.length - 1)).toList) yield  | 
|
63  | 
for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))  | 
|
64  | 
||
| 26 | 65  | 
|
66  | 
// test case using the prices calculated above  | 
|
| 
52
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
67  | 
val d = get_deltas(p)  | 
| 82 | 68  | 
val ttd = get_deltas(tt)  | 
| 26 | 69  | 
|
70  | 
// (3) Write a function that given change factors, a starting balance and a year  | 
|
71  | 
// calculates the yearly yield, i.e. new balanace, according to our dump investment  | 
|
72  | 
// strategy. Another function calculates given the same data calculates the  | 
|
73  | 
// compound yield up to a given year. Finally a function combines all  | 
|
74  | 
// calculations by taking a portfolio, a range of years and a start balance  | 
|
75  | 
// as arguments.  | 
|
76  | 
||
77  | 
||
| 18 | 78  | 
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
 | 
79  | 
val somes = data(year).flatten  | 
|
80  | 
val somes_length = somes.length  | 
|
81  | 
if (somes_length == 0) balance  | 
|
82  | 
  else {
 | 
|
| 62 | 83  | 
val portion: Double = balance.toDouble / somes_length.toDouble  | 
| 18 | 84  | 
balance + (for (x <- somes) yield (x * portion)).sum.toLong  | 
85  | 
}  | 
|
86  | 
}  | 
|
87  | 
||
| 82 | 88  | 
yearly_yield(ttd, 100, 0)  | 
89  | 
yearly_yield(ttd, 100, 1)  | 
|
90  | 
yearly_yield(ttd, 100, 2)  | 
|
91  | 
yearly_yield(ttd, 100, 3) assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2)  | 
|
92  | 
assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2)  | 
|
93  | 
assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2)  | 
|
94  | 
assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2)  | 
|
95  | 
||
96  | 
||
97  | 
||
| 26 | 98  | 
//test case  | 
| 
52
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
99  | 
yearly_yield(d, 100, 0)  | 
| 
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
100  | 
yearly_yield(d, 225, 1)  | 
| 
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
101  | 
yearly_yield(d, 246, 2)  | 
| 
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
102  | 
yearly_yield(d, 466, 3)  | 
| 
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
103  | 
yearly_yield(d, 218, 4)  | 
| 
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
104  | 
yearly_yield(d, 469, 5)  | 
| 
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
105  | 
yearly_yield(d, 587, 6)  | 
| 26 | 106  | 
//yearly_yield(d, 100, 0)  | 
| 
52
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
107  | 
//yearly_yield(d, 125, 1)  | 
| 26 | 108  | 
|
| 18 | 109  | 
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
 | 
110  | 
  if (year >= data.length) balance else {
 | 
|
111  | 
val new_balance = yearly_yield(data, balance, year)  | 
|
112  | 
compound_yield(data, new_balance, year + 1)  | 
|
113  | 
}  | 
|
114  | 
}  | 
|
115  | 
||
| 
52
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
116  | 
compound_yield(d.take(6), 100, 0)  | 
| 
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
117  | 
|
| 18 | 118  | 
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
 | 
119  | 
compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)  | 
|
120  | 
}  | 
|
121  | 
||
| 
52
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
122  | 
investment(List("GOOG", "AAPL"), 2005 to 2009, 100)
 | 
| 18 | 123  | 
|
| 26 | 124  | 
//test cases for the two portfolios given above  | 
125  | 
||
| 18 | 126  | 
println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100))
 | 
127  | 
println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100))
 | 
|
128  | 
||
| 82 | 129  | 
|
130  | 
investment(List("IBM", "BIDU"), 2004 to 2008, 100)
 | 
|
131  | 
||
| 62 | 132  | 
val p_fb = get_prices(List("FB"), 2011 to 2016)
 | 
133  | 
||
134  | 
// prices 2011 - 2016  | 
|
135  | 
//  | 
|
136  | 
List(List(None), List(None), List(Some(28.0)),  | 
|
137  | 
List(Some(54.709999)), List(Some(78.449997)),  | 
|
138  | 
List(Some(102.220001)))  | 
|
139  | 
||
140  | 
// deltas 2011 - 2016  | 
|
141  | 
val d_fb = get_deltas(p_fb)  | 
|
142  | 
||
143  | 
List(List(None), List(None), List(Some(0.9539285357142858)),  | 
|
144  | 
List(Some(0.4339242996513305)), List(Some(0.30299560113431234)))  | 
|
145  | 
||
146  | 
yearly_yield(d_fb, 100, 0) //2011 => 100  | 
|
147  | 
yearly_yield(d_fb, 100, 1) //2012 => 100  | 
|
148  | 
yearly_yield(d_fb, 100, 2) //2013 => 195  | 
|
149  | 
yearly_yield(d_fb, 195, 3) //2014 => 279  | 
|
150  | 
yearly_yield(d_fb, 279, 4) //2015 => 363  | 
|
151  | 
||
152  | 
investment(List("FB"), 2011 to 2012, 100)  // => 100
 | 
|
153  | 
investment(List("FB"), 2011 to 2013, 100)  // => 100
 | 
|
154  | 
investment(List("FB"), 2011 to 2014, 100)  // => 195
 | 
|
155  | 
investment(List("FB"), 2011 to 2015, 100)  // => 279
 | 
|
156  | 
investment(List("FB"), 2011 to 2016, 100)  // => 363
 | 
|
157  | 
||
158  | 
||
| 64 | 159  | 
val rs_p = get_prices(rstate_portfolio, 1978 to 2016)  | 
160  | 
val bl_p = get_prices(blchip_portfolio, 1978 to 2016)  | 
|
161  | 
||
162  | 
val rs_d = get_deltas(rs_p)  | 
|
163  | 
val bl_d = get_deltas(bl_p)  | 
|
164  | 
||
165  | 
rs_p(0)  | 
|
166  | 
<-  | 
|
167  | 
rs_p(1)  | 
|
168  | 
<-  | 
|
169  | 
rs_p(2)  | 
|
170  | 
<-  | 
|
171  | 
rs_p(3)  | 
|
172  | 
||
173  | 
rs_d(0)  | 
|
174  | 
rs_d(1)  | 
|
175  | 
rs_d(2)  | 
|
176  | 
||
177  | 
yearly_yield(rs_d, 100, 0)  | 
|
178  | 
yearly_yield(rs_d, 96, 1)  | 
|
179  | 
yearly_yield(rs_d, 95, 2)  | 
|
180  | 
yearly_yield(rs_d, 134, 3)  | 
|
181  | 
yearly_yield(rs_d, 126, 4)  | 
|
182  | 
yearly_yield(rs_d, 169, 5)  | 
|
183  |