2010-01-09 4 views
7

Yahoo를 사용하여 Emacs Lisp 프로그램 내에서 주가를 얻고 싶습니다. 두 가지 질문이 있습니다.Yahoo에서 Elisp로 주가를 얻고 싶습니까?

  1. http를 어떻게 만들 수 있습니까?
  2. 데이터를 비교할 수 있도록 Elisp에 데이터를 저장할 가장 좋은 점은 무엇입니까? 즉, 해시 테이블, 여러 해시 테이블 또는 목록을 사용하여 야후에서 반환 한 데이터를 나타내야합니까?

다음은 내가하고 싶은 기본적인 개요입니다.

 
;; 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 
;; 

+0

'mapconcat'을 소개해 주셔서 감사합니다. – justinhj

답변

6

다음은 몇 가지 코드입니다. 버퍼로 url을 가져 오는 방법을 보여주고 각 줄을 구문 분석 한 다음 각 항목의 티커와 가격을 표시합니다. 거기에서 수정하여 필요한 것을 할 수 있습니다.

이렇게하면 주식 데이터의 각 행이 목록으로 분석되며, 첫 번째, 두 번째, 세 번째 기능을 사용하거나 nth를 사용하여 값을 가져올 수 있습니다. 방금 반환 할 get-ticker (첫 번째 시세)와 같은 원하는 각 요소를 가져올 함수를 작성할 수 있습니다.

어떤 종류의 데이터 구조를 사용할 지 생각하지 않습니다. 가장 쉬운 것은 무엇이든 괜찮습니다. 고성능이 필요하다면 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))) 
2

체크 아웃 http://edward.oconnor.cx/elisp/. 에드워드 (Edward)는 HTTP를 사용하여 다양한 서비스와 상호 작용하는 몇 가지 예를 다루었으며, 야후 클라이언트 라이브러리를 찾을 수 없다면이 기술을 사용하여 작성할 수 있습니다.

관련 문제