1 people like it.

Processing Prices & Charting (Demo)

Sample code using a simple library for downloading Yahoo stock prices. The demo shows how to process sequences and so on (to run this, load & run previous snippet).

  1: 
  2: 
  3: 
  4: 
  5: 
  6: 
  7: 
  8: 
  9: 
 10: 
 11: 
 12: 
 13: 
 14: 
 15: 
 16: 
 17: 
 18: 
 19: 
 20: 
 21: 
 22: 
 23: 
 24: 
 25: 
 26: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
// --------------------------------------------------------
// Downlaod Facebook stock prices & draw line chart

let fb = Yahoo.GetPrices("FB")
Plot.Line(fb)
Plot.Clear()

// TASK #1: Download 'AAPL' data and draw the chart
// (...)

// --------------------------------------------------------
// Do some calculations with Facebook stock prices

let count = Seq.length fb
let sum = Seq.sum fb
let avg = sum / float count

// Declare a function that calculates the average
let average data = 
  let count = Seq.length data
  let sum = Seq.sum data
  sum / float count

average fb

// Simple function with multiple arguments and type annotations
let triangle (a:float) (b:float) = 
  sqrt ((pown a 2) + (pown b 2))

triangle 3.0 4.0

// Using F# sequence expressions to work with data
let diffs = [ for v in fb -> v - avg ]

// --------------------------------------------------------
// TASK #2a: Calculate the standard deviation of FB prices
// TASK #2b: Write a function 'sdv' that takes a parameter

// (...)

// --------------------------------------------------------
// More advanced sequence expressions 

let avg = Seq.average aapl

// More explicit way of writin the previous
// (a lot more powerful - we can filter, etc.)
let diffs = 
  [ for v in aapl do 
      yield v - avg ]

// Count number of days when price is above average
let more = 
  [ for v in aapl do
      if v > avg then yield v ]
Seq.length more  

// TASK #3: Compare the days when price is above/below avg

// --------------------------------------------------------
// More functions for processing sequences

// Sort & reverse the sequence
let sorted = Seq.rev (Seq.sort fb)
Plot.Line(sorted, name="Sorted")
Plot.Line(fb, name="Normal")
Plot.Clear()

// Take first and last elements
Seq.nth 0 fb
Seq.nth ((Seq.length fb) - 1) fb

// TASK #4a: Calculate the median of the Facebook prices
// TASK #4b: Write a function & use it on Apple data too!

// (...)

// Get values as sequence of pairs (previous and next day price)
let pairs = Seq.pairwise fb
[ for (prev, next) in pairs do
    yield (prev + next) / 2.0 ]

// TASK #5: Calculate how many times did the price
// go up/down between the two consequent days

// --------------------------------------------------------

// Download MSFT and YHOO prices & draw them in a single chart
let yhoo = Yahoo.GetPrices("YHOO", from = DateTime(2012, 1, 1))
let msft = Yahoo.GetPrices("MSFT", from = DateTime(2012, 1, 1))

Plot.Line(yhoo, name="Yhoo", range=(10.0, 35.0))
Plot.Line(msft, name="Msft", range=(10.0, 35.0))
Plot.Clear()

// TASK #6 (BONUS): The Seq.windowed function generalizes 
// Seq.pairwise and returns windows of any given size 
// (As sequences - so you get sequence of sequences!)

// TASK #6a: Use the function to calculate floating average 
// over 10 days and plot that together with the origianl values

// TASK #6b: Use the function & your previous 'sdv' to 
// show the price with two more lines showing value +/- sdv
val fb : seq<float>

Full name: Script.fb
val count : int

Full name: Script.count
module Seq

from Microsoft.FSharp.Collections
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
val sum : float

Full name: Script.sum
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sum
val avg : float

Full name: Script.avg
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val average : data:seq<float> -> float

Full name: Script.average
val data : seq<float>
val count : int
val sum : float
val triangle : a:float -> b:float -> float

Full name: Script.triangle
val a : float
val b : float
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
val pown : x:'T -> n:int -> 'T (requires member get_One and member ( * ) and member ( / ))

Full name: Microsoft.FSharp.Core.Operators.pown
val diffs : float list

Full name: Script.diffs
val v : float
val avg : int

Full name: Script.avg
val average : source:seq<'T> -> 'T (requires member ( + ) and member DivideByInt and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.average
val diffs : int list

Full name: Script.diffs
val v : int
val more : int list

Full name: Script.more
val sorted : obj

Full name: Script.sorted
val sort : source:seq<'T> -> seq<'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Seq.sort
val nth : index:int -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.nth
val pairs : seq<float * float>

Full name: Script.pairs
val pairwise : source:seq<'T> -> seq<'T * 'T>

Full name: Microsoft.FSharp.Collections.Seq.pairwise
val prev : float
val next : float
val yhoo : obj

Full name: Script.yhoo
val msft : obj

Full name: Script.msft
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/co
Posted:12 years ago
Author:Tomas Petricek
Tags: try f#