Yahoo股市API

出自Tan Kian-ting的維基
於 2023年7月8日 (六) 15:38 由 Tankianting討論 | 貢獻 所做的修訂
跳至導覽 跳至搜尋

API 地址

https://query1.finance.yahoo.com/v8/finance/chart/<股票名>?metrics=<價格形式>[,<價格形式>]*&interval=<單位區間>&range=<索取期間>

註:[...]*表可重複0次以上

參數

  • 股票名,格式如:
    • 英文股票代號
    • 「臺灣股票代號」.TW,例0050.TW
  • 價格形式,以下為舉例:
    • Date 日期
    • High 最高價
    • Low 最低價
    • Open 開盤價
    • Close 收盤價
    • Volume 成交量
  • 單位區間:一筆資料的單位,格式為「數字+時間單位」,時間單位如:
    • m:分
    • h:小時
    • d:日
    • wk:週
    • mo:月
  • 索取範圍:資料蒐羅的時間範圍(比如近3月等等),格式如上。

案例

用 curl 索取臺股臺積電(2330)的近3日資料,存到a.txt:

curl "https://query1.finance.yahoo.com/v8/finance/chart/2330.TW?metrics=Date,High,Low,Open,Close,Volume&interval=1d&range=3d" > a.txt

輸出結果:

{"chart":{"result":[{"meta":{"currency":"TWD","symbol":"2330.TW","exchangeName":"TAI","instrumentType":"EQUITY","firstTradeDate":946947600,"regularMarketTime":1688707805,"gmtoffset":28800,"timezone":"CST","exchangeTimezoneName":"Asia/Taipei","regularMarketPrice":565.0,"chartPreviousClose":582.0,"priceHint":2,"currentTradingPeriod":{"pre":{"timezone":"CST","start":1688691600,"end":1688691600,"gmtoffset":28800},"regular":{"timezone":"CST","start":1688691600,"end":1688707800,"gmtoffset":28800},"post":{"timezone":"CST","start":1688707800,"end":1688707800,"gmtoffset":28800}},"dataGranularity":"1d","range":"3d","validRanges":["1d","5d","1mo","3mo","6mo","1y","2y","5y","10y","ytd","max"]},"timestamp":[1688518800,1688605200,1688691600],"indicators":{"quote":[{"low":[579.0,565.0,563.0],"close":[582.0,565.0,565.0],"open":[589.0,573.0,565.0],"high":[589.0,574.0,572.0],"volume":[14913592,28564891,18796190]}],"adjclose":[{"adjclose":[582.0,565.0,565.0]}]}}],"error":null}}

我們需要排整齊,以利閱讀,使用 js-beautify排齊,輸出到a_tidy.txt:

js-beautify a.txt > a_tidy.txt

結果:

{
    "chart": {
        "result": [{
            "meta": {
                "currency": "TWD",
                "symbol": "2330.TW",
                "exchangeName": "TAI",
                "instrumentType": "EQUITY",
                "firstTradeDate": 946947600,
                "regularMarketTime": 1688707805,
                "gmtoffset": 28800,
                "timezone": "CST",
                "exchangeTimezoneName": "Asia/Taipei",
                "regularMarketPrice": 565.0,
                "chartPreviousClose": 582.0,
                "priceHint": 2,
                "currentTradingPeriod": {
                    "pre": {
                        "timezone": "CST",
                        "start": 1688691600,
                        "end": 1688691600,
                        "gmtoffset": 28800
                    },
                    "regular": {
                        "timezone": "CST",
                        "start": 1688691600,
                        "end": 1688707800,
                        "gmtoffset": 28800
                    },
                    "post": {
                        "timezone": "CST",
                        "start": 1688707800,
                        "end": 1688707800,
                        "gmtoffset": 28800
                    }
                },
                "dataGranularity": "1d",
                "range": "3d",
                "validRanges": ["1d", "5d", "1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max"]
            },
            "timestamp": [1688518800, 1688605200, 1688691600],
            "indicators": {
                "quote": [{
                    "low": [579.0, 565.0, 563.0],
                    "close": [582.0, 565.0, 565.0],
                    "open": [589.0, 573.0, 565.0],
                    "high": [589.0, 574.0, 572.0],
                    "volume": [14913592, 28564891, 18796190]
                }],
                "adjclose": [{
                    "adjclose": [582.0, 565.0, 565.0]
                }]
            }
        }],
        "error": null
    }
}