progs/drumb.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 23:09:51 +0000
changeset 465 8ce150207792
parent 133 7c3ca6005af1
permissions -rw-r--r--
updated to scala 3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
     1
// Advanvced Part 3 about really dumb investing strategy
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
     2
//=======================================================
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
//two test portfolios
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CBG", "CCI", 
133
7c3ca6005af1 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
     8
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    11
// (1) The function below should obtain the first trading price
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    12
// for a stock symbol by using the query
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    13
//
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    14
//    http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    15
// 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    16
// and extracting the first January Adjusted Close price in a year.
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
19
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    18
def get_first_price(symbol: String, year: Int): Option[Double] = ...
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    20
// Complete the function below that obtains all first prices
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    21
// for the stock symbols from a portfolio for the given
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    22
// range of years
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    23
19
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    24
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = ...
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    26
// test case
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    27
//val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    28
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    30
// (2) The first function below calculates the change factor (delta) between
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    31
// a price in year n and a price in year n+1. The second function calculates
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    32
// all change factors for all prices (from a portfolio).
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
19
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    34
def get_delta(price_old: Option[Double], price_new: Option[Double]): Option[Double] = ...
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
19
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    36
def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] = ...
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    38
// test case using the prices calculated above
19
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    39
//val d = get_deltas(p)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    42
// (3) Write a function that given change factors, a starting balance and a year
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    43
// calculates the yearly yield, i.e. new balanace, according to our dump investment 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    44
// strategy. Another function calculates given the same data calculates the
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    45
// compound yield up to a given year. Finally a function combines all 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    46
// calculations by taking a portfolio, a range of years and a start balance
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    47
// as arguments.
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
19
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    49
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... 
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    50
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    51
//test case
19
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    52
//yearly_yield(d, 100, 0)
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    53
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    54
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = ... 
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    55
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    56
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = ...
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    57
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    58
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    59
//test cases for the two portfolios given above
19
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    60
//investment(rstate_portfolio, 1978 to 2016, 100)
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    61
//investment(blchip_portfolio, 1978 to 2016, 100)
fa18976cf4cf updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    62