首页 > 解决方案 > 如何从 Alpha Vantage API 中提取数据?

问题描述

我想对股票发出 RSI 警报。

这是 Alpha Vantage API 响应我的请求的数据。每当我运行代码时,我都想提取最新的 RSI。

在这种情况下,它是 77.0835。随着日期和时间(即 2020-01-24 14:30)不断变化,有什么办法可以获得最新的 RSI 吗?

来自 Alpha Vantage 的响应摘录:

{
    "Meta Data": {
        "1: Symbol": "tlt",
        "2: Indicator": "Relative Strength Index (RSI)",
        "3: Last Refreshed": "2020-01-24 14:30:46",
        "4: Interval": "60min",
        "5: Time Period": 14,
        "6: Series Type": "close",
        "7: Time Zone": "US/Eastern Time"
    },
    "Technical Analysis: RSI": {
        "2020-01-24 14:30": {
            "RSI": "77.0835"
        },
        "2020-01-24 13:30": {
            "RSI": "78.0121"
        },
        "2020-01-24 12:30": {
            "RSI": "75.8201"
        },
        "2020-01-24 11:30": {
            "RSI": "75.7447"
        },
        "2020-01-24 10:30": {
            "RSI": "73.9965"
        },
        "2020-01-24 09:30": {
            "RSI": "73.8768"
        }

标签: pythonjsonextract

解决方案


这个怎么样?

data = {
    "Meta Data": {
        "1: Symbol": "tlt",
        "2: Indicator": "Relative Strength Index (RSI)",
        "3: Last Refreshed": "2020-01-24 14:30:46",
        "4: Interval": "60min",
        "5: Time Period": 14,
        "6: Series Type": "close",
        "7: Time Zone": "US/Eastern Time"
    },
    "Technical Analysis: RSI": {
        "2020-01-24 14:30": {
            "RSI": "77.0835"
        },
        "2020-01-24 13:30": {
            "RSI": "78.0121"
        },
        "2020-01-24 12:30": {
            "RSI": "75.8201"
        },
        "2020-01-24 11:30": {
            "RSI": "75.7447"
        },
        "2020-01-24 10:30": {
            "RSI": "73.9965"
        },
        "2020-01-24 09:30": {
            "RSI": "73.8768"
        }
    }
}

most_recent_rsi = data['Technical Analysis: RSI'][sorted(data['Technical Analysis: RSI'])[-1]]

推荐阅读