使用 Quandl google 金融数据集代码标签将 Quandl 数据下载到 pandas
Downloading Quandl data to pandas using Quandl google finance dataset code tags
我想专门使用 Quandl 的 Google 金融数据库来下载股票价格以回测策略。原因是 google finance 与 Quandl 的 WIKI 和 Yahoo 数据库相比,针对拆分调整后的股票等拥有干净的数据。如此处所示,最后一个 link 将显示调整后的股票拆分:
https://www.quandl.com/WIKI/AAPL-Apple-Inc-AAPL
https://www.quandl.com/YAHOO/AAPL-AAPL-Apple-Inc
https://www.quandl.com/GOOG/NASDAQ_AAPL-Apple-Inc-AAPL
但是 Quandl 的 google 数据库标签的形式是 GOOG/NYSE_IBM 或 GOOG/NASDAQ_AAPL,这与 WIKI/IBM、[=35= 等标签不同].
由于手动为这些交易所上市的股票数量添加纽约证券交易所或纳斯达克标签是不可行的,是否有一种有效的方法可以从 Quandl 下载股票数据,给定 csv 或 pandas数据帧?
这是我的代码 FWIW:
nyseList = pd.read_csv('dowjonesIA.csv') # read csv
masterList = pd.DataFrame(nyseList.Ticker) # save symbols only into another df
for index, rows in masterList.iterrows():
ticker = masterList.loc[index] # this will not work for passing element
stock = Quandl.get(ticker, trim_start="2000-01-01", trim_end="2015-01-01")
#stock = Quandl.get("GOOG/NASDAQ_AAPL", trim_start="2000-01-01", trim_end="2015-01-01") #this is the actual format that works
# lags data for signal
stock['diff'] = (stock.Open - stock.Close.shift(1))/stock.Close.shift(1)
lowerBound = -0.08
upperBound = 0.08
#generate signal based on 8% rule
stock['signal'] = np.where(stock['diff'] >= upperBound, 1.0, np.where (stock['diff'] <= lowerBound, -1.0, 0.0))
initialCapital = 100000.0
accountLimit = 0.05
#calculate size based on account risk and price
stock['position'] = (stock.signal*initialCapital*accountLimit)/stock.Open
#shows if there is a position open
stock['open trade'] = np.where(stock['position'] > 0, 1.0, np.where(stock['position'] < 0, -1.0, 0.0))
#determine profit/loss
stock['pnl'] = (stock.position*stock.Close) - (stock.position*stock.Open)
#sums up results to starting acct capital
stock['equity curve'] = initialCapital + stock.pnl.cumsum()
print(stock.head(20)) # is dataframe
# plots test results
stock['equity curve'].plot()
plt.show()
我曾尝试使用 pandas 内置远程数据访问,但在将字符串作为 args 的股票代码传递时也会出现问题。此外,任何以矢量化方式执行循环的建议都值得赞赏,而不是迭代地执行,并且适用于一般逻辑流程。提前致谢。
没关系,我只是将标签作为字符串附加到股票代码字符串中。此格式有效:
masterList = pd.Dataframe('GOOG/NYSE_' + nyseList['Ticker'].astype(str))
归功于此线程:Append string to the start of each value in a said column of a pandas dataframe (elegantly)
我想专门使用 Quandl 的 Google 金融数据库来下载股票价格以回测策略。原因是 google finance 与 Quandl 的 WIKI 和 Yahoo 数据库相比,针对拆分调整后的股票等拥有干净的数据。如此处所示,最后一个 link 将显示调整后的股票拆分:
https://www.quandl.com/WIKI/AAPL-Apple-Inc-AAPL
https://www.quandl.com/YAHOO/AAPL-AAPL-Apple-Inc
https://www.quandl.com/GOOG/NASDAQ_AAPL-Apple-Inc-AAPL
但是 Quandl 的 google 数据库标签的形式是 GOOG/NYSE_IBM 或 GOOG/NASDAQ_AAPL,这与 WIKI/IBM、[=35= 等标签不同].
由于手动为这些交易所上市的股票数量添加纽约证券交易所或纳斯达克标签是不可行的,是否有一种有效的方法可以从 Quandl 下载股票数据,给定 csv 或 pandas数据帧?
这是我的代码 FWIW:
nyseList = pd.read_csv('dowjonesIA.csv') # read csv
masterList = pd.DataFrame(nyseList.Ticker) # save symbols only into another df
for index, rows in masterList.iterrows():
ticker = masterList.loc[index] # this will not work for passing element
stock = Quandl.get(ticker, trim_start="2000-01-01", trim_end="2015-01-01")
#stock = Quandl.get("GOOG/NASDAQ_AAPL", trim_start="2000-01-01", trim_end="2015-01-01") #this is the actual format that works
# lags data for signal
stock['diff'] = (stock.Open - stock.Close.shift(1))/stock.Close.shift(1)
lowerBound = -0.08
upperBound = 0.08
#generate signal based on 8% rule
stock['signal'] = np.where(stock['diff'] >= upperBound, 1.0, np.where (stock['diff'] <= lowerBound, -1.0, 0.0))
initialCapital = 100000.0
accountLimit = 0.05
#calculate size based on account risk and price
stock['position'] = (stock.signal*initialCapital*accountLimit)/stock.Open
#shows if there is a position open
stock['open trade'] = np.where(stock['position'] > 0, 1.0, np.where(stock['position'] < 0, -1.0, 0.0))
#determine profit/loss
stock['pnl'] = (stock.position*stock.Close) - (stock.position*stock.Open)
#sums up results to starting acct capital
stock['equity curve'] = initialCapital + stock.pnl.cumsum()
print(stock.head(20)) # is dataframe
# plots test results
stock['equity curve'].plot()
plt.show()
我曾尝试使用 pandas 内置远程数据访问,但在将字符串作为 args 的股票代码传递时也会出现问题。此外,任何以矢量化方式执行循环的建议都值得赞赏,而不是迭代地执行,并且适用于一般逻辑流程。提前致谢。
没关系,我只是将标签作为字符串附加到股票代码字符串中。此格式有效:
masterList = pd.Dataframe('GOOG/NYSE_' + nyseList['Ticker'].astype(str))
归功于此线程:Append string to the start of each value in a said column of a pandas dataframe (elegantly)