author | Christian Urban <christian.urban@kcl.ac.uk> |
Wed, 02 Nov 2022 21:49:42 +0000 | |
changeset 430 | 274c865b3878 |
parent 35 | 9fea5f751be4 |
permissions | -rw-r--r-- |
18 | 1 |
// Part 2 about Buy-Low-Sell-High using Yahoo Financial Data |
2 |
//=========================================================== |
|
8 | 3 |
|
4 |
||
18 | 5 |
// (1) Complete the function that is given a list of floats |
6 |
// and calculuates the indices for when to buy the commodity |
|
7 |
// and when to sell |
|
8 | 8 |
|
18 | 9 |
def trade_times(xs: List[Double]): (Int, Int) = ... |
10 |
||
11 |
||
12 |
// an example |
|
13 |
//val prices = List(28.0, 18.0, 20.0, 26.0, 24.0) |
|
14 |
//assert(trade_times(prices) == (1, 3), "the trade_times test fails") |
|
8 | 15 |
|
16 |
||
18 | 17 |
// (2) Complete the ``get webpage'' function that takes a |
18 |
// a stock symbol as argument and queries the Yahoo server |
|
19 |
// at |
|
20 |
// http://ichart.yahoo.com/table.csv?s=<<insert stock symbol>> |
|
21 |
// |
|
22 |
// This servive returns a CSV-list that needs to be separated into |
|
23 |
// a list of strings. |
|
8 | 24 |
|
18 | 25 |
def get_page(symbol: String): List[String] = ... |
8 | 26 |
|
18 | 27 |
// (3) Complete the function that processes the CSV list |
28 |
// extracting the dates and anjusted close prices. The |
|
29 |
// prices need to be transformed into Doubles. |
|
30 |
||
31 |
def process_page(symbol: String): List[(String, Double)] = ... |
|
32 |
||
8 | 33 |
|
18 | 34 |
// (4) Complete the query_company function that obtains the |
35 |
// processed CSV-list for a stock symbol. It should return |
|
36 |
// the dates for when to buy and sell the stocks of that company. |
|
37 |
||
38 |
def query_company(symbol: String): (String, String) = |
|
39 |
||
40 |
||
41 |
||
42 |
// some test cases |
|
8 | 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 | 46 |
// some more test cases |
47 |
/* |
|
8 | 48 |
val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU") |
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 | 54 |
*/ |
55 |
||
56 |