progs/trade_sol.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 17 Nov 2016 03:10:44 +0000
changeset 55 6610c1dfa8a9
parent 18 87e55eb309ed
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 2 about Buy-Low-Sell-High using Yahoo Financial Data
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//===========================================================
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
// (1) Complete the function that is given a list of floats
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// and calculuates the indices for when to buy the commodity 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// and when to sell
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
def trade_times(xs: List[Double]): (Int, Int) = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  val low = xs.min
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  val low_index = xs.indexOf(low)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  val rest = xs.drop(low_index)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  val high = rest.max
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  val high_index = rest.indexOf(high)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  (low_index, low_index + high_index)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
val prices = List(28.0, 18.0, 20.0, 26.0, 24.0)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
assert(trade_times(prices) == (1, 3), "the trade_times test fails")
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
// (2) Complete the ``get webpage'' function that takes a
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// a stock symbol as argument and queries the Yahoo server
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// at
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
//      http://ichart.yahoo.com/table.csv?s=<<insert stock symbol>>
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
// 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
// This servive returns a CSV-list that needs to be separated into
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// a list of strings.
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
import io.Source
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
import scala.util._
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def get_yahoo_page(symbol: String): List[String] = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  val url = """http://ichart.yahoo.com/table.csv?s=""" + symbol
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  Try(Source.fromURL(url)("ISO-8859-1").getLines.toList).
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    getOrElse { println(s"  Problem with: $url"); List() }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// (3) Complete the function that processes the CSV list
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
// extracting the dates and anjusted close prices. The
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
// prices need to be transformed into Doubles.
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
def process_page(symbol: String): List[(String, Double)] = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  get_yahoo_page(symbol).drop(1).map(_.split(",").toList).map((xs) => (xs(0), xs(6).toDouble))
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// (4) Complete the query_company function that obtains the
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
// processed CSV-list for a stock symbol. It should return
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
// the dates for when to buy and sell the stocks of that company.
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
def query_company(symbol: String): (String, String) = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  val list = process_page(symbol).reverse
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  val (tbuy, tsell) = trade_times(list.map(_._2))
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  (list(tbuy)._1, list(tsell)._1)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
//query_company("GOOG")
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
for (name <- indices) {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  val times = query_company(name)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  println(s"Buy ${name} on ${times._1} and sell on ${times._2}")
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
/*
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
scala trade.scala 2> /dev/null || echo "command1 borked it"
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
command1
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
if [ $? -ne 0 ]; then
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
    echo "command1 borked it"
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
fi
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
*/
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79