gpt4 book ai didi

elisp - 使用 Elisp 从雅虎获取股票价格?

转载 作者:行者123 更新时间:2023-12-04 11:45:33 28 4
gpt4 key购买 nike

我想使用雅虎从 Emacs Lisp 程序中获取股票价格。我有两个问题。

  • 我如何制作http GET?
  • 在 Elisp 中存储数据的最佳方法是什么,以便我可以对数据进行比较?换句话说,我应该使用一个哈希表、多个哈希表还是列表来表示从雅虎返回的数据?

  • 这是我想做的事情的基本轮廓。

    ;;调用雅虎获取股票价格
    ;;
    ;;雅虎输入:
    ;; http://download.finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG&f=sb2b3jkm6
    ;;雅虎输出:
    ;; “苹果”,211.98,211.82,78.20,215.59,+17.90%
    ;; “GOOG”,602.94,601.69,282.75,629.51,+18.27%
    ;;
    ;;交易品种、卖价、买价、52 周低点、52 周高点、200 天平均线变化百分比
    ;;
    ;;此处描述的 Yahoo 格式:http://www.gummy-stuff.org/Yahoo-data.htm

    (defun get-price-url (tickers)

    s = 符号
    b2 = 实时询问
    b3 = 实时出价
    j = 52 周低点
    k = 52 周高点


    (连接“http://download.finance.yahoo.com/d/quotes.csv?s="
    (mapconcat '身份代码 "+") "&f=sb2b3jk"))

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

    ;;使用 Url 调用 Yahoo,处理结果并放入数据结构中
    ;;

    ;;返回结果按 200 天平均变化最大的降序排序
    ;;

    最佳答案

    这里有一些代码可以帮助您入门;我展示了如何将 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)))

    关于elisp - 使用 Elisp 从雅虎获取股票价格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2032503/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com