Hi. I have a script that added equities in this form:
self.tickers = ["TLT", "GLD", "VTI"]
for ticker in self.tickers:
self.AddEquity(ticker, Resolution.Daily)
However, now I want to add custom data in this form from the NIFTY example.
gold1 = self.AddData(GoldPhys, "IGLN.L", Resolution.Daily).Symbol
bond1 = self.AddData(Treas20, "IDTL.L", Resolution.Daily).Symbol
spy1 = self.AddData(VanSPY, "VDNR.L", Resolution.Daily).Symbol
OR
self.AddData(GoldPhys, "IGLN.L", Resolution.Daily).Symbol
self.AddData(Treas20, "IDTL.L", Resolution.Daily).Symbol
self.AddData(VanSPY, "VDNR.L", Resolution.Daily).Symbol
With data classes like so:
class GoldPhys(PythonData):
'''IGLN.L Custom Data Class'''
def GetSource(self, config, date, isLiveMode):
return SubscriptionDataSource("https://www.dropbox.com/s/????????/IGLN.csv?dl=1", SubscriptionTransportMedium.RemoteFile)
def Reader(self, config, line, date, isLiveMode):
if not (line.strip() and line[0].isdigit()): return None
# New GoldPhys object
gold = GoldPhys()
gold.Symbol = config.Symbol
try:
# Example File Format:
# Date, Open High Low Close Volume
# 2011-09-13 7792.9 7799.9 7722.65 7748.7 116534670
data = line.split(',')
gold.Time = datetime.strptime(data[0], "%Y-%m-%d")
gold.Value = data[4]
gold["open"] = float(data[1])
gold["high"] = float(data[2])
gold["low"] = float(data[3])
gold["close"] = float(data[4])
except ValueError:
# Do nothing
return None
return gold
My question is how to combine the new data classes into a list in the same manner as the first snippet, where all ETFs become 'ticker in self.tickers' ?
Shile Wen
Hi AM H,
In Python, we can store classes in a list. We can iterate over the classes zipped with the tickers for AddData. I’ve shown how to do this in the attached backtest.
Best,
Shile Wen
Shile Wen
Hi AM H,
Alternatively, if all of the custom data files are in the same format, we can create a single Custom Data class for all of the data, and reuse the class for each ticker. I’ve shown how to do this in the attached backtest.
Best,
Shile Wen
AM H
Thank you Shile! This really cleared things up for me, though I've not been able to get the second version working yet. One error on the version I have up and running looks like:Â
Runtime Error: In Scheduled Event 'QQQ: MonthStart: QQQ: 120 min after MarketOpen', Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception. To prevent the exception, ensure that the VDNR.VDNR 2T key exist in the collection and/or that collection is not empty. KeyError : 'VDNR.VDNR 2T'Â
In this instance I'm swapping out a QC equity (VTI) in some weighting code for my VDNR imported data?
Shile Wen
Hi AM H,
Please attach the code that is giving this error to help us identify the problem. For now, one idea is to make sure we have data for a security by using the .ContainsKey method. This is likely not to be the problem, but our backtest doesn't use the real URLs (we use the ones given earlier), so if they were cloned/copy-pasted, there is no data from the URLs.
Best,
Shile Wen
AM H
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!