I tried to write a statement where if an order had been placed for a certain security, then the buy condition is ‘voided’ by not having a value until the next day, but I am running into issues.
def OnData(self, slice):
if not all([data.IsReady for data in self.symbolData.values()]):
return
for symbol, data in self.symbolData.items():
price = self.Securities[symbol].Price
if price >= data.entryPricesLong:
marketorderticket = self.SetHoldings(symbol, 0.1)
stopmarketorderticket = self.StopMarketOrder(symbol, -1.0, price * self.stopLossPercentLong)
limitorderticket = self.LimitOrder(symbol, -1.0, price * self.takeProfitPercentLong)
if price <= data.entryPricesShort:
marketorderticketshort = self.SetHoldings(symbol, -0.1)
stopmarketorderticketshort = self.StopMarketOrder(symbol, -1.0, price * self.stopLossPercentShort)
limitorderticketshort =self.LimitOrder(symbol, -1.0, price * self.takeProfitPercentShort)
if marketorderticket.time < (self.time - datetime.timedelta(hours = 8)):
data.entrypricesLong = None
if marketordertickershort.time < (self.time - datetime.timedelta(hours = 8)):
data.entrypricesShort = None
Fred Painchaud
Hi Victoria,
so you were most probably getting an AttributeError.
BUT
2. If you “Nonify” data.entryPricesLong or data.entryPricesShort, your comparisons:
will then throw None type errors because you cannot compare to None.
SO
The best way is to add two boolean flags in your SymbolData class to determine if it is time to trade or not (one for Long, one for Short). And then you manage their state in the code. They start being True. And then you can do something like this:
Of course, at the end of the day, or something like that, you then need to put the flags back to True so you can trade again. This can be done inside OnEndOfDay, for instance.
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!