I trade with minute resolution data but need to execute paricular code at the end of the day. My intention is to execute this code on the last slice of the day.
After trying OnEndOfDay I shifted to a scheduled event with the following code. This code should work with regular market days and shortened days. The problem is that scheduled events don't fire during warm-up. Does anyone know of a way to detect the last bar of the day even during warm-up? Thanks for any help.
def Initialize(self):
self.EODevent = False
# Schedule EOD events
self.Schedule.On(self.DateRules.EveryDay('SPY'), \
self.TimeRules.BeforeMarketClose('SPY', 0), \
Action(self.EODevents))
def EODevents(self):
self.EODevent = True;
def OnData(self,slice):
...
if self.EODevent:
.. execute code here ...
self.EODevent = False
Rahul Chowdhury
Hi Hugh,
Since consolidators along with their event handlers still fire during warm up, we can use them to "schedule an event". Let's create a custom consolidator which consolidates data from 9 AM to 4 PM and creates 1 bar. We can then use that custom consolidator's event handler, which fires at the end of consolidation at 4 PM, to fire a block of code.
We cannot use a daily consolidator in this instance because daily consolidators end consolidation at midnight and not at market close.
# In Initialize customConsolidator = TradeBarConsolidator(self.Custom) customConsolidator.DataConsolidated += self.OnDataConsolidated ... def Custom(self, dt): period = timedelta(hours=7) start = dt.replace(minute=0) return CalendarInfo(start, period)
This creates a consolidator of period 7 hours which starts at 9 AM everyday.Inside the OnDataConsolidated event handler, we can access the latest minute bar data using self.CurrentSlice.
Hugh Todd
@Rahul Chowdhury, Thanks for that potential workaround. I don't see how to adapt it to work with shortened maket days, but otherwise it's good.
Thanks, Hugh
Rahul Chowdhury
Hi Hugh,
Generally warmup is used to prep indicators for use. What are you trying to specifically accomplish with warmup? There may be an easier way to do the same thing.
Best
Rahul
Hugh Todd
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!