progs/trade.scala
changeset 18 87e55eb309ed
parent 11 417869f65585
child 35 9fea5f751be4
equal deleted inserted replaced
17:ecf83084e41d 18:87e55eb309ed
     1 // Part 2
     1 // Part 2 about Buy-Low-Sell-High using Yahoo Financial Data
     2 
     2 //===========================================================
     3 // (1) the function that calculuates the indices
       
     4 // for when to buy the commodity and when to sell
       
     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 
     3 
    14 
     4 
    15 val prices = List(28.0, 18.0, 20.0, 26.0, 24.0)
     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
    16 
     8 
    17 trade_times(prices)
     9 def trade_times(xs: List[Double]): (Int, Int) = ...
    18 assert(trade_times(prices) == (1, 3), "the first test fails")
       
    19 
    10 
    20 
    11 
    21 import io.Source
    12 // an example
    22 import scala.util._
    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")
    23 
    15 
    24 // (2) the function that queries the Yahoo financial data
       
    25 // servive and returns a comma-separated-value list
       
    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 
    16 
    31 // (3) the function that processes the comma-separated-value list
    17 // (2) Complete the ``get webpage'' function that takes a
    32 // extracting the dates and anjusted close prices
    18 // a stock symbol as argument and queries the Yahoo server
    33 def process_page(url: String): List[(String, Double)] = {
    19 // at
    34   get_page(url).drop(1).map(_.split(",").toList).map((xs) => (xs(0), xs(6).toDouble))
    20 //      http://ichart.yahoo.com/table.csv?s=<<insert stock symbol>>
    35 }
    21 // 
       
    22 // This servive returns a CSV-list that needs to be separated into
       
    23 // a list of strings.
    36 
    24 
    37 // (4) the function that generates the query for a stock
    25 def get_page(symbol: String): List[String] = ...
    38 // market symbol and returns the dates for when to buy and
    26 
    39 // sell
    27 // (3) Complete the function that processes the CSV list
    40 def query_comp(name: String): (String, String) = {
    28 // extracting the dates and anjusted close prices. The
    41   val list = process_page("""http://ichart.yahoo.com/table.csv?s=""" + name).reverse
    29 // prices need to be transformed into Doubles.
    42   val (tbuy, tsell) = trade_times(list.map(_._2))
    30 
    43   (list(tbuy)._1, list(tsell)._1)
    31 def process_page(symbol: String): List[(String, Double)] = ...
    44 }
    32 
       
    33 
       
    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
    45 
    43 
    46 //query_comp("GOOG")
    44 //query_comp("GOOG")
    47 
    45 
    48 
    46 // some more test cases
       
    47 /*
    49 val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
    48 val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
    50 
    49 
    51 for (name <- indices) {
    50 for (name <- indices) {
    52   val times = query_comp(name)
    51   val times = query_comp(name)
    53   println(s"Buy ${name} on ${times._1} and sell on ${times._2}")
    52   println(s"Buy ${name} on ${times._1} and sell on ${times._2}")
    54 }
    53 }
    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 */
    54 */
    66 
    55 
    67 
    56