This content details a quantitative trading strategy that enhances a traditional breakout approach by integrating a machine learning (decision tree) model to filter false signals, aiming for superior risk-adjusted returns compared to the S&P 500.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
So today we are going to combine
breakout strategy with a machine
learning or AI model like a decision
tree model and see what we get and we've
got a substantial great results from
this strategy and we have tested it over
multiple stocks. So it's basically a
portfolio back test. So we can avoid any
kind of selection bias uh in the
strategy and what basically the strategy
does is that it takes a normal breakout
strategy which any any trader out there
uses and it filters out any kind of
false signals via using this machine
learning or AI model. So for example
here, let's take the case of Google
here. So Google uh has got many
breakouts like here for example it's got
a breakout here and breakout here and
came down. It's on a weekly chart but
again there's a breakout here and vend
up. So basically what the machine
learning model is going to do it's going
to take in uh some input features. So it
could be the volume because that's
generally what the trading community
uses. It could be volume, it could be
gaps, it could be the length of the buy,
it could be anything. You can you can
feed in anything with a machine learning
model and that's the beauty of machine
learning strategies that you can feed in
lots of input features and it will take
the most important input features uh
necessary for this strategy to be
beneficial. So the uh we're going to do
a training and testing data. We're going
to split the training and testing data
into completely different sections. So
we don't have any curve fitting. And
obviously like I said before, we're
going to do it over multiple stocks. um
i.e. portfolio testing and we our goal
is not just to get returns higher
returns. Our goal is to get uh returns
but also to protect the portfolio
protect our portfolio from going bust.
Uh and our fundamental goal is to beat
the S&P 500 buy and hold CAGR to maximum
draw down ratio. So that's like 10%
divided by minus 55. Uh that's 0.18. Uh
so for this strategy what we're going to
do is we're going to use a machine
learning model. So the machine learning
model that we use is a decision tree.
Now again I've I've discussed the
decision tree model multiple times
before uh but if you want to have a
thorough understanding there's a video
here using AI decision tree model to
trade stocks. So in there you would get
a complete understanding of how a
decision tree model works and it's very
imperative that you actually do study
what a decision tree model does because
once you know the decision tree model
and how it functions then you'll
understand how effective it is in
applying the strategy. Okay. So let's go
into the training and the testing
methodology that we're going to use. So
uh generally we use a fixed window
training. So that's basically you train
from a fixed um time period. Let's say
that's from 20200. I' I've gone through
this before in the previous videos but
still uh just to get a rough idea for
you guys to understand. So from 2020
you're going to train it and order to
test it you're going to take take a
period which is a completely different
period and see whether it works on that
testing period. So uh while choosing the
training period you have to be careful.
you have to make sure that the training
period does contain some kind of a black
swan event. Uh so for example in 20200
you have the dotcom bubble and also the
global financial crisis which are all
very significant events in the financial
markets and that can really give you
very good training in the model and that
model does survive any kind of black
swan event. So for example, if the 2020
crash did happen or whatever volatility
uh situations happen in the testing
model, you can understand that this
model does work. So that's the normal
traditional fixed window training and
that can be used in many strategies and
we do use that strategies. But then
there's also another one and that's
called sliding window. So sliding window
what happens is that the training and
the testing period keeps on changing. Uh
so for example there is end period of
training and then we test it and then
the training moves to the most recent
period and then it tests the next period
as well. So uh to make you understand a
very simple term. So for example, let's
take this case of this example here. So
we've taken 60 days and 30 days in the
strategy how we've done 80 and 30. We'll
we'll discuss that in a bit. Uh so for
example here we're going to train from
April 1st to June 1st. So that's 60 days
and then we're going to test it from
June 1st to July 1st to see whether it
works. Right? And when July 1st happens
then we're going to train from May 1st
to July 1st and then we're going to test
it till August 1. You can you can see
that trading data keeps on changing. So
what it achieves is that it achieves the
whole idea of adapting to recent market
movements. So what that helps you is
that any kind of market evolution taking
place the strategy gets evolved
accordingly and thus you get an
additional edge in trading from that
specific evolution. So again breaking
down in a table form for example April
1st to May 30th will be the training and
the testing June 1st to June 30 and then
on July 1st May 3rd to June 30 will be
the training and then again uh the
testing bit. So this is basically what a
sliding window is. Uh now just like I
said the sliding window has advantages
it has also got disadvantages as well.
So we're just going to break it down uh
here. So you can see the training is got
fixed for the fixed window and sliding
window slides forward is retrained on
the most recent data. Uh on the testing
part again the testing is fixed for the
sliding window. Testing follows each new
training uh period. Now the adaptability
part is where it makes a huge
difference. So fixed window the
adaptability is low. You have to
manually change it. Uh so for example
you did it from 2020 and you've trained
that model. So there is a a certain day
would come in where you have to be like
okay you know what I need to change that
training. I need to add additional uh
training months into it so that the
model gets trained much better. So there
is that manual execution of
adaptability. However, on the sliding
window, it keeps on adapting to newer
market. You can you don't have to worry
about um manually changing it. It keeps
on adapting to the recent market
conditions. Uh now what's a use case
scenario? So for use case, the best um
situation for longerterm trading would
be basically a fixed window. So if I can
feed in the uh black swan events like
the com or the GFC, then basically it
kind of takes care of any black swan
events. Some some strategies I can do a
fixed window and some strategy I can do
sliding window. A fixed window learns a
lot. So for example here we're only
learning 60 days and then testing it on
30 days. On a fixed window the learning
is much more broader. Uh it's learning
over 10 years of data or or any amount
of years of data. So it can learn a lot
and because it can learn a lot the model
can be much more effective. And again
it's like I said before it's good for
black swan events. Uh now slide even
window it doesn't have to be long-term
it can be short-term or whatever. It's
adaptive uh but it's got a big red flag.
It may not be black swan proof. So
because in this model we have decided
like 80 days or 30 days or 60 days and
30 days. What happens? There's no black
swan event in those 80 days, right? So
when that black swan event happens, this
model will will kind of not perform that
great and it it's kind of like a delayed
uh delayed adaptability. So it's only
after 60 days or 80 days that the new
learning has taken place to the model
and then the model is okay fine. uh we
have the situation and because of that
we uh will adapt to the market
condition. So it is sensitive to recent
market events and it is blind to
historical tail risk. Uh so these are
some of the things that we can achieve
uh do with a sliding window and sliding
window and compare when you do it
across. So when you do it from 2010 to
2025 something you have multiple sliding
windows multiple trading and testing
datas and when you have so many of those
things and also when you have a
portfolio level back testing uh with
lots of stocks without any selection
bias that in that way you are achieving
uh reducing the curve fitting as much as
you can. Okay. So this is one of the
biggest advantages of u quant quant
traders. It's boring and mechanical.
you're just inputting some some
strategies in there and you're achieving
the diversification
uh and you're applying proper stress
test over the recessionary period.
You're trying to check it during the um
periods whether it works or not. And
because of this uh the fundamentally
quant traders have a fundamental edge uh
over retail traders out there. So let's
go into the strategy here. So we're
going to look into the strategy codes
before we see the results of it. So uh
so basically we're just importing the
libraries and quant connect has got a
decision tree classifier library uh and
we import the decision tree classifier
and then we also import the standard
scaler which we are going to use. So the
entire code uh is available for our
quant program core students. You can
check them out. Um it's in the machine
learning and AI algorithmic trading uh
strategy and we have got multiple amount
of machine learning models and all the
machine learning models are
fundamentally designed to beat the S&P
500 CGR to draw down ratio. These are
some of the results uh of these uh
strategies. So we've got linear
regression model, we've got decision
tree, uh we've got another decision tree
model, uh we've got support vector
machine, we've got a current neural
network, long shortterm memory. We also
have discussed combining machine
learning models. Uh so for example, here
is RNN plus regression and then here's
RNN support vector machine. So these are
some of the strategies that we have
created for you guys to understand. Uh
so let's go back into the code and then
we've um we create a class here and then
there's a def initialize function. So
again uh you assumed to know some idea
about Python. If you don't know anything
about Python then I would say you can go into
into
this video here algorithmic trading zero
to hero in Python. It is a very thorough
beginner tutorial for you guys to uh get
to know about Python and then if you
want you can go through the quant
connect tutorial as well but that won't
be necessary. You can just read in
through this class and these functions
and you will understand what the code
does. Uh so basically we have set a
start date and we've got a end date I've
commented out because it's got to test
on the most recent data and then the
cash and the capital allocation. So here
what we're going to do is we're going to
do 5% allocation. So every single time
there will be 5% of the portfolio
allocated to a single position. So
maximum amount of position at any any
interval will be 20 position. So 20 into
five that'll be 100% portfolio
allocated. Um and then basically simple
data what what data we are importing and
then we have got the random seed. So the
whole idea of this random seed is that
uh when you create a AI model like a
decision tree model any kind of AI
models the results can be different
slightly uh because during the training
they initially check like a random
variable they take in a random weights
random variable weights and then they
keep on uh training on it and coming to
a conclusion. So sometimes the final
result might be slightly off. So for
example if it's like a 10% CG maybe you
might get like 10.5% or 11% CG or 9%
CGR. So it can be a slight difference.
So in order to make sure that every
single time we get in the similar
results or exact results uh that's the
reason why we use a random seed of RP NP
random seed 42. Now if people don't know
what 42 is the why why coders use 42 you
can browse online and find out there is
a story behind the number 42. So you can
go into chart GPT and find out why it's
used by coders a lot. Uh and then
basically we are just adding spy because
we'll be using spy as one of our filters
to enter and we're going to calculate
the moving average and we are going to
create a universe of stocks. Um so we're
going to create a universe of stocks
based on certain conditions that we like
and then we're going to warm up and
basically create a model to train uh
we're going to use rebalancing and we
are going to use that information to
train our model. So as you can see here
we have used decision tree classifier
maximum depth five uh and random state
42. Now you could basically make lots of
changes to this uh decision tree
classifier and that's essentially what's
called hyperparameter tuning. So if you
can go into the uh course material and
go into hyperparameter tuning and then
the PDF there
can get it on.
Okay. So you get to understand what kind
of hyperparameter tuning you could do
from what impact it does. For example,
max depth uh pretty much creates a
splits in the tree which you will have
which you will obviously understand when
you watch our video on the decision tree
uh and then how influence how much of an
influence it has on prediction of these
models. Similarly, there are other
hyperparameter tuning you could do like
some of the things are the minimum
sample splits uh minimum sample leaves
uh things like that and then if you can
go back and then you
go through the
decision tree lecture and here you would
basically see like a challenge or
something. So uh you can fine-tune it
based on the maximum depth change to
three uh and also add in the minimum
sample split change. So the whole idea
of all these things is to push you uh to
keep on changing the models and see what
significant impact it makes it to the
strategy. Uh and then things like you
know change the outperformance to QQQUA
instead of SPX uh increase the training
duration day see what the output for
that is. So instead of 80 days you can
do like 100 or 150 days and increase the
testing days as well.
And the other things like the quizzes,
the whole idea of the quiz again is for
you to make sure that you understand
what each part of the decision tree
model, what makes a significant impact
to each of the model. And when you go
through the course notes and the course
lectures and the hyperparameter tuning
and then go through the codes, you will
pretty much be equipped to understand
these questions and then make changes to
the codes in such a way to enable you to
make uh the strategy significantly
better than what you got here. So treat
this code more like for boiler plates
and from there you kind of build up on
it on what you have learned. Okay. So
now let's go back to the code that we
are discussing here. A standard scala is
basically just a function cuz sometimes
you get numbers like an RSI value is
different from a price value. So in
order to like standardize it, we use the
standard scala function. Uh so what
happens after the standard scaling is
that it will have a mean of zero and
standard deviation one. The whole idea
of that is like sometimes let's say for
example there's like a big number like a
huge number like 15,000 or something as
compared to like a RSI which is like 20
or something. So the model might give
significant importance to the bigger
number. So in order to avoid those kind
of situations in the model we need to
have some uh some kind of weight some
kind of importance to each number. We
can't give like a bigger number more
importance. Uh so that's the whole
reason of why we use standard scaler and
then basically we have used whether the
model is trained or not because once the
model is trained then we can execute the
strategy. Uh so this is basically where
function where we decide which stocks to
trade. So as you know liquidity can be a
big of an issue and slippages can be
issued. So what we're going to do is
we're going to find the best liquid
stocks and one of the best ways we can
find out the best liquid stocks is by
using the dollar volume. Uh so basically
what this does is that it checks if the
stock is fundamental later and it checks
the dollar volume of it and it sorts out
the top 100 stocks and we will only be
investing in that top 100 stocks. So the
20 stocks that we uh will be going in
that will be from the basket of 100
stocks. Uh then these two function here
is basically just to remove those stocks
or add those stocks based on the dollar
volume. Now this is the on data function
where we will uh decide when when to
enter the strategy and when not to end
the strategy. uh we will be running the
train model function and as you can see
here uh the time delta is 30 days. So
every 30 days we will be retraining the
model and uh let's go and see where the
model is now. So here's a training model
and we are going to do 80 days. So
remember we said uh 60 days and 30 days
here. So instead of 60 days and 30 days
we'll be doing 80 days in this specific
model. Uh you can keep on changing
whatever you want. You can make it 90
days, you can make it 100 days, you can
make the um testing much more longer as
well. So it depends on you. So what the
training model in summary does is that
they look back 30 days and they compute
the features, the input features that we
feed in which is kind of hidden in this
code in this video. So that can be
something like the gap. So it could be
the volume, it could be the price, it
could be anything that you feed in. It
can be multiple things or it could be
like simple one or two things as well.
And then it basically checks what
happened in the next 10 days whether the
stock kind of rose 3% or whatever. And
then based on that it uses a label yes
or no one or zero. And this collects
these training examples for all the
stocks that we uh we trade. And then we
scale them using the standard scaler.
And then the decision tree predicts
whether the stock is likely to rise
greater than 3% in the near future based
on its past features. So that's in
summary what the training model does.
And then basically we are going to give
out this information uh to the entry and
exit scenario which will be which will
be here. So we are prediction if the
prediction equals one then basically
enter the strategy. Uh so here we have
got the high as the 50-day high. Again
you can change this to 100 days or 20
days or whatever. Uh so that will be one
of our entry conditions. That's a
breakout basically what we're discussing
here. the breakout will be 50 days and
then we also check if the S&P 500 uh is
also trading at a good place and then
the prediction of the machine learning
model we'll say whether to enter or not
and that's it. So now let's go into the
results of the strategy and see how it
has performed. So, uh it's so it's
tested from 2010 to the most recent
date. Uh that's 15th of September. And
then we have got a brokerage fee of
$1,800 something dollars and we've got a
return of $1,700. Uh so let's go into
the minor details of it. So these are
some of the stocks where the trades have
been done. So you can see it's all big
names like Pepsi, Microsoft, Netflix,
Shopify, Facebook. So all the liquid
companies out there and that liquidity
gives us a fundamental edge uh in this
situation this strategy as well. So
let's see the CAGR. So CAGGR is 20%. And
draw down is 33%. So let's do a CAGR to
draw down ratio of S&P 500 which is like
10. The maximum draw down is 55. So 0.18
as we discussed before is the one that
we are trying to beat. Uh so now let's
see it on this specific strategy. So
that's 20.5
uh divided by a maximum draw down is
33%. So 33.4.
So we've got 0.61. So this is performing
spectacularly well. This specific
strategy from a CAGR to draw down
maximum draw down perspective. So what
we use to compare strategy performance
is CAGR to maximum draw down ratio. Now
I'm not going to go deep into it on why
because I've discussed this in the
previous videos and also done a blog
article. uh but I would suggest that you
go into chat GPT or any of the favorite
LLM models and kind of write in why CAGR
to maximum draw down ratio is a better
performance measure as compared to sharp
ratio and you will generally get an idea
uh on why that is the case. Uh so
hopefully you got a general idea on how
we have designed this strategy. Uh we've
the code is pretty simple. We have uh
put it into different sections. The
first section is just you know call
creating and initializing different
things creating the decisionary
classifier and the standard scaler and
then creating a basket of stocks where
we will be putting the trading in. Uh
and then obviously the basket has to
change based on the liquidity and then
obviously we are going to train the
model and once we train the model then
we're going to basically give out that
condition along with the breakout
condition to enter and then we will
enter the strategy and that's it. It's a
very uh structured code. Uh and these
are some of the many approaches you can
do with machine learning. So one thing
you shouldn't do in machine learning is
just use machine learning or AI just to
predict prices just to create uh the
direction that you're going to trade
rather you should have an existing
strategy just got certain amount of
alpha. So this strategy this breakout
strategy has got a little bit of alpha.
The machine learning is just making it
go slightly further and creating a
better results net net. And that's what
the whole idea of uh using machine
learning and AI should be. I see many
people out there just using machine
learning and AI to trade that alone is
not a good idea. You have to combine it
with some kind of value based approach.
So when you have an edge like for
example you have like a 10 uh like 15%
strategy or something and if you use the
machine learning or AI model you can
possibly increase that 15% CAGR to maybe
17% and possibly reduce the draw down
from like 35% to maybe 30% or 25%. So,
if you have any questions or doubts, let
me know in the comment section or
through email, which you can find in the
about page. I'll be happy to respond uh
if we have the time. Uh so, hope you
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.