| author | Christian Urban <urbanc@in.tum.de> | 
| Wed, 08 Nov 2017 13:58:11 +0000 | |
| changeset 131 | 06d8d1c1a61d | 
| parent 129 | 1b0f1573c27c | 
| child 140 | aac534649b27 | 
| permissions | -rw-r--r-- | 
| 129 | 1  | 
// Advanvced Part 3 about a really dumb investment strategy  | 
2  | 
//==========================================================  | 
|
3  | 
||
4  | 
object CW6c {
 | 
|
5  | 
||
| 18 | 6  | 
|
7  | 
//two test portfolios  | 
|
8  | 
||
| 129 | 9  | 
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
 | 
10  | 
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
 | 
|
| 18 | 11  | 
"DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP")  | 
12  | 
||
| 26 | 13  | 
// (1) The function below should obtain the first trading price  | 
14  | 
// for a stock symbol by using the query  | 
|
15  | 
//  | 
|
16  | 
// http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>>  | 
|
17  | 
//  | 
|
18  | 
// and extracting the first January Adjusted Close price in a year.  | 
|
| 18 | 19  | 
|
| 129 | 20  | 
|
| 18 | 21  | 
import io.Source  | 
22  | 
import scala.util._  | 
|
23  | 
||
| 129 | 24  | 
def get_january_data(symbol: String, year: Int) : List[String] =  | 
25  | 
  Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
 | 
|
26  | 
||
27  | 
||
28  | 
def get_first_price(symbol: String, year: Int) : Option[Double] = {
 | 
|
29  | 
val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None  | 
|
30  | 
  data.map(_.split(",").toList(1).toDouble)
 | 
|
| 18 | 31  | 
}  | 
32  | 
||
| 129 | 33  | 
get_first_price("GOOG", 1980)
 | 
34  | 
get_first_price("GOOG", 2010)
 | 
|
35  | 
get_first_price("FB", 2014)
 | 
|
| 18 | 36  | 
|
| 26 | 37  | 
|
38  | 
// Complete the function below that obtains all first prices  | 
|
39  | 
// for the stock symbols from a portfolio for the given  | 
|
40  | 
// range of years  | 
|
41  | 
||
| 18 | 42  | 
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] =  | 
43  | 
for (year <- years.toList) yield  | 
|
44  | 
for (symbol <- portfolio) yield get_first_price(symbol, year)  | 
|
45  | 
||
| 26 | 46  | 
|
47  | 
// test case  | 
|
| 62 | 48  | 
val p_fb = get_prices(List("FB"), 2012 to 2014)
 | 
| 129 | 49  | 
val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
 | 
| 18 | 50  | 
|
| 131 | 51  | 
val tt = get_prices(List("BIDU"), 2004 to 2008)
 | 
| 18 | 52  | 
|
| 26 | 53  | 
// (2) The first function below calculates the change factor (delta) between  | 
54  | 
// a price in year n and a price in year n+1. The second function calculates  | 
|
55  | 
// all change factors for all prices (from a portfolio).  | 
|
56  | 
||
| 129 | 57  | 
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
 | 
| 18 | 58  | 
  (price_old, price_new) match {
 | 
59  | 
case (Some(x), Some(y)) => Some((y - x) / x)  | 
|
60  | 
case _ => None  | 
|
61  | 
}  | 
|
62  | 
}  | 
|
63  | 
||
64  | 
def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] =  | 
|
65  | 
for (i <- (0 until (data.length - 1)).toList) yield  | 
|
66  | 
for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))  | 
|
67  | 
||
| 26 | 68  | 
|
69  | 
// 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
 | 
70  | 
val d = get_deltas(p)  | 
| 82 | 71  | 
val ttd = get_deltas(tt)  | 
| 26 | 72  | 
|
73  | 
// (3) Write a function that given change factors, a starting balance and a year  | 
|
74  | 
// calculates the yearly yield, i.e. new balanace, according to our dump investment  | 
|
75  | 
// strategy. Another function calculates given the same data calculates the  | 
|
76  | 
// compound yield up to a given year. Finally a function combines all  | 
|
77  | 
// calculations by taking a portfolio, a range of years and a start balance  | 
|
78  | 
// as arguments.  | 
|
79  | 
||
80  | 
||
| 18 | 81  | 
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
 | 
82  | 
val somes = data(year).flatten  | 
|
83  | 
val somes_length = somes.length  | 
|
84  | 
if (somes_length == 0) balance  | 
|
85  | 
  else {
 | 
|
| 62 | 86  | 
val portion: Double = balance.toDouble / somes_length.toDouble  | 
| 18 | 87  | 
balance + (for (x <- somes) yield (x * portion)).sum.toLong  | 
88  | 
}  | 
|
89  | 
}  | 
|
90  | 
||
91  | 
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
 | 
|
92  | 
  if (year >= data.length) balance else {
 | 
|
93  | 
val new_balance = yearly_yield(data, balance, year)  | 
|
94  | 
compound_yield(data, new_balance, year + 1)  | 
|
95  | 
}  | 
|
96  | 
}  | 
|
97  | 
||
| 131 | 98  | 
yearly_yield(d, 100, 0)  | 
99  | 
//compound_yield(d.take(6), 100, 0)  | 
|
| 
52
 
7a4fe3f6b188
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
39 
diff
changeset
 | 
100  | 
|
| 18 | 101  | 
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
 | 
102  | 
compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)  | 
|
103  | 
}  | 
|
104  | 
||
| 131 | 105  | 
|
| 18 | 106  | 
|
| 26 | 107  | 
//test cases for the two portfolios given above  | 
108  | 
||
| 131 | 109  | 
//println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
 | 
110  | 
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
 | 
|
| 18 | 111  | 
|
| 82 | 112  | 
|
| 129 | 113  | 
}  |