7 elisp
我想用雅虎从Emacs Lisp程序中获取股票价格.我有两个问题.
这是我想做的基本概要.
;; 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
;;
这里有一些代码可以帮助您入门; 我将展示如何将URL抓取到缓冲区,解析每一行,然后显示每个项目的股票代码和价格.你可以从那里修改它来做你需要的.
这会将每行库存数据解析为一个列表,并且可以直接使用第一,第二,第三函数或使用第n个函数来获取值.您可以编写函数来获取所需的每个元素,例如get-ticker(quote),它将返回(第一个自动收报机)
我不会想过要使用什么样的数据结构; 什么是最简单的是好的.如果你需要高性能,那么你不应该为此使用emacs lisp.
(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)))
Run Code Online (Sandbox Code Playgroud)