18
|
1 |
// Advanvced Part 3 about Mr T. Drumb investing into stocks
|
|
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 |
import io.Source
|
|
12 |
import scala.util._
|
|
13 |
|
|
14 |
def get_yahoo_page(url: String): Option[List[String]] = {
|
|
15 |
Try(Some(Source.fromURL(url)("ISO-8859-1").getLines.toList)).
|
|
16 |
getOrElse { None }
|
|
17 |
}
|
|
18 |
|
|
19 |
def get_first_price(symbol: String, year: Int): Option[Double] = {
|
|
20 |
//println(s"download..${symbol} at ${year}")
|
|
21 |
val year_string = year.toString
|
|
22 |
val date_string = s"&a=0&b=1&c=${year_string}&d=1&e=1&f=${year_string}"
|
|
23 |
val url = """http://ichart.yahoo.com/table.csv?s=""" + symbol + date_string
|
|
24 |
val data = get_yahoo_page(url)
|
|
25 |
data.map(_.last.split(",").toList(6).toDouble)
|
|
26 |
}
|
|
27 |
|
|
28 |
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] =
|
|
29 |
for (year <- years.toList) yield
|
|
30 |
for (symbol <- portfolio) yield get_first_price(symbol, year)
|
|
31 |
|
|
32 |
get_prices(List("GOOG", "AAPL"), 2010 to 2012)
|
|
33 |
|
|
34 |
|
|
35 |
def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = {
|
|
36 |
(price_old, price_new) match {
|
|
37 |
case (Some(x), Some(y)) => Some((y - x) / x)
|
|
38 |
case _ => None
|
|
39 |
}
|
|
40 |
}
|
|
41 |
|
|
42 |
def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] =
|
|
43 |
for (i <- (0 until (data.length - 1)).toList) yield
|
|
44 |
for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
|
|
45 |
|
|
46 |
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
|
|
47 |
val somes = data(year).flatten
|
|
48 |
val somes_length = somes.length
|
|
49 |
if (somes_length == 0) balance
|
|
50 |
else {
|
|
51 |
val portion: Double = balance / somes_length
|
|
52 |
balance + (for (x <- somes) yield (x * portion)).sum.toLong
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
|
|
57 |
if (year >= data.length) balance else {
|
|
58 |
val new_balance = yearly_yield(data, balance, year)
|
|
59 |
compound_yield(data, new_balance, year + 1)
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
|
|
64 |
val d = get_deltas(p)
|
|
65 |
yearly_yield(d, 100, 0)
|
|
66 |
|
|
67 |
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
|
|
68 |
compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
|
|
69 |
}
|
|
70 |
|
|
71 |
|
|
72 |
println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100))
|
|
73 |
println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100))
|
|
74 |
|