GPT: Mean Reversion strategy in Python makes 813% — Transcript
Full transcript
- 0:00Hey guys, it's Salah. In this video, I
- 0:01will show you a mean reversion strategy
- 0:03in Python which not only has a high win
- 0:06rate, but it also works on multiple
- 0:07assets. So, at least until this point,
- 0:10it is the best mean version strategy
- 0:12that I have ever written. As always,
- 0:13first I will show you the entry and exit
- 0:15rules of the strategy on trading view
- 0:17and then we will use GPT to convert
- 0:20those rules into Python code so we can
- 0:22run some back test on it. So, you can
- 0:24see that even if you are not a great
- 0:25programmer, you can still do research
- 0:28with Python for algo trading. Before we
- 0:30continue, I got to say I am not a
- 0:32financial adviser and everything that
- 0:33you see in this video is simply a
- 0:35trading tutorial. Trading always has
- 0:37risk. So no matter what the win rate
- 0:39that you see for the result of this
- 0:41strategy, you should always be careful
- 0:43and do your own research before trading
- 0:45this strategy or anything like it. So
- 0:47with that out of the way, let's get
- 0:48started.
- 0:50Now, this is a mean reversion strategy,
- 0:52which means unlike trend following
- 0:54strategies, we're not going to begin
- 0:56with a leading trend indicator because
- 0:58we don't want the price to continue to
- 1:00its direction. Instead, what we want is
- 1:02for it to go back to its mean, which
- 1:04means we're going to take a position the
- 1:05opposite side of the current direction
- 1:07of the market. Now, a great indicator
- 1:09for that is Ballinger band. So, let's
- 1:11look it up.
- 1:14By the way, the time frame of the
- 1:16strategy is 1 hour. All right. So simply
- 1:18put, we want to buy at the lower band of
- 1:20the Ballinger bands, which would be this
- 1:22green line here. And if it's a short
- 1:24position, we want to do opposite, which
- 1:25means we're going to open your short
- 1:27position at this price. Now, in case
- 1:29you're not already familiar with the
- 1:30Ballinger bands, this blue line here is
- 1:33a moving average. And then these two
- 1:35other lines are simply two standard
- 1:37deviations away from that mean. because
- 1:39statistically speaking whenever the
- 1:41price goes two standard deviation away
- 1:43from the mean it is more likely to go
- 1:46back to its mean. Now I don't remember
- 1:48the exact numbers for this but it's a
- 1:50very interesting theory. However, what I
- 1:52just described isn't actually an interle
- 1:54of the strategy. It's basically where we
- 1:57want to open the position. Now the next
- 1:59indicator which is the main indicator of
- 2:01the strategy is simply the RSI. So let's
- 2:04look it up on trading view.
- 2:08Now, as you already know, the RSI is
- 2:11simply an oscillator. Usually, we want
- 2:14to short when it goes above the
- 2:16threshold, which would be here, and we
- 2:19want to go long when it's vice versa.
- 2:22But the way I want to use it in this
- 2:23strategy again, because it's mini
- 2:25reversion, is actually quite different
- 2:27because I actually want to buy at a
- 2:28pullback. So, I want to be a direction.
- 2:32I want it to be a trend. But instead of
- 2:34buying instantly, we want to bet that
- 2:36the price is going to have a pullback.
- 2:39So I'm actually looking forward for
- 2:41scenarios like this. So let's say there
- 2:43is a strong trend, but I want to buy at
- 2:45pullback like in here for example. So
- 2:47that when it goes up again, I'm going to
- 2:50make a profit. But here's the thing. If
- 2:52I only buy when the price is for example
- 2:55like here above the threshold, the
- 2:57window of opportunity is going to be
- 2:58very small. So instead, what I'll be
- 3:01doing is I will still use the RSI but on
- 3:04a bigger time frame. So let's do that on
- 3:07trading view
- 3:10and change the time frame to
- 3:134 hours.
- 3:16Click okay.
- 3:18All right. So now from here all the way
- 3:22until here would be a valid opportunity
- 3:24for us to open the position. All right.
- 3:26Next, we're going to have to think about
- 3:28situations where maybe the RSI is like
- 3:31this, but there isn't an actual trend or
- 3:33volatility in the market like in this
- 3:35case. So, even if there isn't, for
- 3:37example, in here, the RSI could still be
- 3:40overbought. For example, in here, right?
- 3:42And yes, this is a good candle, but it
- 3:44doesn't mean that it's a trend, right?
- 3:46Now, yes, in this case, if we did open a
- 3:49long position, it would have made us
- 3:50profit, but there will be many false
- 3:52breakouts, and we need to be careful
- 3:54with those. So, the next indicator that
- 3:56I'll be using is simply the ADX. So,
- 3:58let's add it to our chart. And because I
- 4:00don't want to lose so many trades
- 4:02because this is also some kind of
- 4:04squeeze strategy, you could call it. So,
- 4:06we don't want to always wait until the
- 4:08trend is really, really strong, right?
- 4:10We want to be early with it. For that
- 4:12reason, unlike my previous strategies,
- 4:14instead of setting the ADX threshold to
- 4:16a number such as 40, in this one, I'm
- 4:18going to set it to 20. So, let's do that
- 4:21on Trading View. So, wherever this line
- 4:25is above this threshold, we're okay to
- 4:27open the position. But this could also
- 4:29have some false breakout. So, what I'm
- 4:32going to do is to do the same thing, but
- 4:34also on the bigger time frame. So, this
- 4:39will be again 4 hours. We'll do this.
- 4:42But for the 4 hours, I'm going to set
- 4:44the ADX threshold to 40, which would be
- 4:47this luck. Okay? So, by doing this, I
- 4:49won't be able to buy here. I can only
- 4:52buy from here all the way until here.
- 4:55All right. So, we have all the entry
- 4:57rules except one more thing. We're going
- 4:59to need one last filter for the trend.
- 5:01Now, again, I know this is not supposed
- 5:03to be a trend following strategy, but
- 5:05still we need the trend indicator except
- 5:07that we didn't begin with that. I'll
- 5:09show you what I mean. So, usually in a
- 5:11trend following strategy, you want to
- 5:13use a trend indicator with the current
- 5:15time frame. And that could be anything.
- 5:17It could be the moving average, the
- 5:18super trend, the comma indicator,
- 5:20whatever. In this one, I'm going to use
- 5:22the super trend. So, let's add it. And
- 5:25also remove the RSI and the ADX so we
- 5:30can see this better. Now, let's also
- 5:33disable Ballinger bands so that this
- 5:35will be more clear to see. All right.
- 5:36So, you see, for example, here we have
- 5:39an uptrend because the super trend line
- 5:42is below the current price and it's also
- 5:44displaying the color green. And here we
- 5:47have a downtrend because the super trend
- 5:49value is above the current price. Also,
- 5:52it color is red. Now, this is important
- 5:54because that's how we're going to write
- 5:55the Python code for it. Now, here's the
- 5:57thing. If we used super trend as it is
- 5:59right now, for example, in here, this
- 6:02would have been considered a downtrend.
- 6:04But as I explained, I actually want the
- 6:06price to have a pullback, right? So, no
- 6:08matter where it's going before, I want
- 6:11it to go actually against that price for
- 6:14me to be able to open the position. So
- 6:16if it changes the direction of the trend
- 6:17very quickly, it won't be good enough
- 6:19for me. So what I'm going to do is to
- 6:21simply change a time frame one more time
- 6:24from whatever my charts is, which in
- 6:27this case would have been the hourly
- 6:29into 4 hours.
- 6:32Click okay. And now you see this period
- 6:35here is no longer a downtrend, but it is
- 6:38an uptrend because it is displaying the
- 6:40green color. And just like that also
- 6:42this one here isn't considered an
- 6:44uptrend because on the 4hour time frame
- 6:47it is still a downtrend. Now this is
- 6:49something that we would have never done
- 6:50in a trend following strategy but in a
- 6:52mini version that's perfectly fine. All
- 6:54right. So we talked about the entry
- 6:55rules of the strategy but now let's talk
- 6:57about the exit rules for both the stop
- 6:59loss and take profit. So for that I'm
- 7:01going to disable super trend and enable
- 7:04the Ballinger bands again. Zoom it in.
- 7:08All right. So assuming we open the
- 7:10position at the lower band. So I could
- 7:12be here for example. I want the takerit
- 7:15to be at the upper band which would be
- 7:17here at this red line. And of course do
- 7:19the opposite for a short position. Now
- 7:21what about the stop loss? Well for that
- 7:23just like my previous strategies I'm
- 7:25going to use the ATR indicator. So
- 7:27basically it will tell us on average how
- 7:30much every candle has changed in the
- 7:32price. So for instance, let's say from
- 7:33here to here is the actual average price
- 7:36that every candle is changing because in
- 7:38one of them is changing less but in
- 7:40another like here for example is
- 7:42changing a lot. Right? So let's say this
- 7:44is the average and then I want six times
- 7:46of that value to be below my entry. So
- 7:49if my entry for example is here I want
- 7:51it to be six times of this below this
- 7:54which would end up something like here
- 7:56for example. Now if you want the exact
- 7:58value of it, of course you could look up
- 7:59the ATR indicator on trading view and
- 8:02here you would get that value. So in
- 8:04here it would have been 725.
- 8:08So you need to calculate six times of
- 8:12that and subtract it from here. Now the
- 8:14ATR indicator isn't really helpful for
- 8:16manual traders at least based on what
- 8:18I've seen. But in algo trading it is
- 8:20actually very handy. Now, I should also
- 8:22emphasize this that usually in a trend
- 8:24following strategy, we want to have a
- 8:26high risk-to-reward ratio, which is why
- 8:29we want to make sure that when we win,
- 8:30we win bigger than the amount when we
- 8:32lose, which is why even when we do use
- 8:35the ATR, we usually use values such as
- 8:37two or three. But because this one is a
- 8:38mini version, we usually do the
- 8:40opposite, which means the win rate of
- 8:42the strategy is going to be higher.
- 8:44Something like 70 80% is like the
- 8:47minimum for such a strategy. But when we
- 8:50win, we win less. And when we lose,
- 8:52we're going to lose in bigger sizes,
- 8:54which is why our stop loss is being this
- 8:56much below the current entry. If this
- 8:58sounds a bit too complicated, just
- 8:59forget about it once I show you some
- 9:02back test results. You'll see what I'm
- 9:03talking about. All right. So, now that
- 9:04we have both the entry and exit rules of
- 9:07the strategy, we're ready to convert it
- 9:09into Python code. So, we can run some
- 9:10back test on it. Now, for this part, I
- 9:12want to simply feed the entry and exit
- 9:15rules to an LLM model and have it write
- 9:17the Python code for us. And as always,
- 9:19we have two options. One is to use Jesse
- 9:21GPT, which is the cloud version of it
- 9:24that I made myself with some
- 9:25instructions specifically for aggregate
- 9:27trading in Python. Or if you don't want
- 9:29to use my service because you have some
- 9:30privacy concerns or if you don't want to
- 9:33pay me anything, that's perfectly fine.
- 9:35You can just come to my channel and
- 9:37watch this previous video that I made
- 9:39which will show you how to selfhost this
- 9:42by yourself. But again, I'm going to use
- 9:43JGPT. So, let's get started. All right.
- 9:45So first we need to ensure we have
- 9:48picked the right model. Now this is
- 9:50right now set on quen 3 coder which I
- 9:52reviewed a while ago but for this I'm
- 9:55going to go with claw 3.7 sonnet which
- 9:58I'm always happy with. So I kind of see
- 10:00no reason to change this. So let's paste
- 10:03in the in rules. Now I simply ask the
- 10:05model please write a mini version of
- 10:07strategy with below rules. Use the RSI
- 10:09on a bigger time frame specifically the
- 10:114 hours time frame. For long positions
- 10:13the RSI must be above 70. For short
- 10:15positions, do the opposite. As for the
- 10:17trend, use the super trend indicator.
- 10:19Once it is displaying an uptrend, you
- 10:21want to go long. This should also use
- 10:23the bigger time frame. For the ADX, it
- 10:25needs to be above 40 on the bigger time
- 10:27frame. It needs to be above 20 on the
- 10:29current trading time frame. For short
- 10:31positions, do the opposite. For entries
- 10:33in long positions, the entry price must
- 10:35be the lower band of the current
- 10:36Ballinger bands indicator. Set the stop
- 10:39loss as the current entry price minus
- 10:41six times of the current ATR. Risk 2% of
- 10:44the accounts per each trade. For take
- 10:46profit in long positions, close at the
- 10:49upper band of the current Ballinger
- 10:50bands. For short positions, take profit
- 10:53at the lower band of the current
- 10:54Ballinger bands. So basically whatever
- 10:56just showed you on the chart, we are
- 10:58simply asking the model to write it for
- 11:01us. Let's hit enter and wait for the
- 11:03response. All right, so it wrote it for
- 11:05us. It is called mean version. So let's
- 11:07copy this name. All right. Let's go to
- 11:09Jess's dashboard. Create a new strategy
- 11:11and paste this name here. But I will
- 11:13also add the name RSI at the end of it.
- 11:15So it will be mean version RSI. Let's
- 11:18click on create. And now let's go back,
- 11:21copy the whole code and paste it here.
- 11:24Now I could review the code on the
- 11:26editor inside J's dashboard or we could
- 11:29open the VS code which is a
- 11:31significantly better IDE. So that's the
- 11:34one that I'm going to do. So mean
- 11:35reversion RSI. So let's look it up. All
- 11:38right. So first it defined the higher
- 11:40time frame candles with the syntax get
- 11:43candles. Then it's passing the current
- 11:45exchange symbol and specifically the
- 11:47time frame of 4 hours. So this is
- 11:49perfect. The higher TF RSI is returning
- 11:53TA. RSI for the RSI indicator. But
- 11:56instead of passing the current candles,
- 11:58it is passing this higher TF candles. So
- 12:00I really like what it did here. So first
- 12:02it defined a property and later we can
- 12:06pass it very easily whenever we need to.
- 12:09Next it defined the higher time frame
- 12:10super trend like this TA. Trend again
- 12:13passing the higher time frame candles.
- 12:15The higher time frame ADX is again
- 12:18TA.ADX with the higher TF candles. And
- 12:21the current TFADX is simply passing TA.
- 12:26ADX but instead of passing the higher
- 12:28time frame candles it is passing the
- 12:30current candles which in Jesse you can
- 12:33always get it by simply using
- 12:35selfcandle. So you don't need to define
- 12:37this anywhere. Now for the Ballinger
- 12:39bands again it is using this syntax and
- 12:42passing the current candles because we
- 12:44don't want the Ballinger bands on the
- 12:45bigger time frame. We want it on the
- 12:48trading time frame. All right. Next, it
- 12:50defined the ATR by simply returning
- 12:52TA.ATR and then passing the current
- 12:55candles. And then it defined the integer
- 12:57rules of the strategy. So, it's also
- 13:00displaying it here in a beautiful
- 13:01comment. So, the RSI needs to be above
- 13:0470 on 4 hours. The super trend is
- 13:06uptrend on 4 hours. The ADX is above 40.
- 13:09Now, let's take a look here. So, the
- 13:11higher TF RSI must be above 70. The
- 13:15higher TF super trend must be one.
- 13:18This is incorrect.
- 13:20And then the higher time frame ADX must
- 13:22be above 40. And the current time frame
- 13:25ADX must be above 20. So this is really
- 13:27good. But this part is not correct. So
- 13:31let's first see how many times it is
- 13:33defining this.
- 13:35Okay. So just once. So instead of this,
- 13:39I'm going to first of all rename this
- 13:41into simply trend.
- 13:44and I will get the super trend value
- 13:47like this. Then I will say this if the
- 13:51current
- 13:53price is above the value of S. trend and
- 13:59by the way this S dot trend is this
- 14:02value here. So if the current price is
- 14:04above it that means we are in uptrend
- 14:07and if it is below it like in here then
- 14:12we are in a downtrend right? So I'm
- 14:14going to say if the current price is
- 14:16above S. trend return one which means we
- 14:20are in uptrend otherwise we're going to
- 14:23return zero. So let's go back
- 14:28and now this is correct except that we
- 14:31should remove this
- 14:33and for short positions we should also
- 14:36do this. All right. So everything else
- 14:38looks good. Now let's go to the position
- 14:40sizing which is right here. Now the
- 14:43entry price is going to be the lower
- 14:45band of the current Ballinger bands. So
- 14:47I know this is correct. The stop loss is
- 14:50going to be the current entry price
- 14:51which would be this minus 6 times of the
- 14:54current ATR and we are risking 3% of per
- 14:58capita. How? Well by using this utility
- 15:00function risk to quantity. First it gets
- 15:03the current available margin which is
- 15:06how much money your account has for
- 15:07trading. Then it is passing the number
- 15:09for risk. So you see the second
- 15:12parameter is risk per capital. It is
- 15:14passing three. Next we have the entry
- 15:16price the surplus price and then we
- 15:19going to have to set the fee rate and we
- 15:22are passing the current fee of the
- 15:24current exchange that we're trading. Now
- 15:25this one you don't really know. Just
- 15:28pass it like this and you're good to go.
- 15:30And then to submit the actual buy order
- 15:32we're using self buy equals quantity and
- 15:35then enterprise. So notice that we are
- 15:37not subitting the surplus but we did the
- 15:39calculated so that we can use it here to
- 15:43estimate how much we should be buying in
- 15:45order to risk only 3% of capital per
- 15:48that position. Now for a short position
- 15:50we're doing the opposite. Now the should
- 15:52cancel entry is true. Now about this one
- 15:55we need to talk. So let's go back to the
- 15:57chart
- 16:01and bring back the Ballinger bands. So
- 16:03let's remove this. All right. So suppose
- 16:06that at this point for example we decide
- 16:10to submit the buy order and of course
- 16:13because we are setting a price lower
- 16:15than the current price it's going to be
- 16:16a limit buy order right so it won't be a
- 16:19market order and we said that the buy
- 16:22price is going to be at the lower band
- 16:24of the Ballinger bands. So in this point
- 16:26that would have been here right but as
- 16:29you can see on the next candle the price
- 16:31didn't reach here. Not only it didn't
- 16:33reach here but the value of the lower
- 16:35band is now here. So we need to update
- 16:38the order or submit another one and
- 16:41cancel the previous one. Right? And if
- 16:43we keep doing this at this point now
- 16:46this one actually isn't touching this
- 16:48but in this point it is right. So if we
- 16:52had kept our earlier entry price which
- 16:55was actually here then even at this
- 16:58point we would not have opened the
- 17:00position. So we want to wave to tell
- 17:03Jesse that hey keep cancelelling that
- 17:05order and submitting a new one if of
- 17:08course the in rules are still valid and
- 17:11to do that in Jesse we need to use the
- 17:13should cancel entry method and inside it
- 17:16simply return true if we were returning
- 17:18false it wouldn't have done this which
- 17:20means if your strategy is using a market
- 17:23order through open positions you're not
- 17:25going to even need to define this at all
- 17:27anyways after we have an open position
- 17:30so Assuming our order have been
- 17:32executed,
- 17:34we will use this method on open position
- 17:37which is where we submit both the stop
- 17:39loss and take profit. So we're saying if
- 17:41is a long position, this stop loss is
- 17:43going to have the quantity of the
- 17:45current position which we pass by
- 17:48self.position quantity and then the
- 17:50price of it is going to be the entry
- 17:52price of the current position minus 6
- 17:54times of the current ATR. For the
- 17:56takerit, the quantity will again be the
- 17:59quantity of the current position and the
- 18:01price is going to be the upper band of
- 18:03the current Ballinger bands. And of
- 18:05course, if it's a short position, we
- 18:06want to do the opposite. So, very
- 18:08simple. And then we also have the update
- 18:11position. Now, this one is keep updating
- 18:14the takerit price. So, assuming that the
- 18:17Ballinger bands is changing, which it
- 18:19is, it is also updating our takeprofit
- 18:22order. But actually, I don't want it to
- 18:25do this. So, it's doing something that I
- 18:27didn't ask for. So, I'm going to remove
- 18:29this. Now, you see the more specific you
- 18:32are with your request to the model, the
- 18:34more likely it is to follow it. Exactly.
- 18:36Right. And the more vague requests you
- 18:39give it, it might do some new stuff like
- 18:41this. So, I should have been more
- 18:43specific in my request, but it's
- 18:44perfectly fine. I'm okay with changing
- 18:47some things in the code, especially when
- 18:49I'm just removing stuff and all the rest
- 18:51is perfectly fine. All right. So now
- 18:52that we have everything working, let's
- 18:54go to Jesse so we can run our first back
- 18:57test. So let's remove these.
- 19:00Choose mean reversion RSI. Change the
- 19:04time frame to hourly and the
- 19:08symbol to BTC.
- 19:11And for the duration of the back test,
- 19:13I'm going to do since the beginning of
- 19:152024 up until 2025.
- 19:19And I will also choose
- 19:22this state. All right. So let's
- 19:25go back and change the fast mode to
- 19:29enabled. The benchmark is also on. So
- 19:33let's give this a try.
- 19:36By the way, the trading fees are set to
- 19:40almost the double number that it is
- 19:42actually on Binance. I do this on
- 19:44purpose because I want to leave some
- 19:46room for the slippage when the actual
- 19:48trading is happening or for the funding
- 19:50fee or any extra fee that isn't visible
- 19:53inside my back test. I also change the
- 19:55leverage number to 10, but it doesn't
- 19:57actually mean that I'm using 10 times
- 19:58leverage. Right? So, we'll get to that
- 20:00now. Let's take a look. All right. So,
- 20:03we took nine trades. We made 9.23%
- 20:07in profit. Now, the equity curve looks
- 20:10good, but we're not beating the market
- 20:13yet, right? So, but let's go down. And
- 20:15you see the max rodon is near zero.
- 20:18That's ridiculously low, which is
- 20:20awesome. And the win rate is 100%. Now,
- 20:23of course, this isn't realistic. You
- 20:25should never expect a win rate of 100%.
- 20:29And the sharp ratio is 2.48, which is
- 20:32really good. And all right, so
- 20:34everything else looks really good. Now
- 20:36the first thing I want to do is to go
- 20:38back to the code and increase the size
- 20:41of my position. So I'm going to add the
- 20:45quantity by five and again for short
- 20:47positions. Now one very important thing
- 20:49when you do this you need to remember
- 20:51that you are multiplying the entire size
- 20:54of the order by a specific number. So
- 20:57you're not risking 3% per each capital
- 21:00anymore. So this is kind of invalid now.
- 21:03But I'm doing it because I just want to
- 21:05show you the actual equity curve that
- 21:07we're getting now because I want to show
- 21:08you how you can beat the market returns
- 21:11at the end. But in an actual production
- 21:13ready, you want to use another formula.
- 21:15You want to increase maybe this number
- 21:17or even use some other formula for
- 21:19position sizing such as the Kelly
- 21:21criteria. But that's a topic for another
- 21:23video. So let's go back and rerun this
- 21:26again.
- 21:28Now, by the way, we're getting this
- 21:30error because even though the strategy
- 21:32is using the bigger time frame, which is
- 21:334 hours, I didn't define it here. You
- 21:37see, I should have added a data route
- 21:39and chosen the same
- 21:42route. Sorry, symbol and then it should
- 21:45have worked. But I didn't do that. And
- 21:47in back test, that's fine. Jess, it will
- 21:49give you an error, but it will take care
- 21:51of it behind the scenes and the back
- 21:52test will go through. But if you don't
- 21:55do this in a live trading session,
- 21:57you're going to face an issue and your
- 21:58session will get terminated. So be
- 22:00careful with that. All right. So let's
- 22:02remove this. And now the equity curve
- 22:04looks significantly better. Right? We
- 22:06are closer to beating the market. But
- 22:08we're still not fully beating it. Of
- 22:10course, I could simply add to the size
- 22:11of my position more, especially because
- 22:13it has still a very low max draw down,
- 22:16but this isn't a realistic number in all
- 22:19the periods, right? So I'm not going to
- 22:21do that. All right. So first I want to
- 22:23show you the result of the back test on
- 22:25other periods. So let's change this to
- 22:28beginning of 2022 up until 2023
- 22:32and the first day of that year. Let's
- 22:36run this one more time. I'm going to do
- 22:392023 up until 2024 and one more time on
- 22:432024 till 2025
- 22:46and 2025 until
- 22:51almost the 8th month which is just a few
- 22:53days ago. So let's run the whole thing.
- 22:56All right. So in 2022 you see we are
- 22:59ending with losing a little bit by 9%.
- 23:03The max draw is minus 22% and the win
- 23:07rate is 75%. So even though it's not
- 23:10horrible, we're ending with a loss. So
- 23:13that's the first lesson that even though
- 23:15it has a win rate of 100% in 2025 and
- 23:192024, it doesn't mean it's always
- 23:22profitable. In 2023, which you can see
- 23:24the date here, by the way, we're ending
- 23:27with 97% profit. The max thon is minus
- 23:3012% and the win rate is still 100%. So
- 23:34it looks really great for these three
- 23:37years but not so great here but it's not
- 23:39bad either. So let's do one interesting
- 23:41thing. I want to run the back test
- 23:43starting this date until this ending
- 23:46date. Right? So we can see the whole
- 23:48equity curve.
- 23:51So let's run it.
- 24:00All right, there we go. So now if we
- 24:02were trading this strategy in the past
- 24:043.5 years, we would have beaten the
- 24:08market, right? Not only with the ending
- 24:10P&L, but the max draw down is minus 22%.
- 24:14Which is significantly lower than the
- 24:16max draw down that BTC had in this
- 24:19period. We ended with 176% in P&L. The
- 24:22win note is 96% which is absolutely
- 24:25amazing. The sharp ratio is 1.47
- 24:29which is a realistic number and very
- 24:32good. And the average holding time would
- 24:34have been 33 hours. But actually I think
- 24:36we made a mistake because if we check
- 24:39out here it's only taking long
- 24:41positions. So no short positions. So
- 24:43let's go back to the code and see why
- 24:45that is the case. All right. So here for
- 24:47the trend value when it is an uptrend
- 24:51I'm returning one but when it's a
- 24:53downtrend I'm returning minus one. So
- 24:56that was my mistake. So let's go back
- 24:59and rerun this one more time. And I'm
- 25:01also going to rerun this one for 2022.
- 25:07There we go. Now we are beating the
- 25:09market and making a profit. So we're
- 25:13ending with 16% in profit. And in this
- 25:16one we are beating the market even
- 25:18further. So we ended with 253%
- 25:22and the max ardon is still minus 22%.
- 25:25Which means the sharp is now better. It
- 25:27is 1.57.
- 25:29But the win note is slightly lower. It
- 25:31is 94.59.
- 25:34All right. So this is actually great.
- 25:36But there's one problem with this
- 25:38strategy. You see, I don't like this
- 25:40total closed trade number because over
- 25:433.5 years, it only took 37 trades. And I
- 25:48think that's very low because the bigger
- 25:50the number of trades that the strategy
- 25:51is taking during a period, the higher
- 25:54the statistical significance of that
- 25:57strategy. In simple words, it means you
- 25:59can rely more on the result of the back
- 26:02test that is in front of you. And 37 for
- 26:053.5 years isn't good enough for me. And
- 26:08that brings me to the next good thing
- 26:10about the strategy that I found, which
- 26:12is it doesn't work just with BTC. It
- 26:15also works well with other coins. So,
- 26:18let's go back and I'm going to add some
- 26:21more trading routes. So, I'm going to
- 26:22add ETH
- 26:24again on the hourly
- 26:28and I'm going to add
- 26:31Doge again on the hourly. And by the
- 26:34way, let's also change this to be mean
- 26:37the version RSI. Now, this is enough for
- 26:39me for now, but you can find others by
- 26:42yourself. And now, if you run it again,
- 26:45we're going to get an error because the
- 26:47fast mode doesn't work when you are
- 26:49trading multiple trading routes, right?
- 26:51So, we need to disable this. And I'm
- 26:53also going to disable the benchmark so
- 26:55we can see the equity chart better. So,
- 26:57let's run this one more time.
- 27:00Voila, we have something with bigger
- 27:02number of trades. So it is 75 now. And
- 27:06look at this equity curve. It's
- 27:08beautiful.
- 27:10Now the P&L is 813%.
- 27:14The max RON is still minus 22%. So it
- 27:18didn't even get worse.
- 27:21And the win rate is 92%.
- 27:24And the average holding time is a little
- 27:27bit higher. Now by the way, you need to
- 27:28be careful with this cuz it is near to
- 27:3170 hours, right? So that's a few days.
- 27:34So if you trade this strategy at any
- 27:36point, you're going to have to hold the
- 27:38position for days maybe in a loss and
- 27:41psychologically that's going to be
- 27:42difficult. So that's something to be
- 27:44aware of. And then the sharp is now 1.95
- 27:48which is amazing. The winning streak is
- 27:5132 and the losing strike is two. Right.
- 27:54Overall, I think this looks great.
- 27:56Probably the best strategy I've ever
- 27:58made so far. And let's not forget that
- 28:01I'm still trading only three assets. We
- 28:04could continue this for some other
- 28:05assets and find some good ones and
- 28:07improve the results even further. Now,
- 28:09as always, I am going to submit both the
- 28:12code and the result of the back test of
- 28:14the strategy on other time frames or
- 28:16symbols on our strategies page which you
- 28:19can check it out. And if you already
- 28:21have a free account, you can also
- 28:22download the code of the strategy
- 28:24assuming it's a free strategy like this
- 28:26one that I just shared. Now guys, a lot
- 28:28of effort goes into research before
- 28:30making a video like this. So if you want
- 28:32me to continue making them, please
- 28:34support me in any way that you can.
- 28:36Liking the video, posting a comment with
- 28:38your opinion, and also subscribing to
- 28:40the channel helps me a lot. So thank you
- 28:42so much for your continued support. I'll
- 28:44see you in the next one.
- 28:48[Music]
- 28:57[Music]
About this transcript
This page contains the full transcript of GPT: Mean Reversion strategy in Python makes 813% by Algo-trading with Saleh, generated from the public captions YouTube serves with the video. The transcript has 5,239 words across 721 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.