Last active
January 3, 2016 01:29
-
-
Save rgantt/8389560 to your computer and use it in GitHub Desktop.
Grabs the stock price (from the Yahoo! finance API) of all passed symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open Core.Std | |
| open Yfinance_j | |
| type json = string | |
| type symbol = string | |
| type price = float | |
| let url symbols = | |
| "http://finance.yahoo.com/webservice/v1/symbols/" | |
| ^ (String.concat ~sep:"," symbols) | |
| ^ "/quote?format=json" | |
| let stock_data symbols = | |
| let open Http_client.Convenience in | |
| http_get (url symbols) | |
| let quotes_of_response json = | |
| let response = Yfinance_j.response_of_string json in | |
| let quotes = List.map ~f:(fun request -> request.resource.fields) response.list.resources in | |
| List.map ~f:(fun quote -> (quote.symbol, quote.price)) quotes | |
| let of_string symbol = symbol | |
| let to_string symbol = symbol | |
| let of_float price = price | |
| let to_float price = price |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* abstract types *) | |
| type json | |
| type symbol | |
| type price | |
| (* finance interface *) | |
| val url : symbol list -> json | |
| val stock_data : symbol list -> json | |
| val quotes_of_response : json -> (symbol * price) list | |
| (* helpers to translate from abstract to concrete types *) | |
| val of_string : string -> symbol | |
| val to_string : symbol -> string | |
| val of_float : float -> price | |
| val to_float : price -> float |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| all: atdgen stock_price | |
| stock_price: | |
| corebuild -pkg core_extended,netclient,yojson,atdgen stock_price.native | |
| atdgen: | |
| atdgen -j yfinance.atd | |
| atdgen -t yfinance.atd | |
| clean: | |
| rm -f *.native | |
| rm -rf _build/ | |
| rm yfinance_*.ml* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open Core.Std | |
| open Finance | |
| type symbol = Finance.symbol | |
| type price = Finance.price | |
| type quote = symbol * price | |
| let quotes symbols = | |
| let open Finance in | |
| quotes_of_response (stock_data symbols) | |
| let rec display = function | |
| | [] -> () | |
| | (symbol, price) :: tl -> | |
| printf "%s: %0.2f\n" (Finance.to_string symbol) (Finance.to_float price); | |
| display tl | |
| exception Symbol_required of string | |
| let remove_first = function | |
| | [] | [_] -> raise (Symbol_required "You must provide at least one element") | |
| | _ :: tl -> tl | |
| let () = | |
| let symbols = remove_first (Array.to_list Sys.argv) in | |
| display (quotes (List.map ~f:Finance.of_string symbols)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* abstract types *) | |
| type symbol | |
| type price | |
| type quote | |
| (* leaky list interface *) | |
| val remove_first : 'a list -> 'a list | |
| (* stock prices interface *) | |
| val quotes : symbol list -> quote list | |
| (* Output functions *) | |
| val display : quote list -> unit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type response_class = [ | |
| Resource_list <json name="resource-list"> | |
| ] | |
| type meta = { | |
| response_type <json name="type"> : response_class; | |
| start : int; | |
| count : int; | |
| } | |
| type asset_class = [ | |
| Equity <json name="equity"> | |
| | Money_market <json name="moneymarket"> | |
| | Mutual_fund <json name="mututalfund"> | |
| ] | |
| type quote = { | |
| name : string; | |
| price : float; | |
| symbol : string; | |
| ts : int; | |
| asset_type <json name="type"> : asset_class; | |
| utctime : string; | |
| volume : int; | |
| } | |
| type resource = { | |
| classname : string; | |
| fields : quote; | |
| } | |
| type result = { | |
| resource : resource; | |
| } | |
| type results = { | |
| meta : meta; | |
| resources : result list; | |
| } | |
| type response = { | |
| list : results; | |
| } |
Author
Author
Factored out an interface
Author
Factored out a finance interface/implementation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rely on ATDGen to generate the schema for the Yahoo! finance JSON types