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