progs/trade.scala
changeset 11 417869f65585
parent 8 ab77f6006f1f
child 18 87e55eb309ed
equal deleted inserted replaced
10:e7eeeb5b41dc 11:417869f65585
       
     1 // Part 2
     1 
     2 
     2 
     3 // (1) the function that calculuates the indices
     3 val trades = List(28.0, 18.0, 20.0, 26.0, 24.0)
     4 // for when to buy the commodity and when to sell
     4 
       
     5 def trade_times(xs: List[Double]): (Int, Int) = {
     5 def trade_times(xs: List[Double]): (Int, Int) = {
     6   val low = xs.min
     6   val low = xs.min
     7   val low_index = xs.indexOf(low)
     7   val low_index = xs.indexOf(low)
     8   val rest = xs.drop(low_index)
     8   val rest = xs.drop(low_index)
     9   val high = rest.max
     9   val high = rest.max
    10   val high_index = rest.indexOf(high)
    10   val high_index = rest.indexOf(high)
    11   (low_index, low_index + high_index)
    11   (low_index, low_index + high_index)
    12 }
    12 }
    13 
    13 
    14 
    14 
    15 trade_times(trades)
    15 val prices = List(28.0, 18.0, 20.0, 26.0, 24.0)
    16 
    16 
    17 assert(trade_times(List(10.0, 8.0, 7.0, 6.0)) == (3, 3), "the first test fails")
    17 trade_times(prices)
    18 
    18 assert(trade_times(prices) == (1, 3), "the first test fails")
    19 
    19 
    20 
    20 
    21 import io.Source
    21 import io.Source
    22 import scala.util._
    22 import scala.util._
    23 
    23 
       
    24 // (2) the function that queries the Yahoo financial data
       
    25 // servive and returns a comma-separated-value list
    24 def get_page(url: String): List[String] = {
    26 def get_page(url: String): List[String] = {
    25   Try(Source.fromURL(url)("ISO-8859-1").getLines.toList).
    27   Try(Source.fromURL(url)("ISO-8859-1").getLines.toList).
    26     getOrElse { println(s"  Problem with: $url"); List() }
    28     getOrElse { println(s"  Problem with: $url"); List() }
    27 }
    29 }
    28 
    30 
       
    31 // (3) the function that processes the comma-separated-value list
       
    32 // extracting the dates and anjusted close prices
    29 def process_page(url: String): List[(String, Double)] = {
    33 def process_page(url: String): List[(String, Double)] = {
    30   get_page(url).drop(1).map(_.split(",").toList).map((xs) => (xs(0), xs(6).toDouble))
    34   get_page(url).drop(1).map(_.split(",").toList).map((xs) => (xs(0), xs(6).toDouble))
    31 }
    35 }
    32 
    36 
    33 def query_comp(name: String): (Int, Int) = {
    37 // (4) the function that generates the query for a stock
    34   val list = process_page("""http://ichart.yahoo.com/table.csv?s=""" + name)
    38 // market symbol and returns the dates for when to buy and
    35   trade_times(list.reverse.map(_._2))
    39 // sell
       
    40 def query_comp(name: String): (String, String) = {
       
    41   val list = process_page("""http://ichart.yahoo.com/table.csv?s=""" + name).reverse
       
    42   val (tbuy, tsell) = trade_times(list.map(_._2))
       
    43   (list(tbuy)._1, list(tsell)._1)
    36 }
    44 }
       
    45 
       
    46 //query_comp("GOOG")
    37 
    47 
    38 
    48 
    39 val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
    49 val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
    40 
    50 
    41 indices.map(query_comp(_))
    51 for (name <- indices) {
       
    52   val times = query_comp(name)
       
    53   println(s"Buy ${name} on ${times._1} and sell on ${times._2}")
       
    54 }
    42 
    55 
    43 val appl_page = get_page("""http://ichart.yahoo.com/table.csv?s=GOOG""")
       
    44 val goog_page = get_page("""http://ichart.yahoo.com/table.csv?s=GOOG""")
       
    45 goog_page.length
       
    46 
       
    47 val pairs = goog_page.drop(1).map(_.split(",").toList).map((xs:List[String]) => (xs(0), xs(6).toDouble))
       
    48 
       
    49 pairs.map(_._2)
       
    50 
       
    51 trade_times(pairs.reverse.map(_._2))
       
    52 
       
    53 pairs(3067)
       
    54 pairs(11)
       
    55 
    56 
    56 
    57 
    57 /*
    58 /*
    58 scala trade.scala 2> /dev/null || echo "command1 borked it"
    59 scala trade.scala 2> /dev/null || echo "command1 borked it"
    59 
    60