-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrumb.scala
162 lines (87 loc) · 2.97 KB
/
drumb.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Advanced Part 3 about a really dumb investment strategy
//==========================================================
object CW6c {
//two test portfolios
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP")
import io.Source
import scala.util._
def get_january_data(symbol: String, year: Int): List[String] = {
val content = Source.fromFile(symbol + ".csv").mkString
val newContent = content.split("\n").toList
val toReturn = newContent.filter(newContent => newContent.startsWith(year.toString))
toReturn
}
def get_first_price(symbol: String, year: Int): Option[Double] = {
val jan_data = get_january_data(symbol, year)
if (jan_data.length < 0) {
None
}
else {
val first = jan_data.head
val arr = first.split(",")
val toReturn = arr(1).toDouble
Some(toReturn)
}
}
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = {
val toReturn = for (x <- years) yield {
val hej: List[Option[Double]] = for (y <- portfolio) yield {
val addTo: Option[Double] = get_first_price(y, x)
addTo
}
hej
}
toReturn.toList
}
def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = {
if (price_new.isDefined && price_old.isDefined) {
val pn: Double = price_new.get
val po: Double = price_old.get
val toReturn = (pn - po) / po
Some(toReturn)
}
else {
None
}
}
def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] = {
val toReturn = for (x <- (0 until data.size - 1).toList) yield {
val temp = for (y <- (0 until data.head.size).toList) yield {
val addTo: Option[Double] = get_delta(data(x)(y), data(x + 1)(y))
addTo
}
temp
}
toReturn
}
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
val nrCompanies = data.head.size
val perCompany = balance / nrCompanies
if (data(year).nonEmpty) {
val toReturn = for (x <- (0 until data(year).size).toArray) yield {
perCompany * data(year)(x).get
}
toReturn.sum.toLong + balance
}
else {
balance
}
}
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int) : Long = {
if(year == data.size - 1){
yearly_yield(data, balance, year)
}
else if( data.size == 2){
yearly_yield(data, balance, year)
}
else{
compound_yield(data, yearly_yield(data, balance, year), year + 1)
}
}
def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = {
val prices = get_prices(portfolio, years)
val deltasPrices = get_deltas(prices)
compound_yield(deltasPrices, start_balance , 0)
}
}