templates1/drumb.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 23 Apr 2020 14:49:54 +0100
changeset 334 305e1dcdb3bc
parent 281 32dfd2ca577b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
281
32dfd2ca577b updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     1
// Core Part about a really dumb investment strategy
266
31e5218f43de updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
     2
//===================================================
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
281
32dfd2ca577b updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     4
object CW6b {
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
//two test portfolios
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
144
41a2b4f2c30c updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
     8
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
41a2b4f2c30c updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
     9
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    10
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") 
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    13
// (1) The function below takes a stock symbol and a year as arguments.
266
31e5218f43de updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    14
//     It should read the corresponding CSV-file and then extract the January 
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    15
//     data from the given year. The data should be collected in a list of
266
31e5218f43de updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    16
//     strings (one entry for each line in the CSV-file).
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    17
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    18
import io.Source
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    19
import scala.util._
132
d91f907b6ed3 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    20
d91f907b6ed3 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    21
//def get_january_data(symbol: String, year: Int) : List[String] = ...
d91f907b6ed3 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    22
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    23
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    24
// (2) From the output of the get_january_data function, the next function 
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    25
//     should extract the first line (if it exists) and the corresponding
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    26
//     first trading price in that year with type Option[Double]. If no line 
266
31e5218f43de updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    27
//     is generated by get_january_data then the result is None; and Some if 
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    28
//     there is a price.
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    29
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
//def get_first_price(symbol: String, year: Int) : Option[Double] = ...
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    34
// (3) Complete the function below that obtains all first prices
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    35
//     for the stock symbols from a portfolio (list of strings) and 
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    36
//     for the given range of years. The inner lists are for the
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    37
//     stock symbols and the outer list for the years.
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    40
//def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ...
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    41
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    44
// (4) The function below calculates the change factor (delta) between
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    45
//     a price in year n and a price in year n + 1. 
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    47
//def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = ...
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    49
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    50
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    51
// (5) The next function calculates all change factors for all prices (from a 
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    52
//     portfolio). The input to this function are the nested lists created by 
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    53
//     get_prices above.
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    54
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    55
//def get_deltas(data: List[List[Option[Double]]]) :  List[List[Option[Double]]] = ...
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    56
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    59
// (6) Write a function that given change factors, a starting balance and an index,
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    60
//     calculates the yearly yield, i.e. new balance, according to our dumb investment 
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    61
//     strategy. Index points to a year in the data list.
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    62
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    63
//def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ... 
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    64
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    66
// (7) Write a function compound_yield that calculates the overall balance for a 
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    67
//     range of years where in each year the yearly profit is compounded to the new 
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    68
//     balances and then re-invested into our portfolio. For this use the function and 
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    69
//     results generated under (6). The function investment calls compound_yield
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    70
//     with the appropriate deltas and the first index.
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    71
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    72
//def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ... 
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    74
//def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ...
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    78
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    79
//Test cases for the two portfolios given above
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
266
31e5218f43de updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    81
//println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
31e5218f43de updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    82
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
199
2e13dedd922e updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    84
281
32dfd2ca577b updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
    85
}