使用 yahoo finance 将商品读入 python
reading commodities into python with yahoo finance
我正在使用 yahoo finance 将股票数据读入 python。它适用于股票,但是,它会在商品上出现错误。
下面的代码可以完美运行:
import pandas as pd
import numpy as np
import pandas.io.data as web
import datetime
# We will look at stock prices over the past year, starting at January 1, 2016
start = datetime.datetime(2016,1,1)
end = datetime.date.today()
stock = "AAPL"
# get stock data, from yahoo finance within the dates specified
stock = web.DataReader(stock, "yahoo", start, end)
stock.head(n=3)
但是,如果将句柄更改为商品而不是股票,则会出现此错误:
import pandas as pd
import numpy as np
import pandas.io.data as web
import datetime
# We will look at stock prices over the past year, starting at January 1, 2016
start = datetime.datetime(2016,1,1)
end = datetime.date.today()
stock = "GCG17.CMX"
# get stock data, from yahoo finance within the dates specified
stock = web.DataReader(stock, "yahoo", start, end)
stock.head(n=3)
OSError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.finance.yahoo.com/table.csv?s=GCG17.CMX&a=0&b=1&c=1970&d=0&e=22&f=2017&g=d&ignore=.csv'
谁能帮帮我?
您可以使用YQL Console并尝试使用SQL来提取您想要的数据。
比如我尝试select * from yahoo.finance.historicaldata where symbol = "AAPL" and startDate = "2016-01-01" and endDate = "2017-01-22
,成功得到我想要的table。
如果我用 "GCG17.CMX" 替换 "AAPL",我什么也得不到。因此,我认为python中的库如pandas和yahoo_finance没有任何问题。问题出现在yahoo端。
您可以尝试寻找其他数据库如沃顿数据服务等来获得您想要的table。希望这可以帮助。
您遇到错误,因为雅虎财经不保存历史商品价格。
而是使用著名的 yfinance
。这里是糖期货:
import yfinance as yf
df = yf.download('SB=F', '2020-03-23')
如果您想要黄金期货,只需使用代码 GC=F
即可。如果你想绘制它:
import finplot as fplt
fplt.candlestick_ochl(df[['Open','Close','High','Low']])
fplt.show()
我正在使用 yahoo finance 将股票数据读入 python。它适用于股票,但是,它会在商品上出现错误。
下面的代码可以完美运行:
import pandas as pd
import numpy as np
import pandas.io.data as web
import datetime
# We will look at stock prices over the past year, starting at January 1, 2016
start = datetime.datetime(2016,1,1)
end = datetime.date.today()
stock = "AAPL"
# get stock data, from yahoo finance within the dates specified
stock = web.DataReader(stock, "yahoo", start, end)
stock.head(n=3)
但是,如果将句柄更改为商品而不是股票,则会出现此错误:
import pandas as pd
import numpy as np
import pandas.io.data as web
import datetime
# We will look at stock prices over the past year, starting at January 1, 2016
start = datetime.datetime(2016,1,1)
end = datetime.date.today()
stock = "GCG17.CMX"
# get stock data, from yahoo finance within the dates specified
stock = web.DataReader(stock, "yahoo", start, end)
stock.head(n=3)
OSError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.finance.yahoo.com/table.csv?s=GCG17.CMX&a=0&b=1&c=1970&d=0&e=22&f=2017&g=d&ignore=.csv'
谁能帮帮我?
您可以使用YQL Console并尝试使用SQL来提取您想要的数据。
比如我尝试select * from yahoo.finance.historicaldata where symbol = "AAPL" and startDate = "2016-01-01" and endDate = "2017-01-22
,成功得到我想要的table。
如果我用 "GCG17.CMX" 替换 "AAPL",我什么也得不到。因此,我认为python中的库如pandas和yahoo_finance没有任何问题。问题出现在yahoo端。
您可以尝试寻找其他数据库如沃顿数据服务等来获得您想要的table。希望这可以帮助。
您遇到错误,因为雅虎财经不保存历史商品价格。
而是使用著名的 yfinance
。这里是糖期货:
import yfinance as yf
df = yf.download('SB=F', '2020-03-23')
如果您想要黄金期货,只需使用代码 GC=F
即可。如果你想绘制它:
import finplot as fplt
fplt.candlestick_ochl(df[['Open','Close','High','Low']])
fplt.show()