Using AI For Optimizing Strategies In Python — Transcript
Full transcript
- 0:00hey guys it's Sol in the previous video
- 0:01I created a swing Trend following
- 0:04strategy which was performing well but
- 0:06in this video I'm going to use the
- 0:08optimization mode of Jesse which uses
- 0:10the genetic algorithm to improve the
- 0:13results that we already had also we're
- 0:15going to have a giveaway so make sure to
- 0:16stick until the end of the video so you
- 0:18won't miss it all right let's get into
- 0:19it so this was the performance that we
- 0:21were
- 0:22getting this was our p&l the number of
- 0:26Trades we did and the Max rodon and the
- 0:30sharp ratio everything looks okay but
- 0:33let's see if we can improve it so I'm
- 0:35going to open our strategy now we're
- 0:37using a believe four indicators for this
- 0:39strategy the adx has a threshold of 25
- 0:42and I'm not passing any period number
- 0:46for it and that's intentional and we're
- 0:48using three EMAs and we're using the ATR
- 0:53for setting our suplus and for setting
- 0:56our initial take profit all right let's
- 0:58begin so instead of 25 I'm going to
- 1:01write self HP which is a dictionary so I
- 1:05will give it a name adx
- 1:09threshold and then I will come down here
- 1:12and Define a new method called hyper
- 1:15parameters and in it I'm going to return
- 1:17a list of dictionary values dictionary
- 1:20has a name it has a type it has a
- 1:22default value and what the co-pilot is
- 1:26missing it will also have a Min and Max
- 1:28values
- 1:32now for adx the type of int is fine the
- 1:36default value of 25 is also find the
- 1:38minimum of 10 and Max of 50 it actually
- 1:41looks perfect okay so let's go back and
- 1:44continue we also have EMA
- 1:47values so instead of this one I'm going
- 1:50to say self HP
- 1:52E1 period I'm going to have E2 period
- 1:57and E3 now I will go here and add okay
- 2:01so this looks fine but it's not perfect
- 2:05so defa the default value for the E1 was
- 2:0821 that means the Min of it should not
- 2:10be two it should be 10 the max should be
- 2:1340 that's fine the default for the
- 2:16second period was 50 so now I don't like
- 2:22this Min so I'm going to set the Min to
- 2:2440 and the max to 70 and for the third
- 2:29one we had the period of 100 so the Min
- 2:33would be around 70 and the max would be
- 2:38130 okay this looks good next we have
- 2:42the ATR value which we are using
- 2:44initially for setting our suplus now we
- 2:47were multiplying the ATR value by two so
- 2:49instead of two I will say self HP sub
- 2:54ATR I'm going to copy this because I'm
- 2:57also using it elsewhere so here
- 3:00and also here and also here and now I
- 3:04will Define a new value called a stop
- 3:07ATR the type of it is still int actually
- 3:12for this one I don't want the type of it
- 3:14to be int because we don't just want it
- 3:16to be two or three or four we values
- 3:19such as 2.1 2.2 these are actually
- 3:22perfect with this use case so instead I
- 3:24will give it the type of float the
- 3:26default is two the minimum is one and
- 3:29the max is five actually five is a bit
- 3:32too much for my suplus I don't like it
- 3:34um 3.5 I believe is like good enough for
- 3:40me and we also need another one for the
- 3:43take
- 3:47profit going to copy this and paste it
- 3:50also here and I will add another
- 3:53one the default is two actually no the
- 3:57default was three in our strategy and
- 4:00the minimum of one and maximum of let's
- 4:04say
- 4:054.1 actually let's set it to five okay
- 4:08so this is good so for some indicators
- 4:11such as the EMA I am replacing the
- 4:14period number with the hyper parameter
- 4:16which we're going to optimize but for
- 4:17some other such as the adx or the ATR I
- 4:20don't do that why simply put the more
- 4:22hyper parameters you use in your
- 4:24strategies the more you try to optimize
- 4:26every single part of it the more it is
- 4:28likely that your strategy is going to
- 4:30end up being overfit which means it
- 4:33might perform well in back test but in
- 4:34live touring it won't so that's always a
- 4:37risk of optimization and we want to
- 4:39avoid that other than that I'd like to
- 4:42be able to understand my strategy to the
- 4:44fullest I don't want to be a lot of
- 4:45differences when I was trading it
- 4:47manually or when I was trading it with
- 4:49my butt and I almost never changed the
- 4:51default parameters on trading view when
- 4:53I'm trading so when I'm turning my
- 4:56strategies into algorithms I want to
- 4:58keep doing the same thing but I want to
- 5:01emphasize that if you are a beginner
- 5:03with Jesse if you're just getting
- 5:04started don't try to optimize every
- 5:06single part of your strategy and also
- 5:09never use the optimization mode for
- 5:11turning a negative strategy into a
- 5:13profitable one only use it to improve
- 5:15your existing strategies all right
- 5:18especially if you're a beginner if you
- 5:19haven't even executed let's say 100 back
- 5:22test with Jesse yet don't start with the
- 5:25optimization mod because you're going to
- 5:26face lots of issues and chances are you
- 5:29will just get exhaust it and stop
- 5:30trading all together and I don't want
- 5:32that happening to you okay so this looks
- 5:34really good but I also want to do one
- 5:36more thing because I think it will have
- 5:37some educational value so here we were
- 5:40using the current price for the entry of
- 5:43our strategy we were using a market
- 5:44order to open our positions but you
- 5:47might wonder what we were using a limit
- 5:49order well so what if instead of using
- 5:52the current price I said self price plus
- 5:55I say plus because this is a go short
- 5:57method so instead of some kind of number
- 6:00now to make this a dynamic number to
- 6:03work for every other strategy I'm going
- 6:05to use again the ATR indicator so I will
- 6:08say t ATR and I'm going to multiply it
- 6:12by a hyper parameter value I will call
- 6:14it entry ATR and actually let's copy
- 6:18this and go to go long but this time
- 6:22because it's for long position instead
- 6:23of adding we will be subtracting now I
- 6:27will come down and add in another value
- 6:30Now the default of it is set to one
- 6:33actually for okay so that's good for the
- 6:35default but the minimum I want it to be
- 6:37very low such as this or even lower than
- 6:41that and the maximum could be yeah let's
- 6:44say two so now we are using a limit
- 6:46order to enter our positions actually so
- 6:49let's run another back test for this
- 6:51strategy to First make sure that it
- 6:53actually works and we're not making any
- 6:55syntax error and second because I want
- 6:58to see what this change that just made
- 7:00had in our numbers so we were getting
- 7:04128% with the max run of 15 and with a
- 7:07sharp of
- 7:101.47 okay so we made significantly less
- 7:14than before and the Maxon is even
- 7:17worse okay so this is the number we're
- 7:19getting right now but it doesn't matter
- 7:21much so let's go to the optimization
- 7:24page now and everything is fine except
- 7:26the strategy so I'm I'm going to choose
- 7:30the correct one as for the duration you
- 7:32have to notice that it's a bit different
- 7:34than the back test mode so for instance
- 7:36when you are back testing and your
- 7:38duration is set to since the beginning
- 7:40of 2022 and up until let's say 2023 it
- 7:44will be for one year right but when you
- 7:46set the same number for the optimization
- 7:49it will split those candles into a
- 7:51training and a testing set and the exact
- 7:53number is actually it will use the 85%
- 7:55of it for training and the other 15% for
- 7:58testing also it's best if if you leave
- 8:00some time out for the cross validation
- 8:03period now if these numbers don't make
- 8:05sense to you right now that's okay I'm
- 8:07going to explain it later when we get
- 8:08the results so for this one I'm going to
- 8:10set the beginning to 2022 and the ending
- 8:14to 20 24 now this optimal number of
- 8:17Trades number here is actually quite
- 8:19important now what does it mean so if I
- 8:22run a back test with this sitation this
- 8:25is the result that I will get now the
- 8:27number of Trades that we're executing is
- 8:3053 so in the optimization for the same
- 8:33period you should set the total number
- 8:36of Trades to approximately similar
- 8:38number now why do we have this setting
- 8:39in the first place well the reason is
- 8:42imagine if your strategy during like one
- 8:44year it executed three trades but all of
- 8:47them were in profit which means your the
- 8:50win rate of your strategy is going to be
- 8:51100% is that a good strategy well on
- 8:54paper it seems perfect right it it has
- 8:57100% win rate but in reality it's it's
- 8:59not great because three trades is just
- 9:02too low it doesn't have statistical
- 9:04significance in other words you're not
- 9:06going to be confident trading that
- 9:08strategy you're not going to be
- 9:09confident with the uh with any number
- 9:12that or chart that you're seeing because
- 9:14there's a low chance that in the future
- 9:16the same thing is going to happen but if
- 9:18your strategy was executing let's say
- 9:20100 50 200 or whatever you will be much
- 9:23more confident in its metrics right so
- 9:26with that in mind we want to tell justy
- 9:28what would be an optimal number of
- 9:30trades for this strategy now the number
- 9:32that you pick it will have to do
- 9:34something with the duration of the back
- 9:35test and the time frame of your back
- 9:37test and that's why this isn't a
- 9:39hardcoded number and you have to give it
- 9:41as a setting now in this example we're
- 9:44trading the 4 hours time frame which is
- 9:46a big one and the number of Trades that
- 9:50we were getting in our backst was around
- 9:5253 so in the optimization mode I will
- 9:55set this number to something such as 50
- 9:58which it already is so this is good for
- 10:01me but if I wanted this strategy to
- 10:03execute more trades just to be more
- 10:05confident in its results yes I could
- 10:07have increased this number to S
- 10:08something such as 70 or something all
- 10:11right let's bring it back to 50
- 10:13everything else looks fine also when you
- 10:16are running the optimization mode make
- 10:18sure in the settings that firstly you
- 10:20set the CPU course to a number that you
- 10:23can afford so my machine has eight
- 10:25chords so I'm going to set this to six
- 10:29you can choose the fitness function the
- 10:31default which is the sharp is fine for
- 10:33me the warm-up candles is very important
- 10:36so if in my back test for instance I was
- 10:39using let's say a bigger number such as
- 10:41400 candles then I should make sure that
- 10:44in the optimization I also have the same
- 10:46number so this is really
- 10:49important and as for the exchange you
- 10:51can set the trading fee and right now
- 10:53this trading fee is set for spot trading
- 10:57not for trading futures so if I go back
- 10:59to the back test section and look at the
- 11:01fees for binance paper future is set to
- 11:04this
- 11:05number but on the optimization is
- 11:08actually higher so let's change this and
- 11:10the assorting capital of 10,000 is fine
- 11:12the type of it is Futures by default The
- 11:15Leverage mode is set to cross and The
- 11:17Leverage is set to
- 11:19three again make sure these numbers are
- 11:21the same that you were using for your
- 11:23back test all right everything else
- 11:25looks fine and we should be able to
- 11:27start the optimization
- 11:30oh so I forgot one thing uh let's also
- 11:33enable the debug
- 11:35mode and the fast mode for this
- 11:38strategy okay let's run it
- 11:41again notice that it's telling me that
- 11:43in 21 minutes it will finish well here's
- 11:46the thing the optimization mode or let's
- 11:48call it the Genting algorithm it has two
- 11:51phases the first phase is for generating
- 11:54the initial population now what does it
- 11:56mean it means it is creating some
- 11:58numbers some hyper parameters randomly
- 12:01and no one knows if those are going to
- 12:03be good numbers or bad numbers and
- 12:04that's what's happening right now so in
- 12:0621 minutes we're going to finish doing
- 12:08that in the second phase we're going to
- 12:10use those population and we're going to
- 12:13breed the numbers that are good because
- 12:15that's how the genetic algorithm works
- 12:17it breeds the good dnas and it tries to
- 12:19make even better babies or in our case
- 12:22that would be better results for our
- 12:23strategy so that's why we have two
- 12:26phases so in about 20 minutes I will
- 12:28come back and we're going to have
- 12:30another table but even in here you can
- 12:32see we have the average strategy
- 12:34execution time which is useful and
- 12:36here's the information about the
- 12:38strategy you're running this is the
- 12:40population size the number of iterations
- 12:43and the solution length the more hyper
- 12:44parameters that you add to your strategy
- 12:47these numbers are going to exponentially
- 12:49increase so that's also another reason
- 12:52why you shouldn't use as many IP
- 12:53parameters as you can just try to keep
- 12:56it as minimum as possible another reason
- 12:58is that your optimization sessions are
- 13:00just going to finish much faster it's
- 13:03been around 3 hours since we started
- 13:05this optimization session and it's been
- 13:0731% of the second phase of the
- 13:09optimization session however the thing
- 13:12about optimization is that you don't
- 13:14have to wait for it to reach 100%
- 13:17sometimes you can start playing around
- 13:18with the results as soon as you see some
- 13:21numbers that you actually like and also
- 13:23sometimes you just wait and wait and
- 13:25wait but after a certain point you don't
- 13:27see any improvements anymore more so
- 13:30especially for the sake of this tutorial
- 13:32because I want to continue my recording
- 13:35I'm going to use the results that I have
- 13:37so far but in reality you probably want
- 13:40to wait for for it to reach to 60% or
- 13:4370% uh or you want to try out all the
- 13:46numbers after it's at 30% try the number
- 13:49see if you like it and then at 60% and
- 13:52then compare them and see which one
- 13:53works better so here these are the type
- 13:56of results that we're getting now notice
- 13:57there's a rank which isn't really
- 13:59important but usually the best one comes
- 14:01first we have the DNA which is really
- 14:03the result that uh we're going to use
- 14:05later we have the fitness number which
- 14:07is what it's ranking them based on and
- 14:10then we have these numbers that are
- 14:12really useful to us we have the training
- 14:14versus testing win rate so this one is
- 14:16the win rate that in theatis is getting
- 14:19using this DNA on the training period
- 14:22but on the testing period is getting
- 14:24this win rate now these are the the
- 14:26total trades that were executed so in
- 14:28the train period is 36 on the T on the
- 14:32testing period is six and these are the
- 14:34p&l for those two back tests now what's
- 14:37the difference between training and
- 14:38testing well the training period is what
- 14:41Jesse is using to improve itself based
- 14:45on the metric that that it is using
- 14:47which is this Fitness number here but
- 14:49the testing number it has no effect in
- 14:52in in that but for our eyes it's really
- 14:55useful so why do we have it there
- 14:57because if you see a DNA that's
- 14:59performing extremely well in the
- 15:01training period but in the testing it's
- 15:03doing horribly then you can just ignore
- 15:05that one and and that's also why I said
- 15:08the ranking doesn't really matter much
- 15:10because the ranking is only considering
- 15:12the training period so this one looks
- 15:14good it and it is our number one rank uh
- 15:18but the total trades is actually lower
- 15:21than the second option but nonetheless
- 15:24let's begin with it so I'm going to copy
- 15:26this DNA here and go back to my editor
- 15:29and I will add a new function called DNA
- 15:33and it should return simply a string
- 15:37which is the DNA we just copied okay so
- 15:40let's go back to the back testing page
- 15:43now these were the results that we were
- 15:45getting and remember that in the
- 15:47previous video our results was actually
- 15:49better than this and this is because of
- 15:51that change I made last minute to open
- 15:53positions using a limit order because I
- 15:55also wanted to test that one but anyway
- 15:57so this is we have 5% pnl and minus 22%
- 16:02Max thron so let's open a new tab and
- 16:07rerun the same back test but this time
- 16:09using that
- 16:11DNA okay so this is the number we're
- 16:14getting the panl is at 97% and the max
- 16:18thron is at minus 8% so that's a
- 16:21significant Improvement but here's the
- 16:22thing so far we had the training period
- 16:25and the testing period those were the
- 16:26two that the optimization
- 16:29session was running and we were seeing
- 16:31the numbers here but what I do to make
- 16:33sure my strategy isn't overfit is to use
- 16:37a third period which is called a
- 16:39validation period so what I mean by that
- 16:41is for the period of this optimization
- 16:44session we chose since the beginning of
- 16:472022 up until
- 16:502024 just the first day so it hasn't
- 16:54been back toed on since 2024 up until
- 16:58let's say last month so let's go back to
- 17:01backst and execute another backst since
- 17:04the beginning of
- 17:062024 up until the 7th month and just to
- 17:11make sure we are running it without the
- 17:13DNA I will comment this now let's run
- 17:16this and this is the number we're
- 17:19getting okay 29% profit minus 13% Max
- 17:23thr okay so now this time let's run it
- 17:27with the DNA
- 17:29again 29% minus
- 17:3413 now we're getting 21% minus almost 7%
- 17:39so the profit number came down a bit but
- 17:42the max Rod also came down and if you
- 17:44remember from my previous video I
- 17:46mentioned that when the max rodon comes
- 17:48down it allows you to add more position
- 17:51which will end up making us a bigger p&l
- 17:53number so I I'm not sure if I can say
- 17:55this is a worse strategy than the other
- 17:57one uh but but but anyways let's just
- 18:00continue because I want to test other
- 18:02DNA numbers because this one was not
- 18:04making that many trades and I wasn't
- 18:06super happy about that now this one is
- 18:08making more
- 18:10Trad and it seems to have a better pnl
- 18:13at least in here so let's copy this
- 18:19one let's go back to back test 97 -
- 18:258 107%
- 18:299 and 7 okay so it does look better but
- 18:32also Let's test it on the validation
- 18:34period 21 -
- 18:387 19 - 11 so you see even though it got
- 18:43better in our training and testing
- 18:45period it did not get better in our
- 18:46validation period so that's why I won't
- 18:49use this
- 18:50DNA so let's carry
- 18:53on so this one is executing actually the
- 18:57most number of Trades
- 18:59and that's something I really like
- 19:03so let's copy this one go back to back
- 19:06test 107% minus
- 19:129
- 19:1495% and minus
- 19:1610% not bad but let's see how it does
- 19:20the validation
- 19:23period 24% minus 7% okay so it's doing
- 19:27actually really well in the validation
- 19:30period so far I think it's the best one
- 19:33let's see if we have any one more
- 19:38interesting okay so look at this one
- 19:40it's actually really good the win rate
- 19:43of it is lower than these ones but it is
- 19:46executing more trades and the pel is
- 19:50actually better okay so you see it
- 19:51jumped that's because a new iteration of
- 19:54the optimization session was just done
- 19:56and these are the new results but
- 19:58luckily I copied it before it happened
- 19:59so let's move
- 20:05on 95 -
- 20:1110 121 Max than- 12
- 20:1512% so it looks actually quite good 24%
- 20:20minus
- 20:227 oh it's actually resing the bed here
- 20:26it's really bad
- 20:30okay let's go back keep
- 20:33looking let's try this
- 20:43one 107 -
- 20:479 19% minus this okay so I think this
- 20:53one had the best numbers so far
- 21:02- 7
- 21:0424 95% minus 10 okay yeah so I think I
- 21:08really like this one so again this is
- 21:10what we had before the optimization
- 21:12session and this is what we have after
- 21:15and just to make sure we also run it on
- 21:17the validation period and it's also
- 21:20performing will there so before I forget
- 21:23there's something really important that
- 21:24when you run a back TST using a DNA
- 21:26string you will get a new section here
- 21:29called hyper parameters it will tell you
- 21:31the DNA and the values that have been
- 21:32used for this practice so remember the
- 21:35the hyper parameters we defined here so
- 21:37you might be asking okay so in this
- 21:39backst what number is being used for the
- 21:41adx threshold and if you go here it's 44
- 21:45what number is being used for the first
- 21:47period is 35 44 80 and the sub ATR is at
- 21:521 and the take profit ATR is at 1.6 and
- 21:57that inry ATR which I defined at the
- 21:59last second is at
- 22:020.06 so this can also be really handy
- 22:05and teach us a lot so for example I
- 22:07didn't even think that a very short sub
- 22:09ATR number would be this much helpful
- 22:12but you can clearly see that it actually
- 22:13is okay so just as a reminder these
- 22:16results were just after 34% of the
- 22:20progress bar if I come back in let's say
- 22:22three or 4 hours we're going to get
- 22:24completely new results and hopefully
- 22:26better ones but I think I made my point
- 22:29in for the video and that is if you
- 22:31believe in your strategy you know how
- 22:33it's working and you just want to
- 22:36improve some of the parameters of it or
- 22:38if you have a setup that you know in
- 22:40theory should work but in the practice
- 22:42just isn't working so an example of that
- 22:44is that last minute limit order that I
- 22:47added for our entry because I think it
- 22:50makes sense that just wait for a small
- 22:52pullback to enter your position but the
- 22:55numbers weren't really good and that's
- 22:57because yes the logic it was good but
- 23:00the exact amount that I was using just
- 23:02wasn't good in these type of scenarios
- 23:04the optimization session can be really
- 23:06really helpful now before I leave we're
- 23:07going to have a giveaway a random person
- 23:09who likes this video posts a comment and
- 23:11subscribes to the channel is going to
- 23:12win 1 million bunk token all right it's
- 23:15time to pick the winner of the last
- 23:20video and the winner
- 23:22is oh it CH me number nine number nine
- 23:27shim
- 23:30are you okay so let's pick another
- 23:33one all right so it says really liking
- 23:36these strategy videos thank you so much
- 23:38for your comment please reach out to me
- 23:39so that I can send you your bunk tokens
- 23:41thanks for watching and happy Trading
- 23:44[Music]
About this transcript
This page contains the full transcript of Using AI For Optimizing Strategies In Python by Algo-trading with Saleh, generated from the public captions YouTube serves with the video. The transcript has 3,949 words across 541 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.