progs/trade.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 05 Nov 2016 17:11:47 +0000
changeset 8 ab77f6006f1f
child 11 417869f65585
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
val trades = List(28.0, 18.0, 20.0, 26.0, 24.0)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
trade_times(trades)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
assert(trade_times(List(10.0, 8.0, 7.0, 6.0)) == (3, 3), "the first test fails")
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
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
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def get_page(url: String): List[String] = {
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  Try(Source.fromURL(url)("ISO-8859-1").getLines.toList).
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
    getOrElse { println(s"  Problem with: $url"); List() }
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
}
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
def process_page(url: String): List[(String, Double)] = {
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  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
    31
}
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def query_comp(name: String): (Int, Int) = {
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  val list = process_page("""http://ichart.yahoo.com/table.csv?s=""" + name)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  trade_times(list.reverse.map(_._2))
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
}
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
indices.map(query_comp(_))
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
val appl_page = get_page("""http://ichart.yahoo.com/table.csv?s=GOOG""")
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
val goog_page = get_page("""http://ichart.yahoo.com/table.csv?s=GOOG""")
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
goog_page.length
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
val pairs = goog_page.drop(1).map(_.split(",").toList).map((xs:List[String]) => (xs(0), xs(6).toDouble))
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
pairs.map(_._2)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
trade_times(pairs.reverse.map(_._2))
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
pairs(3067)
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
pairs(11)
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
scala trade.scala 2> /dev/null || echo "command1 borked it"
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
command1
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
if [ $? -ne 0 ]; then
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
    echo "command1 borked it"
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
fi
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
*/
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66