I'm trying to enter a trade 2 minutes before market close. At this point, the currently working daily bar is almost complete and I want to add it to my entry logic. However, this is currently throwing an error:
from AlgorithmImports import *
from datetime import *
from math import *
class TestBreakout(QCAlgorithm) :
def Initialize(self) :
self._minutes_before_market_close = 2
self.allocation = 1
self._days_held = 0
self.gld_daily = self.add_equity("GLD", Resolution.DAILY).symbol
self.gld = self.add_equity("GLD", Resolution.MINUTE).symbol
self.consolidator = TradeBarConsolidator(timedelta(1))
self.subscription_manager.add_consolidator(self.gld, self.consolidator)
# Schedule entry and exit 2 minutes before market close
self.schedule.on(
self.date_rules.every_day(),
self.time_rules.before_market_close(self.gld, self._minutes_before_market_close),
self.entry_exit
)
def entry_exit(self) :
valid_entry = self.check_GLD_entry()
if not self.portfolio[self.gld].invested and valid_entry :
self.set_holdings(self.gld, self.allocation, False, "GLD_BO entry")
def check_GLD_entry(self) :
current_price = self.securities[self.gld].price
gld_history = self.history(self.gld_daily, 3, Resolution.DAILY)
todays_bar = self.consolidator.working_bar
gld_history.add(todays_bar)
if gld_history.empty :
return False
gld_highest_high = gld_history.iloc[:-1]["high"].max()
if current_price <= gld_highest_high :
return False
return True
def on_data(self, data: Slice) :
pass
Mia Alissi
Sources: Data Consolidation Algorithm, Time Period Consolidators - QuantConnect.com, Futures Fast Trend Following, with Trend Strength, Handling Data - QuantConnect.com, Calendar Consolidators - QuantConnect.com
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.
David Maucher
David Maucher
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!