progs/trade.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 22:42:09 +0000
changeset 464 73ced118f73d
parent 35 9fea5f751be4
permissions -rw-r--r--
updated to scala 3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     1
// Part 2 about Buy-Low-Sell-High using Yahoo Financial Data
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     2
//===========================================================
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     5
// (1) Complete the function that is given a list of floats
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     6
// and calculuates the indices for when to buy the commodity 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     7
// and when to sell
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     9
def trade_times(xs: List[Double]): (Int, Int) = ...
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    10
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    11
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    12
// an example
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    13
//val prices = List(28.0, 18.0, 20.0, 26.0, 24.0)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    14
//assert(trade_times(prices) == (1, 3), "the trade_times test fails")
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    17
// (2) Complete the ``get webpage'' function that takes a
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    18
// a stock symbol as argument and queries the Yahoo server
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    19
// at
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    20
//      http://ichart.yahoo.com/table.csv?s=<<insert stock symbol>>
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    21
// 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    22
// This servive returns a CSV-list that needs to be separated into
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    23
// a list of strings.
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    25
def get_page(symbol: String): List[String] = ...
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    27
// (3) Complete the function that processes the CSV list
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    28
// extracting the dates and anjusted close prices. The
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    29
// prices need to be transformed into Doubles.
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    30
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    31
def process_page(symbol: String): List[(String, Double)] = ...
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    32
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    34
// (4) Complete the query_company function that obtains the
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    35
// processed CSV-list for a stock symbol. It should return
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    36
// the dates for when to buy and sell the stocks of that company.
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    37
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    38
def query_company(symbol: String): (String, String) =
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    39
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    40
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    41
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    42
// some test cases
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
35
9fea5f751be4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 18
diff changeset
    44
//query_company("GOOG")
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 8
diff changeset
    45
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    46
// some more test cases
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    47
/*
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 8
diff changeset
    50
for (name <- indices) {
35
9fea5f751be4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 18
diff changeset
    51
  val times = query_company(name)
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 8
diff changeset
    52
  println(s"Buy ${name} on ${times._1} and sell on ${times._2}")
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 8
diff changeset
    53
}
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
*/
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56