Modified code to asset selection using CoarseSelectionFilter but received “Operation timed out” Error.
Any help higly appreciated,
class CreativeTanFly(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2012, 1, 1) # Set Start Date
self.SetCash(1000000) # Set Strategy Cash
self.AddUniverse(self.CoarseSelectionFilter)
self.UniverseSettings.Resolution = Resolution.Minute
self.UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw
self.symbol_data_by_symbol={}
def CoarseSelectionFilter(self, coarse):
sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
return [x.Symbol for x in sortedByDollarVolume[:10]]
def OnSecuritiesChanged(self, changes):
for security in changes.RemovedSecurities:
symbol = security.Symbol
if security.Invested:
self.Liquidate(symbol)
symbol_data = self.symbol_data_by_symbol.pop(symbol, None)
if symbol_data:
symbol_data.dispose()
for security in changes.AddedSecurities:
symbol = security.Symbol
self.symbol_data_by_symbol[symbol] = SymbolData(self, symbol)
def OnData(self, data):
for symbol, symbol_data in self.symbol_data_by_symbol.items():
if not (data.ContainsKey(symbol) and data[symbol] is not None and symbol_data.openingBar is not None and symbol_data.fhmin is not None):
return
# if current price greater than open at 1% - open long position
if not self.Portfolio[symbol.Value].Invested:
if (self.Time.hour>=12):
if (data[symbol].Close > symbol_data.openingBar.Open*(1+1.6/100) and not symbol_data.wastrade):
self.SetHoldings(symbol, 1/500)
symbol_data.wastrade = True
else:
# close if current price less than 1HMin.
if ((self.Time.hour==10 and self.Time.minute >=30) or
(self.Time.hour>10)):
if (data[symbol.Value].Low < symbol_data.fhmin):
self.Liquidate(symbol.Value)
class SymbolData:
openingBar = None
def __init__(self, algorithm, symbol):
self.algorithm = algorithm
self.symbol = symbol
self.fhmin = None
self.wastrade = False
# Setup minute consolidator
self.five_minute_consolidator = TradeBarConsolidator(timedelta(minutes=5))
self.five_minute_consolidator.DataConsolidated += self.five_minute_consolidation_handler
algorithm.SubscriptionManager.AddConsolidator(symbol, self.five_minute_consolidator)
def five_minute_consolidation_handler(self, sender, bar):
if bar.Time.hour == 9 and bar.Time.minute == 30:
self.openingBar = bar
self.fhmin = bar.Low
self.wastrade = False
else:
if (bar.Time.hour == 9 and bar.Time.minute >30) or (bar.Time.hour==10 and bar.Time.minute <=30):
if (self.fhmin!= None and bar.Low < self.fhmin):
self.fhmin = bar.Low
def dispose(self):
self.algorithm.SubscriptionManager.RemoveConsolidator(self.symbol, self.five_minute_consolidator)
Ilya mart
Why is it “Accessing the securities collection/slice object by string ticker is only available for securities added with the AddSecurity-family methods. For more details, please check out the documentation.”? This is kinda strange, no?
Why QC use another logic for asset selection using filters?
Varad Kabade
Hi Ilya mart,
The above algorithm uses Symbol.Value to as a key to various classes like self.Portfolio to resolve this issue, we recommend using only the Symbol object as Symbol.Value respresents the ticker. We recommend going through the following doc for more information. Refer to the attached backtest.
Best,
Varad Kabade
Ilya mart
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!