Opening Range Breakout Tutorial – Time Period Consolidation in QuantConnect

Opening Range Breakout Tutorial – Time Period Consolidation in QuantConnect

Hi my name is Jared Broad, and in this
Boot Camp tutorial we’re going to be building an Opening Range Breakout
strategy. The Opening Range Breakout is a technical trading strategy which
uses the range of prices set in a certain period of time, say that first 30
minutes, and then trades when the price leaves that range. In this we’re going to
learn how to use consolidators, we’re going to take a position based on that
range, and then we’re going to use scheduled events to close out our
position. So let’s take a look at the first task, and we see here we’re
creating a consolidator. Consolidators are a way of taking a range of data and
representing it with a single point. You might often say you want a four-hour bar,
or a 3-hour bar, and that would be a single point made up of lots of other
data that went into it. QuantConnect provides this consolidate
API which can take a time delta, a period to consolidate, a calendar time frame say
weekly or monthly, or a resolution field which you pass in the resolution of
the data you’d like. All consolidators should be called in initialize. You want
to do this once and then use the results of that. It’s a common mistake to try and
do this inside OnData where you might end up making millions and millions of
consolidators when you only intend to have one. Once you’ve consolidated data
the output of that goes into an event handler. Event handlers are just a
function that are called when something happens. So in this case once the four
hours of data say have been consolidated into a single point that result will be
passed into the event handler. For this one the first thing we need to do
is create a 30-minute consolidator which triggers this function. So let’s go to
our code, and we call the self.Consolidate helper which provides a shortcut
to do all of the consolidation magic for us. Here we need to do
a 30-minute consolidator. We can use the timedelta, and we
need to make it for Tesla. 30 minutes. And then we need to use the
OnDataConsolidated event handler here. This is just a function name and we can
pass in any function name. For this one the function that it’s looking for is
OnDataConsolidated, so we pass in this one. We need to define that. That is a
function that doesn’t exist yet, so let’s create it. OnDataConsolidated(self, bar) Okay so we’ve got our event handler
here, OnDataConsolidated, and it’s passing in the bar that we’ve just created, the 30-minute
bar, now we need to save it to self.currentBar. Okay great, let’s
see how we go. Okay great on to the next task. One thing to note is that QuantConnect
has a lot of different data. some of the data has quote data meaning the bid and ask
prices, and some data has just the trade data, so just the last sale that occurred
in that asset. Depending on what you want you need to use a slightly
different consolidator to get the data out. For example equity data currently
only has trades and we believe that we’ll have quote data in January
2020. Forex data only has quotes, crypto has both trades and quotes, and
you can see the others in the table here. So it’s important to use thought into
what data you’re looking for. Another thing to note is that the bars that are
created depend on the period that you’re asking for. So in this example we’re asking for 30-minute bars so the start time of that
bar would be say at 9:00 a.m. and the end time would be 9:30. So when it’s released,
when it’s emitted into the event handler the time would be after 9:30, and the
start time would be 9:00 a.m., depending on the the length of the bar that you’re
creating. So let’s look at the bar’s time and decide whether or not to save it as
the first bar. We need to find the bar that starts at 9:30 a.m. Eastern Time.
Remember that the time value on bars is the start time of it. So we can say if bar.Time Okay so we’re going to be checking
the bar’s time is 9:00 a.m., the bars start time is also 9:30, and then if it is, save
that bar. Okay great on to the next task. Now that we have our first bar of the
trading day we need to look to see when the price moves above its high or its
low and then trade accordingly. When the price goes below the lowest
point of that opening range we need to short. With QuantConnect there’s no
special specifics needed for shorting you just need to pass in a negative
value for the quantity, or a negative for the SetHoldings. We want to monitor for
these particular points here as shown on the plot which is the high and low of
that opening range. Point number one, if we’re already invested let’s return. We can’t use the opening bar until it’s
ready, so let’s just check for it there. Now the next point, the close
price of Tesla is above the high We want to go long. And then conversely if the
price is below the low then we need to short. Okay, final check. Let’s submit our answer. So with QuantConnect it’s just a few
lines of code and you can look at the opening bar, you can check Tesla’s
price, you can see if you’ve broken out of that range and then trade
accordingly. With SetHoldings here passing in the -1 we’re
setting up a portfolio 100% short. Awesome there we go the task is completed. Okay great so far we’re actually just
entering the position long or short and we’re never actually liquidating or
going flat. So this can mean that we’re actually holding positions longer than
we should that aren’t based on the thesis of the strategy. We can use
scheduled events for this if we want to close out something or do something at a
specific time each day. Scheduled events are a way of passing in rules, date
rules and time rules, and then triggering events or functions to happen at a
specific time. It uses this method here, Schedule.On and the first
argument is the date rule, when you want, what day you want the event to run,
and the next argument is the time rule. So on that day what time do you want
this project or this function to run. Here you copy this and put it here. So
this is saying for every day that SPY is trading, or in our case Tesla at 1:30
p.m. we want to call ClosePositions to close out the trade. So let’s have a look
at our objectives, create the scheduled event at 1:30, call ClosePositions, and
then inside of our ClosePositions function set the opening bar to None and
liquidate Tesla. Let’s do that here. Inside our function we set the open bar to
none to reset our trading logic. And then we also liquidate Tesla. If you remember liquidate from our previous tasks here it doesn’t hurt to call it
even if you don’t have any holdings, so it’s easy to put this in
here without needing to do any checks to see our portfolio. Okay so now we’ve created
a scheduled event that’s going to run every day for Tesla at 1:30 and close
out any positions if we have them. see Let’s see how we go. Okay great, congratulations! You’ve passed
the Opening Range Boot Camp tutorial. Thank you very much for watching and
I’ll see you on the next one.

local_offerevent_note February 21, 2020

account_box Arnold Mann


2 thoughts on “Opening Range Breakout Tutorial – Time Period Consolidation in QuantConnect”

Leave a Reply

Your email address will not be published. Required fields are marked *