templates/drumb.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 08 Nov 2017 02:06:54 +0000
changeset 129 b1a51285de7e
child 132 2ca6db82d207
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
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// (1) The function below should obtain the first trading price
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
// for a stock symbol by using the query
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
//
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
//    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
    18
// 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
// and extracting the first January Adjusted Close price in a year.
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
//def get_january_data(symbol: String, year: Int) : List[String] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
//def get_first_price(symbol: String, year: Int) : Option[Double] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
// Complete the function below that obtains all first prices
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
// for the stock symbols from a portfolio for the given
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// range of years
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
//def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
// test case
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
//val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
// (2) The first function below calculates the change factor (delta) between
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
// 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
    38
// all change factors for all prices (from a portfolio).
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
//def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// test case using the prices calculated above
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
//val d = get_deltas(p)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// (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
    49
// calculates the yearly yield, i.e. new balanace, according to our dump investment 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
// strategy. Another function calculates given the same data calculates the
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
// compound yield up to a given year. Finally a function combines all 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
// calculations by taking a portfolio, a range of years and a start balance
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
// as arguments.
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
//def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
//test case
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
//yearly_yield(d, 100, 0)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
//def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
//def investment(portfolio: List[String], years: Range, start_balance: Long): Long = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
//test cases for the two portfolios given above
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
//investment(rstate_portfolio, 1978 to 2016, 100)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
//investment(blchip_portfolio, 1978 to 2016, 100)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
}