I have a small manual universe and I would like my code to run for any of the securities within it, but my for loop is not working. Help please.
for security in self.ActiveSecurities:
if self.maxPrice1.Current.Value <= entryPrice:
self.SetHoldings(security, 0.2)
self.stopMarketOrder(security, -1.0, stopLossPercent)
self.limitOrder(security, -1.0, takeProfitPercent)
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'int'>) method. Please checkout the API documentation.
at OnData
self.SetHoldings(security in main.py: line 51 (Open Stacktrace)
Fred Painchaud
Hi Victoria,
It's a bit difficult to fully diagnose without the full code. But assuming everything else is ok:
Fred
Victoria Butler
Fred,
Here is the full attached code. I am trying to convert my manual trading strategy, or a version of it, into code. There are still some elements missing, just trying to get the bare bones running. The values for the parameters don't matter much right now. Here is everything I have so far. I implemented your fixes, and made a few of my own, but it wouldn't allow me to attach a backtest.
class DancingYellowFlamingo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 1)
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Hour
tickers = ["AAPL", "MSFT", "TSLA", "NVDA", "DIS", "AMD", "NFLX"]
symbols = [Symbol.Create(t, SecurityType.Equity, Market.USA) for t in tickers]
self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))
for x in symbols:
self.AddSecurity(x)
for x in symbols:
self.maxPrice8 = self.MAX(x, 8)
for x in symbols:
self.minPrice8 = self.MIN(x, 8)
def OnData(self, data):
entryPriceLong = (self.maxPrice8.Current.Value * 1.001)
entryPriceShort = (self.minPrice8.Current.Value * 0.999)
stopLossPercentLong = 0.997
takeProfitTargetLong = 1.0065
stopLossPercentShort = 1.003
takeProfitPercentShort = 0.9935
for security in self.ActiveSecurities:
if self.security.Symbol.Value >= entryPriceLong:
self.SetHoldings(security.Symbol, 0.1)
self.stopMarketOrder(security.Symbol, -1.0, stopLossPercent)
self.limitOrder(security.Symbol, -1.0, takeProfitPercent)
elif self.security.Symbol.Value <= entryPriceShort:
self.SetHoldings(security.Symbol, -0.1)
self.stopMarketOrder(security.Symbol, -1.0, stopLossPercent)
self.limitOrder(security.Symbol, -1.0, takeProfitPercent)
self.Schedule.On(self.DateRules.EveryDay(security.Symbol), self.TimeRules.BeforeMarketClose(security.Symbol, 5), self.EveryDayAfterMarketClose(self.LiquidateHoldings))
Victoria Butler
https://www.quantconnect.com/u/fred-painchaud
Fred Painchaud
Hi Victoria,
Here is a modified version of your code with comments:
So, again, I am not saying it is sexy code or the way to go. Not even that it does what you want (which is unspecified - you are implementing your manual technique). But it should run (I did not write it in an IDE but from eyeballing, it looks ok).
Continue to persevere.
Fred
Victoria Butler
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!