gpt4 book ai didi

Python 绘制从 Quandl API 获取的数据的方式错误?

转载 作者:太空宇宙 更新时间:2023-11-03 16:15:37 24 4
gpt4 key购买 nike

我有以下代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# quandl_data.py

from __future__ import print_function

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import requests


def construct_futures_symbols(
symbol, start_year=2010, end_year=2016
):
"""
Constructs a list of futures contract codes
for a particular symbol and timeframe.
"""
futures = []
# March, June, September and
# December delivery codes
months = 'HMUZ'
for y in range(start_year, end_year+1):
for m in months:
futures.append("%s%s%s" % (symbol, m, y))
return futures


def download_contract_from_quandl(contract, dl_dir):
"""
Download an individual futures contract from Quandl and then
store it to disk in the 'dl_dir' directory. An auth_token is
required, which is obtained from the Quandl upon sign-up.
"""
# Construct the API call from the contract and auth_token
api_call = "https://www.quandl.com/api/v3/datasets/"
api_call += "CME/%s.csv" % contract
# If you wish to add an auth token for more downloads, simply
# comment the following line and replace MY_AUTH_TOKEN with
# your auth token in the line below
params = "?sort_order=asc"
params = "?auth_token=myTokenHere&sort_order=asc"
full_url = "%s%s" % (api_call, params)

# Download the data from Quandl
data = requests.get(full_url).text
# Store the data to disk
fc = open('%s/%s.csv' % (dl_dir, contract), 'w')
fc.write(data)
fc.close()


def download_historical_contracts(
symbol, dl_dir, start_year=2010, end_year=2016
):
"""
Downloads all futures contracts for a specified symbol
between a start_year and an end_year.
"""
contracts = construct_futures_symbols(
symbol, start_year, end_year
)
for c in contracts:
print("Downloading contract: %s" % c)
download_contract_from_quandl(c, dl_dir)

if __name__ == "__main__":
symbol = 'ES'

# Make sure you've created this
# relative directory beforehand
dl_dir = 'quandl/futures/ES'

# Create the start and end years
start_year = 2010
end_year = 2016

# Download the contracts into the directory
download_historical_contracts(
symbol, dl_dir, start_year, end_year
)

# Open up a single contract via read_csv
# and plot the settle price
es = pd.io.parsers.read_csv(
"%s/ESH2010.csv" % dl_dir, index_col="Date"
)
es["Settle"].plot()
plt.show()

代码运行没有错误,但绘制方向错误。似乎是从新日期到旧日期进行策划。我想先绘制最旧的数据。

Graph of result

我该如何实现这一目标?我想改变 params = "?sort_order=asc"params = "?sort_order=desc" ,它仅更改 .csv 文件顺序,而不更改绘图。

有什么想法吗?

最佳答案

好的,我已经使用了 API 文档并发现了问题。您需要用来对数据进行排序的参数是:“order=asc|desc”,而不是之前认为的“sort_order”。

请使用此功能:

def download_contract_from_quandl(contract, dl_dir):
"""
Download an individual futures contract from Quandl and then
store it to disk in the 'dl_dir' directory. An auth_token is
required, which is obtained from the Quandl upon sign-up.
"""
# Construct the API call from the contract and auth_token
api_call = "https://www.quandl.com/api/v3/datasets/"
api_call += "CME/%s.csv" % contract
# If you wish to add an auth token for more downloads, simply
# comment the following line and replace MY_AUTH_TOKEN with
# your auth token in the line below
params = "?auth_token=YOUR_TOKEN"
params += "&order=asc"
full_url = "%s%s" % (api_call, params)

# Download the data from Quandl
data = requests.get(full_url).text
# Store the data to disk
fc = open('%s/%s.csv' % (dl_dir, contract), 'w')
fc.write(data)

注意:您使用 API 的方式(通过简单的 http 请求)虽然可行,但并不是使用其 API 的理想方式。有一个名为 Quandl 的 python 包,您可以像这样安装:

pip3 install quandl

在您的系统上。另外,您将有一个(而不是在每个请求中使用 auth_token=YOUR_TOKEN 的多个)身份验证调用,如下所示:

quandl.ApiConfig.api_key = 'YOUR_TOEKN'

然后每个 api 调用都将简单而优雅地使用它们的包或手动创建 http 请求,如下所示:

data = quandl.get("CME/ESH2010.csv", order="asc")

我建议使用第二种使用 API 的方法,但这两种方法都可以完美工作。

干杯,或者。

关于Python 绘制从 Quandl API 获取的数据的方式错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38963711/

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