Hello all,
I have a custom data file that looks like:
Ticker,Sector
A,XLV
AA,XLB
AAC,XLV
...
Basically trying to add Sector data to my data set for underlying stocks.
The following code, however, does not seem to pull in the data. I get "no Sectors data" in the log file.
Please take a look:
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect.Python import PythonData
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018,5,25) #Set Start Date
self.SetEndDate(2018,5,29) #Set End Date
self.SetCash(20000) #Set Strategy Cash
self.AddEquity("SPY", Resolution.Second)
# add the custom set of sector data from an outside file
self.AddData(SectorData, "SECTORS", Resolution.Daily)
# we know the file is available, because the below works
#file = self.Download("https://www.dropbox.com/sh/vf8dp7snigw7j9x/AADniLYgjeV0GTamCI3DTCb9a/Sector_Lookup.csv?dl=1")
#self.Debug(file)
# get the universe of stocks every morning
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
# Universe selection - coarse
def CoarseSelectionFunction(self, coarse):
# price > 5 and volume > 500k
selected = [x for x in coarse if (float(x.Price) >= 5 and x.Volume > 500000) ]
return [ x.Symbol for x in selected ]
# Universe selection - fine
def FineSelectionFunction(self, fine):
self.Debug("starting fine selection")
# check that "fine" stocks are available in downloaded Ticker/Sector combinations
return [ x.Symbol for x in fine ]
# intraday trading
def OnData(self, data):
if data.ContainsKey("SECTORS"):
self.Debug("SECTORS has been added to data")
else:
self.Debug("no SECTORS data")
if not self.Portfolio.Invested:
self.SetHoldings("SPY", 1)
# Custom data download description
class SectorData(PythonData):
def GetSource(self, config, date, isLiveMode):
url = "https://www.dropbox.com/sh/vf8dp7snigw7j9x/AADniLYgjeV0GTamCI3DTCb9a/Sector_Lookup.csv?dl=1"
return SubscriptionDataSource(url, SubscriptionTransportMedium.RemoteFile)
def Reader(self, config, line, date, isLiveMode):
sectors = SectorData()
sectors.Symbol = config.Symbol
data = line.split(',')
sectors["Ticker"] = data[0]
sectors["Sector"] = data[1]
return sectors
Ted Murphy
I seem to be able to load the sector data into a dictionary in Initialize():
# upload the sector info file = self.Download("https://www.dropbox.com/sh/vf8dp7snigw7j9x/AADniLYgjeV0GTamCI3DTCb9a/Sector_Lookup.csv?dl=1") self.SectorData = dict(item.split(",") for item in file.splitlines())
Then access the sector data down below via this:
self.Debug("sector for AAPL is: " + self.SectorData["AAPL"])
Ted Murphy
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!