Fading the Gap Tutorial - Mean Reversion in QuantConnect - YouTube

Channel: QuantConnect

[7]
Hi there, my name is Jared Broad, and in this Boot Camp tutorial we're going to be
[11]
looking into the fading the gap strategy. This strategy monitors the market for
[16]
gaps that occurred overnight, and then bets in the opposite direction assuming
[20]
that the market will mean revert. Here we're going to use scheduled events to
[25]
set up the scaffolding for our algorithm, and then we're going to use a rolling
[29]
window to calculate the price changes which occurred overnight. We're then
[33]
going to use a standard deviation indicator to try and eliminate a
[36]
parameter in our algorithm. So let's get started. First we're going to use
[44]
scheduled events to save the opening and closing prices for our algorithm. Here
[49]
we're going to use some time rules that we haven't previously seen, these are
[53]
AfterMarketOpen and BeforeMarketClose. As you'd expect these trigger the
[58]
scheduled event after the market opens and a few minutes before the market
[63]
closes. If you pass in zero here it will trigger
[67]
the scheduled event at exactly four o'clock on a traditional equities day.
[71]
So the first thing we need to do is create a scheduled event to run zero
[77]
minutes before the market closes or as the market closes at 4:00 p.m. We can
[82]
copy this code here and we want to run this every day before the market closes.
[101]
Then we need to pass in the event handler that we want to use, and we're
[105]
going to call an event handler here, ClosingBar. Let's create that method,
[114]
and while we're here let's create the other methods which are right here.
[125]
Finally another method here ClosePositions. Okay now we need another
[135]
scheduled event to run every day one minute after the market opens. At 9:30
[140]
exactly we don't have the full minute bar, so we need to wait for at least one
[144]
minute to get that opening price. Let's create a new scheduled event to
[151]
run after market open, one minute, and we want to call the opening bar event
[159]
handler. Okay, now finally we don't quite have the trading logic yet, but here
[169]
we're going to make sure that we close out our positions 45 minutes after the
[173]
market opens no matter what. So let's reuse the lot of this code here.
[178]
We want to do it 45 minutes later, and we want to call ClosePositions.
[189]
And then finally here we need to liquidate our position.
[198]
Now we're only trading Tesla, so we don't really need to pass in the symbol
[201]
here but it's always good to be safe. So now we're going to call ClosePositions
[207]
45 minutes after market open every single day. Okay let's see how we go.
[217]
Okay great, on to the next task. Here we're going to
[221]
create a rolling window. The rolling window will be used to save the price of
[225]
yesterday and the price of today, so that we can calculate price movement
[230]
overnight. And this will be the foundation of the gap calculation that
[234]
we do later. So a rolling window is kind of the opposite of an array. An array
[240]
when you and items get added on to the bottom. The oldest item is at position 0
[245]
and then as you add new items they increase in the index. Rolling windows
[250]
have an inverted index so the newest item is always at position 0 and the
[255]
older items get shuffled back over time. This can help for this sort of situation
[260]
where we want to just work with a few values, and we can access those values
[265]
easily with our rolling window. So let's first create a rolling window. Rolling
[271]
windows can be of any type, they can hold any sort of data that you want
[275]
except you just need to remember you need to define the data that you want to
[279]
save here. In this case we want a length two rolling window. So remember this is a 0
[284]
indexed array so that means that there will be data at position 0 and 1 and
[289]
those two numbers will be our window. The next parts says to add the current trade
[297]
bar of Tesla to our rolling window. With a scheduled event you don't always
[301]
have easy access to price data that occurred in the OnData method. To make
[306]
that easier LEAN has created a CurrentSlice property in your algorithm.
[311]
So you can see here we've got self.window.add, and we can just add
[318]
in the current slice for Tesla to our rolling window. Number 3, let's do exactly
[326]
the same thing but for the opening bar.
[333]
Okay let's see how it goes.
[341]
Okay great. Next we're going to use our rolling
[345]
window to get the price movement that happened overnight. So just like an
[349]
indicator the rolling window has the IsReady property to tell you when the
[353]
window is full. So with our example it's fairly easy because we've only got two
[358]
values, but imagine you've got a hundred items in your window you want to make
[361]
sure that the window is completely full of data before you start doing any sort
[364]
of math on it. So we can use this if not window ready then return to
[370]
control when we do our trading. And now we will never reach this part of the
[377]
code until we have a full rolling window. So let's calculate the price delta that
[383]
happened overnight. If you remember the rolling window position zero has the
[389]
most recent trade bar. So this is the bar we've just added up here, and this would
[395]
be the opening bar, so we want to make sure we get the opening price for that
[401]
bar. Then secondly we want to take the close of yesterday to find our
[409]
opening range from the close at 4:00 p.m. to the opening price at 9:30. So now
[415]
we have an accurate delta. Let's decide when to trade. So you can see here we're
[421]
going to say if the delta is less than -$10 then set holdings to 100% Tesla.
[427]
So if delta is -10.
[437]
So -10 is a fairly arbitrary number, but for the
[440]
sake of this demonstration we're using this to decide when to trade. But of
[445]
course in production would never want to use such an arbitrary parameter in your algorithm.
[452]
Okay great on to the next task
[459]
Okay so now we've got our algorithm trading, but we're using the -10
[464]
value as a parameter. One way that we could eliminate this is to look at
[469]
the deviation of the gaps to see if they're normal, and if it's within an
[474]
expected range. If it goes outside of that expected range that could be a
[478]
good signal for us to trade. So to do that we could use the standard deviation
[483]
indicator to monitor the volatility and see if our jumps, our overnight gaps, are
[489]
outside of that range. To do that we're going to use a manual indicator. Manual
[494]
indicators under the hood are very similar to normal indicators except you
[499]
need to update them with price data manually. So you can see here there's a
[504]
manual indicator for every automatic helper but it's just the full spelling
[508]
of the indicators name. It takes the name and the period just like a normal
[515]
indicator. So first let's create our standard deviation object for Tesla, and
[520]
we want to create it with a 60-minute period. So if you note here this is 60
[527]
periods but it doesn't necessarily know the resolution of the
[531]
data because we pump it with data, so it has no idea of its resolution. We control
[537]
this indicator totally. So next we're going to update that indicator with some
[543]
data. The update method takes two arguments, the time and the value you'd
[547]
like to update, so in this case we're going to pass in the close of Tesla.
[558]
Here we're going to have a standard deviation indicator that represents the
[563]
last 60 minutes of the trading day from yesterday. Now point number three. Let's
[570]
check if our indicator's ready before we use it. This is always a good thing to do.
[575]
So if not ready then let's return
[586]
Okay great, so now we're pretty sure that our indicators is created, is being
[590]
updated, and now it's ready to use. Now we can use that indicator to figure out how
[597]
many standard deviations the overnight gap represents. Standard deviations
[602]
typically range from negative 3 to positive 3 and when the standard
[607]
deviations are more than negative 3 it has less than 1% chance of occurring. So
[613]
we can use this for our trading.
[624]
Let's calculate the number of standard
[627]
deviations that this price delta represents. We can take the price
[631]
delta divided by the standard deviation value.
[635]
Okay, and when that is more than negative 3 we trade.
[658]
Let's see how we go.
[663]
So far so good.
[667]
Okay great, congratulations you've passed the Fading the Gap Boot Camp tutorial. In
[673]
this tutorial you've learned about manual indicators, scheduling after
[677]
market open and before market close, and using the current slice property of your
[681]
algorithm. I hope you enjoyed the tutorial. Thank you for watching, and I'll
[684]
see you in the next one!