templates/drumb.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 08 Nov 2017 15:45:33 +0000
changeset 132 2ca6db82d207
parent 129 b1a51285de7e
child 135 077e63e96287
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Advanvced Part 3 about a really dumb investment strategy
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==========================================================
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW6c {
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//two test portfolios
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CBG", "CCI", 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
132
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    14
// (1.a) The function below takes a stock symbol and a year as argument.
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    15
//       It should read the corresponding CSV-file and read the january 
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    16
//       data from that year. The data should be collected in a list of
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    17
//       strings for each line in the CSV file.
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    18
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    19
//def get_january_data(symbol: String, year: Int) : List[String] = ...
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    20
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
//
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
//    http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// and extracting the first January Adjusted Close price in a year.
132
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    25
// 
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    26
// (1.b) From the output of the get_january_data function, the next function 
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    27
//       should extract the first line (if it exists) and the corresponding
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    28
//       first trading price in that year as Option[Double]. If no line is 
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    29
//       given 
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    30
//
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
//def get_first_price(symbol: String, year: Int) : Option[Double] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
// Complete the function below that obtains all first prices
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
// for the stock symbols from a portfolio for the given
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
// range of years
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
// test case
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// (2) The first function below calculates the change factor (delta) between
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// a price in year n and a price in year n+1. The second function calculates
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// all change factors for all prices (from a portfolio).
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
//def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
//def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
// test case using the prices calculated above
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
//val d = get_deltas(p)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
// (3) Write a function that given change factors, a starting balance and a year
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
// calculates the yearly yield, i.e. new balanace, according to our dump investment 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
// strategy. Another function calculates given the same data calculates the
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
// compound yield up to a given year. Finally a function combines all 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
// calculations by taking a portfolio, a range of years and a start balance
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
// as arguments.
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
//def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
//test case
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
//yearly_yield(d, 100, 0)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
//def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//def investment(portfolio: List[String], years: Range, start_balance: Long): Long = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
//test cases for the two portfolios given above
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
//investment(rstate_portfolio, 1978 to 2016, 100)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
//investment(blchip_portfolio, 1978 to 2016, 100)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
}