Getting stock prices from Yahoo with Elisp?

I would like to use Yahoo to get stock prices through the Emacs Lisp program. I have two questions.

  • How to do http get?
  • What is the best way to store data in Elisp so that I can compare data? In other words, should you use a single hash table, multiple hash tables, or lists to represent the data returned from Yahoo?

Here is a basic plan of what I would like to do.

 ;;  Call Yahoo to get equity prices
 ;;
 ;;  Yahoo Input: 
 ;;  http://download.finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG&f=sb2b3jkm6
 ;;  Yahoo Output:
 ;;  "AAPL", 211.98,211.82,78.20,215.59, + 17.90%
 ;;  "GOOG", 602.94,601.69,282.75,629.51, + 18.27%
 ;;
 ;;  Symbol, ask, bid, 52 week low, 52 week high,% change from 200 day mavg
 ;;
 ;;  Yahoo format described here: http://www.gummy-stuff.org/Yahoo-data.htm

 (defun get-price-url (tickers)
 "
 s = symbol
 b2 = ask real-time
 b3 = bid real-time
 j = 52 week low
 k = 52 week high
 "

   (concat "http://download.finance.yahoo.com/d/quotes.csv?s="
       (mapconcat 'identity tickers "+") "& f = sb2b3jk"))


 (setq lst '("AAPL" "GOOG" "MSFT" "ORCL"))
 (setq url (get-price-url lst))

 ;;  Call Yahoo with Url, process results and place in a data structure
 ;; 

 ;;  Return results sorted by largest change in 200 day mavg, in descending order
 ;;

+7
elisp
source share
2 answers

Here is the code to get you started; I will show how to grab the buffer URL, parse each line, and then display the ticker and price of each item. You can change it from there to do what you need.

This parses each row of stock data in the list, and it directly intercepts the values ​​using the first, second, third functions or using the nth. You can write functions to capture every element you want, for example, get-ticker (quote), which will just return (first ticker)

I would not think which data structure to use; all that is easiest is beautiful. If you need high performance, you should not use emacs lisp for this anyway.

(defun test() (interactive) (let ((quotes (get-quotes '("AAPL" "GOOG" "MSFT" "ORCL" "ERTS" "THQI") "sb"))) (show-quotes quotes))) (defun show-quotes(quotes) (dolist (quote quotes) (message (format "%s $%.2f" (first quote) (string-to-number (second quote)))))) (defun get-quotes(tickers field-string) "Given a list of ticker names and a string of fields to return as above, this grabs them from Yahoo, and parses them" (let ((results-buffer (get-yahoo-quotes-to-buffer (get-price-url tickers field-string)))) (switch-to-buffer results-buffer) (parse-quote-buffer results-buffer))) (defun get-price-url (tickers field-string) "Set up the get url" (concat "http://download.finance.yahoo.com/d/quotes.csv?s=" (mapconcat 'identity tickers "+") "&f=" field-string)) (defun get-yahoo-quotes-to-buffer(url) "Retrieve the quotes to a buffer and return it" (url-retrieve-synchronously url)) (defun parse-quote-buffer(b) "Parse the buffer for quotes" (goto-line 1) (re-search-forward "^\n") (beginning-of-line) (let ((res nil)) (while (> (point-max) (point)) (setf res (cons (split-string (thing-at-point 'line) ",") res)) (forward-line 1)) (reverse res))) 
+6
source share

Check out http://edward.oconnor.cx/elisp/ . Edward has several examples of interacting with various services using HTTP, and if you cannot find the Yahoo client library, you can write it using these methods.

+2
source share

All Articles