author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sun, 06 Nov 2016 18:28:47 +0000 | |
changeset 11 | 417869f65585 |
parent 8 | ab77f6006f1f |
child 18 | 87e55eb309ed |
permissions | -rw-r--r-- |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
1 |
// Part 2 |
8 | 2 |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
3 |
// (1) the function that calculuates the indices |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
4 |
// for when to buy the commodity and when to sell |
8 | 5 |
def trade_times(xs: List[Double]): (Int, Int) = { |
6 |
val low = xs.min |
|
7 |
val low_index = xs.indexOf(low) |
|
8 |
val rest = xs.drop(low_index) |
|
9 |
val high = rest.max |
|
10 |
val high_index = rest.indexOf(high) |
|
11 |
(low_index, low_index + high_index) |
|
12 |
} |
|
13 |
||
14 |
||
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
15 |
val prices = List(28.0, 18.0, 20.0, 26.0, 24.0) |
8 | 16 |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
17 |
trade_times(prices) |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
18 |
assert(trade_times(prices) == (1, 3), "the first test fails") |
8 | 19 |
|
20 |
||
21 |
import io.Source |
|
22 |
import scala.util._ |
|
23 |
||
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
24 |
// (2) the function that queries the Yahoo financial data |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
25 |
// servive and returns a comma-separated-value list |
8 | 26 |
def get_page(url: String): List[String] = { |
27 |
Try(Source.fromURL(url)("ISO-8859-1").getLines.toList). |
|
28 |
getOrElse { println(s" Problem with: $url"); List() } |
|
29 |
} |
|
30 |
||
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
31 |
// (3) the function that processes the comma-separated-value list |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
32 |
// extracting the dates and anjusted close prices |
8 | 33 |
def process_page(url: String): List[(String, Double)] = { |
34 |
get_page(url).drop(1).map(_.split(",").toList).map((xs) => (xs(0), xs(6).toDouble)) |
|
35 |
} |
|
36 |
||
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
37 |
// (4) the function that generates the query for a stock |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
38 |
// market symbol and returns the dates for when to buy and |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
39 |
// sell |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
40 |
def query_comp(name: String): (String, String) = { |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
41 |
val list = process_page("""http://ichart.yahoo.com/table.csv?s=""" + name).reverse |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
42 |
val (tbuy, tsell) = trade_times(list.map(_._2)) |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
43 |
(list(tbuy)._1, list(tsell)._1) |
8 | 44 |
} |
45 |
||
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
46 |
//query_comp("GOOG") |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
47 |
|
8 | 48 |
|
49 |
val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU") |
|
50 |
||
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
51 |
for (name <- indices) { |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
52 |
val times = query_comp(name) |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
8
diff
changeset
|
53 |
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
|
54 |
} |
8 | 55 |
|
56 |
||
57 |
||
58 |
/* |
|
59 |
scala trade.scala 2> /dev/null || echo "command1 borked it" |
|
60 |
||
61 |
command1 |
|
62 |
if [ $? -ne 0 ]; then |
|
63 |
echo "command1 borked it" |
|
64 |
fi |
|
65 |
*/ |
|
66 |
||
67 |