Yahoo Historical Quote Implementation

F# module to return historical EOD: open | high |low | close | volume quotes from Yahoo. Single day requests only. DataContract provided on the result record for easy serialization.

Copy Source
Copy Link
Tools:
  1: module Yahoo
  2:     open System
  3:     open System.Net 
  4:     open System.Text 
  5:     open System.Web
  6:     open System.IO
  7:     open System.Security.Authentication 
  8:     open System.Runtime.Serialization // needs system.runtime.serialization; system.xml
  9: 
 10: 
 11: (*          
 12: use datacontract defintiion of the record for serialization.
 13:     [<DataContract>] 
 14:     type YahooPrice = {
 15:         [<field: DataMember(Name="yahooSymbol") >] 
 16:         yahooSymbol : string ;
 17: 
 18:         [<field: DataMember(Name="day") >] 
 19:         day: string ;
 20: 
 21:         [<field: DataMember(Name="openPrice") >] 
 22:         openPrice: float ;
 23: 
 24:         [<field: DataMember(Name="closePrice") >] 
 25:         closePrice: float ;
 26: 
 27:         [<field: DataMember(Name="highPrice") >] 
 28:         highPrice: float ;
 29: 
 30:         [<field: DataMember(Name="lowPrice") >] 
 31:         lowPrice: float ;
 32: 
 33:         [<field: DataMember(Name="adjustedClosePrice") >] 
 34:         adjustedClosePrice: float;
 35: 
 36:         [<field: DataMember(Name="volume") >] 
 37:         volume: float;
 38: 
 39:     }
 40: *) 
 41: 
 42:     type YahooPrice = {
 43:         yahooSymbol : string ;
 44:         day: string ;
 45:         openPrice: float ;
 46:         closePrice: float ;
 47:         highPrice: float ;
 48:         lowPrice: float ;
 49:         adjustedClosePrice: float;
 50:         volume: float;
 51:     }
 52: 
 53:     type YahooRequest = {
 54:         day: string;
 55:         ticker: string ;
 56:     }
 57: 
 58:     exception BadResults of string * string
 59: 
 60:     let historicalQuote (r:YahooRequest) = 
 61:         
 62:         let date: string = r.day
 63:         let symbol: string = r.ticker
 64: 
 65:         let oops() = 
 66:             raise(BadResults(symbol, date))
 67: 
 68:         let pad (i:int) =  
 69:             let s = i.ToString() ; 
 70:             if s.Length < 2 then "0" + s else s
 71: 
 72:         let yr = date.Substring(0,4)
 73:         let mth = Int32.Parse(date.Substring(5,2)) - 1
 74:         let day = Int32.Parse(date.Substring(8,2))
 75:         let u = "http://ichart.finance.yahoo.com/table.csv?s=" + symbol + "&a=" + pad(mth) + "&b=" + pad(day) + "&c=" + yr + "&d=" + pad(mth) + "&e=" + pad(day) + "&f=" + yr
 76:         let request : HttpWebRequest = downcast WebRequest.Create(u) 
 77:         request.Method <- "GET" 
 78:         request.ContentType <- "application/x-www-form-urlencoded" 
 79:                 
 80:         let resultsArr =
 81:             try
 82:                 let response = request.GetResponse()        
 83:                 let result = 
 84:                     try 
 85:                         use reader = new StreamReader(response.GetResponseStream()) 
 86:                         reader.ReadToEnd(); 
 87:                     finally 
 88:                         response.Close() 
 89:                 if result.Split('\n').Length < 2 then oops()
 90:                 if result.Split('\n').[1].Length < 2 then oops()
 91:                 if result.Split('\n').[1].Split(',').Length < 7 then oops()
 92:                 result.Split('\n').[1].Split(',')
 93:             with 
 94:                 | _ -> [|date; "-1"; "-1"; "-1"; "-1"; "-1"; "-1";|]
 95: 
 96:         {
 97:             yahooSymbol  = symbol ;
 98:             day= resultsArr.[0] ;
 99:             openPrice= Double.Parse(resultsArr.[1]);
100:             closePrice= Double.Parse(resultsArr.[4]);
101:             highPrice= Double.Parse(resultsArr.[2]);
102:             lowPrice= Double.Parse(resultsArr.[3]);
103:             volume= Double.Parse(resultsArr.[5]);
104:             adjustedClosePrice= Double.Parse(resultsArr.[6]);
105:         }
106: 
107: 
108:     let valu = historicalQuote {day="2010/04/26"; ticker="MSFT";}
module Yahoo
namespace System
namespace System.Net
namespace System.Text
namespace System.Web
namespace System.IO
namespace System.Security
namespace System.Security.Authentication
namespace System.Runtime
namespace System.Runtime.Serialization
type YahooPrice =
  {yahooSymbol: string;
   day: string;
   openPrice: float;
   closePrice: float;
   highPrice: float;
   lowPrice: float;
   adjustedClosePrice: float;
   volume: float;}

Full name: Yahoo.YahooPrice

  type: YahooPrice
  implements: IEquatable<YahooPrice>
  implements: Collections.IStructuralEquatable
  implements: IComparable<YahooPrice>
  implements: IComparable
  implements: Collections.IStructuralComparable
YahooPrice.yahooSymbol: string
Multiple items
val string : 'T -> string

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

--------------------

type string = String

Full name: Microsoft.FSharp.Core.string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
YahooPrice.day: string
YahooPrice.openPrice: float
Multiple items
val float : 'T -> float (requires member op_Explicit)

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

--------------------

type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>

  type: float<'Measure>
  implements: IComparable
  implements: IConvertible
  implements: IFormattable
  implements: IComparable<float<'Measure>>
  implements: IEquatable<float<'Measure>>
  inherits: ValueType


--------------------

type float = Double

Full name: Microsoft.FSharp.Core.float

  type: float
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<float>
  implements: IEquatable<float>
  inherits: ValueType
YahooPrice.closePrice: float
YahooPrice.highPrice: float
YahooPrice.lowPrice: float
YahooPrice.adjustedClosePrice: float
YahooPrice.volume: float
type YahooRequest =
  {day: string;
   ticker: string;}

Full name: Yahoo.YahooRequest

  type: YahooRequest
  implements: IEquatable<YahooRequest>
  implements: Collections.IStructuralEquatable
  implements: IComparable<YahooRequest>
  implements: IComparable
  implements: Collections.IStructuralComparable
YahooRequest.day: string
YahooRequest.ticker: string
exception BadResults of string * string

Full name: Yahoo.BadResults
val historicalQuote : YahooRequest -> YahooPrice

Full name: Yahoo.historicalQuote
val r : YahooRequest

  type: YahooRequest
  implements: IEquatable<YahooRequest>
  implements: Collections.IStructuralEquatable
  implements: IComparable<YahooRequest>
  implements: IComparable
  implements: Collections.IStructuralComparable
val date : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val symbol : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val oops : (unit -> 'a)
val raise : Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
val pad : (int -> string)
val i : int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
Multiple items
val int : 'T -> int (requires member op_Explicit)

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

--------------------

type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>

  type: int<'Measure>
  implements: IComparable
  implements: IConvertible
  implements: IFormattable
  implements: IComparable<int<'Measure>>
  implements: IEquatable<int<'Measure>>
  inherits: ValueType


--------------------

type int = int32

Full name: Microsoft.FSharp.Core.int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
val s : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
Multiple overloads
Object.ToString() : string
Int32.ToString(provider: IFormatProvider) : string
Int32.ToString(format: string) : string
Int32.ToString(format: string, provider: IFormatProvider) : string
property String.Length: int
val yr : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
Multiple overloads
String.Substring(startIndex: int) : string
String.Substring(startIndex: int, length: int) : string
val mth : int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
type Int32 =
  struct
    member CompareTo : obj -> int
    member CompareTo : int -> int
    member Equals : obj -> bool
    member Equals : int -> bool
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> System.TypeCode
    member ToString : unit -> string
    member ToString : string -> string
    member ToString : System.IFormatProvider -> string
    member ToString : string * System.IFormatProvider -> string
    static val MaxValue : int
    static val MinValue : int
    static member Parse : string -> int
    static member Parse : string * System.Globalization.NumberStyles -> int
    static member Parse : string * System.IFormatProvider -> int
    static member Parse : string * System.Globalization.NumberStyles * System.IFormatProvider -> int
    static member TryParse : string * int -> bool
    static member TryParse : string * System.Globalization.NumberStyles * System.IFormatProvider * int -> bool
  end

Full name: System.Int32

  type: Int32
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
Multiple overloads
Int32.Parse(s: string) : int
Int32.Parse(s: string, provider: IFormatProvider) : int
Int32.Parse(s: string, style: Globalization.NumberStyles) : int
Int32.Parse(s: string, style: Globalization.NumberStyles, provider: IFormatProvider) : int
val day : int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
val u : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val request : HttpWebRequest

  type: HttpWebRequest
  implements: ISerializable
  inherits: WebRequest
  inherits: MarshalByRefObject
type HttpWebRequest =
  class
    inherit System.Net.WebRequest
    member Abort : unit -> unit
    member Accept : string with get, set
    member AddRange : int -> unit
    member AddRange : int64 -> unit
    member AddRange : int * int -> unit
    member AddRange : int64 * int64 -> unit
    member AddRange : string * int -> unit
    member AddRange : string * int64 -> unit
    member AddRange : string * int * int -> unit
    member AddRange : string * int64 * int64 -> unit
    member Address : System.Uri
    member AllowAutoRedirect : bool with get, set
    member AllowWriteStreamBuffering : bool with get, set
    member AutomaticDecompression : System.Net.DecompressionMethods with get, set
    member BeginGetRequestStream : System.AsyncCallback * obj -> System.IAsyncResult
    member BeginGetResponse : System.AsyncCallback * obj -> System.IAsyncResult
    member ClientCertificates : System.Security.Cryptography.X509Certificates.X509CertificateCollection with get, set
    member Connection : string with get, set
    member ConnectionGroupName : string with get, set
    member ContentLength : int64 with get, set
    member ContentType : string with get, set
    member ContinueDelegate : System.Net.HttpContinueDelegate with get, set
    member CookieContainer : System.Net.CookieContainer with get, set
    member Credentials : System.Net.ICredentials with get, set
    member Date : System.DateTime with get, set
    member EndGetRequestStream : System.IAsyncResult -> System.IO.Stream
    member EndGetRequestStream : System.IAsyncResult * System.Net.TransportContext -> System.IO.Stream
    member EndGetResponse : System.IAsyncResult -> System.Net.WebResponse
    member Expect : string with get, set
    member GetRequestStream : unit -> System.IO.Stream
    member GetRequestStream : System.Net.TransportContext -> System.IO.Stream
    member GetResponse : unit -> System.Net.WebResponse
    member HaveResponse : bool
    member Headers : System.Net.WebHeaderCollection with get, set
    member Host : string with get, set
    member IfModifiedSince : System.DateTime with get, set
    member KeepAlive : bool with get, set
    member MaximumAutomaticRedirections : int with get, set
    member MaximumResponseHeadersLength : int with get, set
    member MediaType : string with get, set
    member Method : string with get, set
    member Pipelined : bool with get, set
    member PreAuthenticate : bool with get, set
    member ProtocolVersion : System.Version with get, set
    member Proxy : System.Net.IWebProxy with get, set
    member ReadWriteTimeout : int with get, set
    member Referer : string with get, set
    member RequestUri : System.Uri
    member SendChunked : bool with get, set
    member ServicePoint : System.Net.ServicePoint
    member Timeout : int with get, set
    member TransferEncoding : string with get, set
    member UnsafeAuthenticatedConnectionSharing : bool with get, set
    member UseDefaultCredentials : bool with get, set
    member UserAgent : string with get, set
    static member DefaultCachePolicy : System.Net.Cache.RequestCachePolicy with get, set
    static member DefaultMaximumErrorResponseLength : int with get, set
    static member DefaultMaximumResponseHeadersLength : int with get, set
  end

Full name: System.Net.HttpWebRequest

  type: HttpWebRequest
  implements: ISerializable
  inherits: WebRequest
  inherits: MarshalByRefObject
type WebRequest =
  class
    inherit System.MarshalByRefObject
    member Abort : unit -> unit
    member AuthenticationLevel : System.Net.Security.AuthenticationLevel with get, set
    member BeginGetRequestStream : System.AsyncCallback * obj -> System.IAsyncResult
    member BeginGetResponse : System.AsyncCallback * obj -> System.IAsyncResult
    member CachePolicy : System.Net.Cache.RequestCachePolicy with get, set
    member ConnectionGroupName : string with get, set
    member ContentLength : int64 with get, set
    member ContentType : string with get, set
    member Credentials : System.Net.ICredentials with get, set
    member EndGetRequestStream : System.IAsyncResult -> System.IO.Stream
    member EndGetResponse : System.IAsyncResult -> System.Net.WebResponse
    member GetRequestStream : unit -> System.IO.Stream
    member GetResponse : unit -> System.Net.WebResponse
    member Headers : System.Net.WebHeaderCollection with get, set
    member ImpersonationLevel : System.Security.Principal.TokenImpersonationLevel with get, set
    member Method : string with get, set
    member PreAuthenticate : bool with get, set
    member Proxy : System.Net.IWebProxy with get, set
    member RequestUri : System.Uri
    member Timeout : int with get, set
    member UseDefaultCredentials : bool with get, set
    static member Create : string -> System.Net.WebRequest
    static member Create : System.Uri -> System.Net.WebRequest
    static member CreateDefault : System.Uri -> System.Net.WebRequest
    static member DefaultCachePolicy : System.Net.Cache.RequestCachePolicy with get, set
    static member DefaultWebProxy : System.Net.IWebProxy with get, set
    static member GetSystemWebProxy : unit -> System.Net.IWebProxy
    static member RegisterPrefix : string * System.Net.IWebRequestCreate -> bool
  end

Full name: System.Net.WebRequest

  type: WebRequest
  implements: ISerializable
  inherits: MarshalByRefObject
Multiple overloads
WebRequest.Create(requestUri: Uri) : WebRequest
WebRequest.Create(requestUriString: string) : WebRequest
property WebRequest.Method: string
property WebRequest.ContentType: string
val resultsArr : string []

  type: string []
  implements: ICloneable
  implements: Collections.IList
  implements: Collections.ICollection
  implements: Collections.IStructuralComparable
  implements: Collections.IStructuralEquatable
  implements: Collections.Generic.IList<string>
  implements: Collections.Generic.ICollection<string>
  implements: seq<string>
  implements: Collections.IEnumerable
  inherits: Array
val response : WebResponse

  type: WebResponse
  implements: ISerializable
  implements: IDisposable
  inherits: MarshalByRefObject
WebRequest.GetResponse() : WebResponse
val result : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val reader : StreamReader

  type: StreamReader
  implements: IDisposable
  inherits: TextReader
  inherits: MarshalByRefObject
type StreamReader =
  class
    inherit System.IO.TextReader
    new : System.IO.Stream -> System.IO.StreamReader
    new : System.IO.Stream * bool -> System.IO.StreamReader
    new : System.IO.Stream * System.Text.Encoding -> System.IO.StreamReader
    new : System.IO.Stream * System.Text.Encoding * bool -> System.IO.StreamReader
    new : System.IO.Stream * System.Text.Encoding * bool * int -> System.IO.StreamReader
    new : string -> System.IO.StreamReader
    new : string * bool -> System.IO.StreamReader
    new : string * System.Text.Encoding -> System.IO.StreamReader
    new : string * System.Text.Encoding * bool -> System.IO.StreamReader
    new : string * System.Text.Encoding * bool * int -> System.IO.StreamReader
    member BaseStream : System.IO.Stream
    member Close : unit -> unit
    member CurrentEncoding : System.Text.Encoding
    member DiscardBufferedData : unit -> unit
    member EndOfStream : bool
    member Peek : unit -> int
    member Read : unit -> int
    member Read : char [] * int * int -> int
    member ReadLine : unit -> string
    member ReadToEnd : unit -> string
    static val Null : System.IO.StreamReader
  end

Full name: System.IO.StreamReader

  type: StreamReader
  implements: IDisposable
  inherits: TextReader
  inherits: MarshalByRefObject
WebResponse.GetResponseStream() : Stream
TextReader.ReadToEnd() : string
WebResponse.Close() : unit
Multiple overloads
String.Split(separator: char []) : string []
String.Split(separator: string [], options: StringSplitOptions) : string []
String.Split(separator: char [], options: StringSplitOptions) : string []
String.Split(separator: char [], count: int) : string []
String.Split(separator: string [], count: int, options: StringSplitOptions) : string []
String.Split(separator: char [], count: int, options: StringSplitOptions) : string []
type Double =
  struct
    member CompareTo : obj -> int
    member CompareTo : float -> int
    member Equals : obj -> bool
    member Equals : float -> bool
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> System.TypeCode
    member ToString : unit -> string
    member ToString : string -> string
    member ToString : System.IFormatProvider -> string
    member ToString : string * System.IFormatProvider -> string
    static val MinValue : float
    static val MaxValue : float
    static val Epsilon : float
    static val NegativeInfinity : float
    static val PositiveInfinity : float
    static val NaN : float
    static member IsInfinity : float -> bool
    static member IsNaN : float -> bool
    static member IsNegativeInfinity : float -> bool
    static member IsPositiveInfinity : float -> bool
    static member Parse : string -> float
    static member Parse : string * System.Globalization.NumberStyles -> float
    static member Parse : string * System.IFormatProvider -> float
    static member Parse : string * System.Globalization.NumberStyles * System.IFormatProvider -> float
    static member TryParse : string * float -> bool
    static member TryParse : string * System.Globalization.NumberStyles * System.IFormatProvider * float -> bool
  end

Full name: System.Double

  type: Double
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<float>
  implements: IEquatable<float>
  inherits: ValueType
Multiple overloads
Double.Parse(s: string) : float
Double.Parse(s: string, provider: IFormatProvider) : float
Double.Parse(s: string, style: Globalization.NumberStyles) : float
Double.Parse(s: string, style: Globalization.NumberStyles, provider: IFormatProvider) : float
val valu : YahooPrice

Full name: Yahoo.valu

  type: YahooPrice
  implements: IEquatable<YahooPrice>
  implements: Collections.IStructuralEquatable
  implements: IComparable<YahooPrice>
  implements: IComparable
  implements: Collections.IStructuralComparable

More information

Link: http://fssnip.net/3s
Posted: 3 years ago
Author: akaPhenom
Tags: Histocical Stock Quote, Finance, Web