I posted this question on an old thread, but realized I should probably start a new thread.
Is it possible to add multiple columns from the same Quandl table? e.g.:
MyAlgo(QCAlgorithm):
def Initialize(self):
self.onemo = self.AddData(Quandl1moRate, "USTREASURY/YIELD", Resolution.Daily, DateTimeZone.Utc, True)
self.oneyr = self.AddData(Quandl1yrRate, "USTREASURY/YIELD", Resolution.Daily, DateTimeZone.Utc, True)
class Quandl1moRate(PythonQuandl):
def __init__(self):
self.ValueColumnName = "1 mo"
class Quandl1yrRate(PythonQuandl):
def __init__(self):
self.ValueColumnName = "1 yr"
This doesn't work for me. (I get "Trying to dynamically access a method that does not exist throws a TypeError exception.")
Somewhat related, how do I go about adding Quandl data that comes in monthly increments?
Benjamin Ettinger
OK, I was able to figure out that I shouldn't be assigning a variable to AddData, so I changed my code to:
MyAlgo(QCAlgorithm): def Initialize(self): self.AddData(Quandl1moRate, "USTREASURY/YIELD", Resolution.Daily, DateTimeZone.Utc, True) self.AddData(Quandl1yrRate, "USTREASURY/YIELD", Resolution.Daily, DateTimeZone.Utc, True) class Quandl1moRate(PythonQuandl): def __init__(self): self.ValueColumnName = "1 mo" class Quandl1yrRate(PythonQuandl): def __init__(self): self.ValueColumnName = "1 yr"
In Rebalance, I attempt to access the values with:
data["Quandl1yrRate"].Value
but I get the error:
Runtime Error: In Scheduled Event 'SPY: MonthStart: SPY: 0 min after MarketOpen', KeyNotFoundException : The given key ' ' was not present in the dictionary.
How should I be accessing the two separate data streams for "USTREASURY/YIELD"??
Link Liang
Hi Benjamin,
We could subscribe this Quandl table with one of the column name, and access the other one with
slice["USTREASURY/YIELD"].GetProperty(column2_name)
Here is my attempt with a backtest. Let us know if you have further questions!
Benjamin Ettinger
Thank you! This is what I was looking for.
I attempted to incorporate this into my code, but I'm running into a problem when attempting to access the history thus:
def OnData(self, slice): data = slice["USTREASURY/YIELD"] self.one_year = data.GetProperty("1 yr") hist = self.History(self.one_year,self.start,self.end,Resolution.Daily)
This doesn't work. What should go in the first argument of self.History() to access the history of the "1 yr" property of "USTREASURY/YIELD"? Apologies for the beginner questions.
Link Liang
Hi Benjamin,
The first argument of History() method should be symbols or ticker names. In this case, the ticker name is "USTREASURY/YIELD", as the way Quandl custom data is implemented. Therefore here is how we should access the historical data:
hist = self.History(["USTREASURY/YIELD"],timedelta(days = 10),Resolution.Daily)
And it would return a pandas dataframe. Here is how to access a column of it:
hist["1 mo"]
You could read pandas documentation regarding more usage of dataframe. Hope it helps.
Benjamin Ettinger
Perfect, thank you! I'm familiar with pandas but I didn't realize this output was going into a pandas dataframe.
Benjamin Ettinger
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!