progs/trade.scala
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--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 8
diff changeset
     1
// Part 2
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
def trade_times(xs: List[Double]): (Int, Int) = {
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  val low = xs.min
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
  val low_index = xs.indexOf(low)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  val rest = xs.drop(low_index)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  val high = rest.max
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  val high_index = rest.indexOf(high)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  (low_index, low_index + high_index)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
}
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
import io.Source
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
import scala.util._
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
def get_page(url: String): List[String] = {
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  Try(Source.fromURL(url)("ISO-8859-1").getLines.toList).
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
    getOrElse { println(s"  Problem with: $url"); List() }
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
}
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def process_page(url: String): List[(String, Double)] = {
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  get_page(url).drop(1).map(_.split(",").toList).map((xs) => (xs(0), xs(6).toDouble))
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
}
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
}
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
/*
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
scala trade.scala 2> /dev/null || echo "command1 borked it"
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
command1
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
if [ $? -ne 0 ]; then
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
    echo "command1 borked it"
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
fi
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
*/
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67