I'm new to this and keep getting an error that COIN or MSTR have no tradable days despite the dates I set being tradable dates. Does anyone know why I could be getting this error? My ondata function is never even getting called.
from AlgorithmImports import *
class FirstMinuteBreakoutAlgorithm(QCAlgorithm):
def Initialize(self):
self.set_start_date(2024, 7, 15)
self.set_end_date(2024, 7, 15)
self.set_cash(100000)
# self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
self.tickers = ["COIN", "MSTR"] # Predefined list of tickers
self.symbols = [self.AddEquity(ticker, Resolution.Second).Symbol for ticker in self.tickers]
self.first_minute_has_passed = False
self.reached_over_prev_high = {}
self.high_over_prev_high = {}
self.previous_day_highs = {}
self.daily_lows = {}
for symbol in self.symbols:
self.reached_over_prev_high[symbol] = False
self.daily_lows[symbol] = self.Identity(symbol, Resolution.Daily, Field.Low)
history = self.History(symbol, 1, Resolution.Daily)
if not history.empty:
self.previous_day_highs[symbol] = history.iloc[0]["high"]
self.Debug(self.previous_day_highs)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(timedelta(minutes=1)), self.UpdateMinuteHigh)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen(self.symbols[0], 1), self.SetFirstMinutePassed)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose(self.symbols[0], 1), self.ResetDailyData)
self.Log("Initialize finished")
def UpdateMinuteHigh(self):
self.Debug("Update minute high started")
for symbol in self.symbols:
if self.reached_over_prev_high[symbol]:
continue
prev_minute_bar = self.History(symbol, 1, Resolution.Minute).iloc[0]
if prev_minute_bar["high"] >= self.previous_day_highs[symbol]:
self.high_over_prev_high[symbol] = prev_minute_bar["high"]
self.reached_over_prev_high[symbol] = True
self.Debug("Update minute high ended")
def SetFirstMinutePassed(self):
self.Log("set first minute started")
self.first_minute_has_passed = True
def OnData(self, data):
self.Log("ONDATA ACTUALLY RAN")
if not self.first_minute_has_passed:
return
for symbol in self.symbols:
if self.Portfolio[symbol].Invested or not self.reached_over_prev_high[symbol]:
continue
if symbol in data and data[symbol].Price > self.high_over_prev_high[symbol]:
quantity = 500 / data[symbol].Price
self.SetHoldings(symbol, quantity)
self.Log(f"Bought {quantity} shares of {symbol} at {data[symbol].Price}")
stop_loss_price = self.daily_lows[symbol].Current.Value
self.Log(f"Stop loss set at ${stop_loss_price}")
self.StopMarketOrder(symbol, -quantity, stop_loss_price)
def ResetDailyData(self):
self.first_minute_has_passed = False
self.reached_over_prev_high.clear()
self.high_over_prev_high.clear()
self.previous_day_highs.clear()
self.daily_lows.clear()
Mia Alissi
Here is the corrected version of the code with PEP 8 compliance:
Ensure that the necessary imports are included, and verify the availability of data for the specified date.
Sources: Momentum Effect In Commodities Futures, The Dynamic Breakout II Strategy, Bybit Custom Data Crypto Regression Algorithm, Bybit Crypto Futures Regression Algorithm, Momentum And State Of Market Filters
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.
Daniel Rios
That did not fix the problem. Why would the data not be available for that specific day? It's a valid trading day for sure.
Louis Szeto
Hi Daniel
I did not reproduce your issue, but just getting margin insufficiency messages. Can you reproduce it now?
Best
Louis
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.
Daniel Rios
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!