YouTube2Text

GPT: Mean Reversion strategy in Python makes 813% — Transcript

by Algo-trading with Saleh · 5,239 words · 721 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hey guys, it's Salah. In this video, I
  2. 0:01will show you a mean reversion strategy
  3. 0:03in Python which not only has a high win
  4. 0:06rate, but it also works on multiple
  5. 0:07assets. So, at least until this point,
  6. 0:10it is the best mean version strategy
  7. 0:12that I have ever written. As always,
  8. 0:13first I will show you the entry and exit
  9. 0:15rules of the strategy on trading view
  10. 0:17and then we will use GPT to convert
  11. 0:20those rules into Python code so we can
  12. 0:22run some back test on it. So, you can
  13. 0:24see that even if you are not a great
  14. 0:25programmer, you can still do research
  15. 0:28with Python for algo trading. Before we
  16. 0:30continue, I got to say I am not a
  17. 0:32financial adviser and everything that
  18. 0:33you see in this video is simply a
  19. 0:35trading tutorial. Trading always has
  20. 0:37risk. So no matter what the win rate
  21. 0:39that you see for the result of this
  22. 0:41strategy, you should always be careful
  23. 0:43and do your own research before trading
  24. 0:45this strategy or anything like it. So
  25. 0:47with that out of the way, let's get
  26. 0:48started.
  27. 0:50Now, this is a mean reversion strategy,
  28. 0:52which means unlike trend following
  29. 0:54strategies, we're not going to begin
  30. 0:56with a leading trend indicator because
  31. 0:58we don't want the price to continue to
  32. 1:00its direction. Instead, what we want is
  33. 1:02for it to go back to its mean, which
  34. 1:04means we're going to take a position the
  35. 1:05opposite side of the current direction
  36. 1:07of the market. Now, a great indicator
  37. 1:09for that is Ballinger band. So, let's
  38. 1:11look it up.
  39. 1:14By the way, the time frame of the
  40. 1:16strategy is 1 hour. All right. So simply
  41. 1:18put, we want to buy at the lower band of
  42. 1:20the Ballinger bands, which would be this
  43. 1:22green line here. And if it's a short
  44. 1:24position, we want to do opposite, which
  45. 1:25means we're going to open your short
  46. 1:27position at this price. Now, in case
  47. 1:29you're not already familiar with the
  48. 1:30Ballinger bands, this blue line here is
  49. 1:33a moving average. And then these two
  50. 1:35other lines are simply two standard
  51. 1:37deviations away from that mean. because
  52. 1:39statistically speaking whenever the
  53. 1:41price goes two standard deviation away
  54. 1:43from the mean it is more likely to go
  55. 1:46back to its mean. Now I don't remember
  56. 1:48the exact numbers for this but it's a
  57. 1:50very interesting theory. However, what I
  58. 1:52just described isn't actually an interle
  59. 1:54of the strategy. It's basically where we
  60. 1:57want to open the position. Now the next
  61. 1:59indicator which is the main indicator of
  62. 2:01the strategy is simply the RSI. So let's
  63. 2:04look it up on trading view.
  64. 2:08Now, as you already know, the RSI is
  65. 2:11simply an oscillator. Usually, we want
  66. 2:14to short when it goes above the
  67. 2:16threshold, which would be here, and we
  68. 2:19want to go long when it's vice versa.
  69. 2:22But the way I want to use it in this
  70. 2:23strategy again, because it's mini
  71. 2:25reversion, is actually quite different
  72. 2:27because I actually want to buy at a
  73. 2:28pullback. So, I want to be a direction.
  74. 2:32I want it to be a trend. But instead of
  75. 2:34buying instantly, we want to bet that
  76. 2:36the price is going to have a pullback.
  77. 2:39So I'm actually looking forward for
  78. 2:41scenarios like this. So let's say there
  79. 2:43is a strong trend, but I want to buy at
  80. 2:45pullback like in here for example. So
  81. 2:47that when it goes up again, I'm going to
  82. 2:50make a profit. But here's the thing. If
  83. 2:52I only buy when the price is for example
  84. 2:55like here above the threshold, the
  85. 2:57window of opportunity is going to be
  86. 2:58very small. So instead, what I'll be
  87. 3:01doing is I will still use the RSI but on
  88. 3:04a bigger time frame. So let's do that on
  89. 3:07trading view
  90. 3:10and change the time frame to
  91. 3:134 hours.
  92. 3:16Click okay.
  93. 3:18All right. So now from here all the way
  94. 3:22until here would be a valid opportunity
  95. 3:24for us to open the position. All right.
  96. 3:26Next, we're going to have to think about
  97. 3:28situations where maybe the RSI is like
  98. 3:31this, but there isn't an actual trend or
  99. 3:33volatility in the market like in this
  100. 3:35case. So, even if there isn't, for
  101. 3:37example, in here, the RSI could still be
  102. 3:40overbought. For example, in here, right?
  103. 3:42And yes, this is a good candle, but it
  104. 3:44doesn't mean that it's a trend, right?
  105. 3:46Now, yes, in this case, if we did open a
  106. 3:49long position, it would have made us
  107. 3:50profit, but there will be many false
  108. 3:52breakouts, and we need to be careful
  109. 3:54with those. So, the next indicator that
  110. 3:56I'll be using is simply the ADX. So,
  111. 3:58let's add it to our chart. And because I
  112. 4:00don't want to lose so many trades
  113. 4:02because this is also some kind of
  114. 4:04squeeze strategy, you could call it. So,
  115. 4:06we don't want to always wait until the
  116. 4:08trend is really, really strong, right?
  117. 4:10We want to be early with it. For that
  118. 4:12reason, unlike my previous strategies,
  119. 4:14instead of setting the ADX threshold to
  120. 4:16a number such as 40, in this one, I'm
  121. 4:18going to set it to 20. So, let's do that
  122. 4:21on Trading View. So, wherever this line
  123. 4:25is above this threshold, we're okay to
  124. 4:27open the position. But this could also
  125. 4:29have some false breakout. So, what I'm
  126. 4:32going to do is to do the same thing, but
  127. 4:34also on the bigger time frame. So, this
  128. 4:39will be again 4 hours. We'll do this.
  129. 4:42But for the 4 hours, I'm going to set
  130. 4:44the ADX threshold to 40, which would be
  131. 4:47this luck. Okay? So, by doing this, I
  132. 4:49won't be able to buy here. I can only
  133. 4:52buy from here all the way until here.
  134. 4:55All right. So, we have all the entry
  135. 4:57rules except one more thing. We're going
  136. 4:59to need one last filter for the trend.
  137. 5:01Now, again, I know this is not supposed
  138. 5:03to be a trend following strategy, but
  139. 5:05still we need the trend indicator except
  140. 5:07that we didn't begin with that. I'll
  141. 5:09show you what I mean. So, usually in a
  142. 5:11trend following strategy, you want to
  143. 5:13use a trend indicator with the current
  144. 5:15time frame. And that could be anything.
  145. 5:17It could be the moving average, the
  146. 5:18super trend, the comma indicator,
  147. 5:20whatever. In this one, I'm going to use
  148. 5:22the super trend. So, let's add it. And
  149. 5:25also remove the RSI and the ADX so we
  150. 5:30can see this better. Now, let's also
  151. 5:33disable Ballinger bands so that this
  152. 5:35will be more clear to see. All right.
  153. 5:36So, you see, for example, here we have
  154. 5:39an uptrend because the super trend line
  155. 5:42is below the current price and it's also
  156. 5:44displaying the color green. And here we
  157. 5:47have a downtrend because the super trend
  158. 5:49value is above the current price. Also,
  159. 5:52it color is red. Now, this is important
  160. 5:54because that's how we're going to write
  161. 5:55the Python code for it. Now, here's the
  162. 5:57thing. If we used super trend as it is
  163. 5:59right now, for example, in here, this
  164. 6:02would have been considered a downtrend.
  165. 6:04But as I explained, I actually want the
  166. 6:06price to have a pullback, right? So, no
  167. 6:08matter where it's going before, I want
  168. 6:11it to go actually against that price for
  169. 6:14me to be able to open the position. So
  170. 6:16if it changes the direction of the trend
  171. 6:17very quickly, it won't be good enough
  172. 6:19for me. So what I'm going to do is to
  173. 6:21simply change a time frame one more time
  174. 6:24from whatever my charts is, which in
  175. 6:27this case would have been the hourly
  176. 6:29into 4 hours.
  177. 6:32Click okay. And now you see this period
  178. 6:35here is no longer a downtrend, but it is
  179. 6:38an uptrend because it is displaying the
  180. 6:40green color. And just like that also
  181. 6:42this one here isn't considered an
  182. 6:44uptrend because on the 4hour time frame
  183. 6:47it is still a downtrend. Now this is
  184. 6:49something that we would have never done
  185. 6:50in a trend following strategy but in a
  186. 6:52mini version that's perfectly fine. All
  187. 6:54right. So we talked about the entry
  188. 6:55rules of the strategy but now let's talk
  189. 6:57about the exit rules for both the stop
  190. 6:59loss and take profit. So for that I'm
  191. 7:01going to disable super trend and enable
  192. 7:04the Ballinger bands again. Zoom it in.
  193. 7:08All right. So assuming we open the
  194. 7:10position at the lower band. So I could
  195. 7:12be here for example. I want the takerit
  196. 7:15to be at the upper band which would be
  197. 7:17here at this red line. And of course do
  198. 7:19the opposite for a short position. Now
  199. 7:21what about the stop loss? Well for that
  200. 7:23just like my previous strategies I'm
  201. 7:25going to use the ATR indicator. So
  202. 7:27basically it will tell us on average how
  203. 7:30much every candle has changed in the
  204. 7:32price. So for instance, let's say from
  205. 7:33here to here is the actual average price
  206. 7:36that every candle is changing because in
  207. 7:38one of them is changing less but in
  208. 7:40another like here for example is
  209. 7:42changing a lot. Right? So let's say this
  210. 7:44is the average and then I want six times
  211. 7:46of that value to be below my entry. So
  212. 7:49if my entry for example is here I want
  213. 7:51it to be six times of this below this
  214. 7:54which would end up something like here
  215. 7:56for example. Now if you want the exact
  216. 7:58value of it, of course you could look up
  217. 7:59the ATR indicator on trading view and
  218. 8:02here you would get that value. So in
  219. 8:04here it would have been 725.
  220. 8:08So you need to calculate six times of
  221. 8:12that and subtract it from here. Now the
  222. 8:14ATR indicator isn't really helpful for
  223. 8:16manual traders at least based on what
  224. 8:18I've seen. But in algo trading it is
  225. 8:20actually very handy. Now, I should also
  226. 8:22emphasize this that usually in a trend
  227. 8:24following strategy, we want to have a
  228. 8:26high risk-to-reward ratio, which is why
  229. 8:29we want to make sure that when we win,
  230. 8:30we win bigger than the amount when we
  231. 8:32lose, which is why even when we do use
  232. 8:35the ATR, we usually use values such as
  233. 8:37two or three. But because this one is a
  234. 8:38mini version, we usually do the
  235. 8:40opposite, which means the win rate of
  236. 8:42the strategy is going to be higher.
  237. 8:44Something like 70 80% is like the
  238. 8:47minimum for such a strategy. But when we
  239. 8:50win, we win less. And when we lose,
  240. 8:52we're going to lose in bigger sizes,
  241. 8:54which is why our stop loss is being this
  242. 8:56much below the current entry. If this
  243. 8:58sounds a bit too complicated, just
  244. 8:59forget about it once I show you some
  245. 9:02back test results. You'll see what I'm
  246. 9:03talking about. All right. So, now that
  247. 9:04we have both the entry and exit rules of
  248. 9:07the strategy, we're ready to convert it
  249. 9:09into Python code. So, we can run some
  250. 9:10back test on it. Now, for this part, I
  251. 9:12want to simply feed the entry and exit
  252. 9:15rules to an LLM model and have it write
  253. 9:17the Python code for us. And as always,
  254. 9:19we have two options. One is to use Jesse
  255. 9:21GPT, which is the cloud version of it
  256. 9:24that I made myself with some
  257. 9:25instructions specifically for aggregate
  258. 9:27trading in Python. Or if you don't want
  259. 9:29to use my service because you have some
  260. 9:30privacy concerns or if you don't want to
  261. 9:33pay me anything, that's perfectly fine.
  262. 9:35You can just come to my channel and
  263. 9:37watch this previous video that I made
  264. 9:39which will show you how to selfhost this
  265. 9:42by yourself. But again, I'm going to use
  266. 9:43JGPT. So, let's get started. All right.
  267. 9:45So first we need to ensure we have
  268. 9:48picked the right model. Now this is
  269. 9:50right now set on quen 3 coder which I
  270. 9:52reviewed a while ago but for this I'm
  271. 9:55going to go with claw 3.7 sonnet which
  272. 9:58I'm always happy with. So I kind of see
  273. 10:00no reason to change this. So let's paste
  274. 10:03in the in rules. Now I simply ask the
  275. 10:05model please write a mini version of
  276. 10:07strategy with below rules. Use the RSI
  277. 10:09on a bigger time frame specifically the
  278. 10:114 hours time frame. For long positions
  279. 10:13the RSI must be above 70. For short
  280. 10:15positions, do the opposite. As for the
  281. 10:17trend, use the super trend indicator.
  282. 10:19Once it is displaying an uptrend, you
  283. 10:21want to go long. This should also use
  284. 10:23the bigger time frame. For the ADX, it
  285. 10:25needs to be above 40 on the bigger time
  286. 10:27frame. It needs to be above 20 on the
  287. 10:29current trading time frame. For short
  288. 10:31positions, do the opposite. For entries
  289. 10:33in long positions, the entry price must
  290. 10:35be the lower band of the current
  291. 10:36Ballinger bands indicator. Set the stop
  292. 10:39loss as the current entry price minus
  293. 10:41six times of the current ATR. Risk 2% of
  294. 10:44the accounts per each trade. For take
  295. 10:46profit in long positions, close at the
  296. 10:49upper band of the current Ballinger
  297. 10:50bands. For short positions, take profit
  298. 10:53at the lower band of the current
  299. 10:54Ballinger bands. So basically whatever
  300. 10:56just showed you on the chart, we are
  301. 10:58simply asking the model to write it for
  302. 11:01us. Let's hit enter and wait for the
  303. 11:03response. All right, so it wrote it for
  304. 11:05us. It is called mean version. So let's
  305. 11:07copy this name. All right. Let's go to
  306. 11:09Jess's dashboard. Create a new strategy
  307. 11:11and paste this name here. But I will
  308. 11:13also add the name RSI at the end of it.
  309. 11:15So it will be mean version RSI. Let's
  310. 11:18click on create. And now let's go back,
  311. 11:21copy the whole code and paste it here.
  312. 11:24Now I could review the code on the
  313. 11:26editor inside J's dashboard or we could
  314. 11:29open the VS code which is a
  315. 11:31significantly better IDE. So that's the
  316. 11:34one that I'm going to do. So mean
  317. 11:35reversion RSI. So let's look it up. All
  318. 11:38right. So first it defined the higher
  319. 11:40time frame candles with the syntax get
  320. 11:43candles. Then it's passing the current
  321. 11:45exchange symbol and specifically the
  322. 11:47time frame of 4 hours. So this is
  323. 11:49perfect. The higher TF RSI is returning
  324. 11:53TA. RSI for the RSI indicator. But
  325. 11:56instead of passing the current candles,
  326. 11:58it is passing this higher TF candles. So
  327. 12:00I really like what it did here. So first
  328. 12:02it defined a property and later we can
  329. 12:06pass it very easily whenever we need to.
  330. 12:09Next it defined the higher time frame
  331. 12:10super trend like this TA. Trend again
  332. 12:13passing the higher time frame candles.
  333. 12:15The higher time frame ADX is again
  334. 12:18TA.ADX with the higher TF candles. And
  335. 12:21the current TFADX is simply passing TA.
  336. 12:26ADX but instead of passing the higher
  337. 12:28time frame candles it is passing the
  338. 12:30current candles which in Jesse you can
  339. 12:33always get it by simply using
  340. 12:35selfcandle. So you don't need to define
  341. 12:37this anywhere. Now for the Ballinger
  342. 12:39bands again it is using this syntax and
  343. 12:42passing the current candles because we
  344. 12:44don't want the Ballinger bands on the
  345. 12:45bigger time frame. We want it on the
  346. 12:48trading time frame. All right. Next, it
  347. 12:50defined the ATR by simply returning
  348. 12:52TA.ATR and then passing the current
  349. 12:55candles. And then it defined the integer
  350. 12:57rules of the strategy. So, it's also
  351. 13:00displaying it here in a beautiful
  352. 13:01comment. So, the RSI needs to be above
  353. 13:0470 on 4 hours. The super trend is
  354. 13:06uptrend on 4 hours. The ADX is above 40.
  355. 13:09Now, let's take a look here. So, the
  356. 13:11higher TF RSI must be above 70. The
  357. 13:15higher TF super trend must be one.
  358. 13:18This is incorrect.
  359. 13:20And then the higher time frame ADX must
  360. 13:22be above 40. And the current time frame
  361. 13:25ADX must be above 20. So this is really
  362. 13:27good. But this part is not correct. So
  363. 13:31let's first see how many times it is
  364. 13:33defining this.
  365. 13:35Okay. So just once. So instead of this,
  366. 13:39I'm going to first of all rename this
  367. 13:41into simply trend.
  368. 13:44and I will get the super trend value
  369. 13:47like this. Then I will say this if the
  370. 13:51current
  371. 13:53price is above the value of S. trend and
  372. 13:59by the way this S dot trend is this
  373. 14:02value here. So if the current price is
  374. 14:04above it that means we are in uptrend
  375. 14:07and if it is below it like in here then
  376. 14:12we are in a downtrend right? So I'm
  377. 14:14going to say if the current price is
  378. 14:16above S. trend return one which means we
  379. 14:20are in uptrend otherwise we're going to
  380. 14:23return zero. So let's go back
  381. 14:28and now this is correct except that we
  382. 14:31should remove this
  383. 14:33and for short positions we should also
  384. 14:36do this. All right. So everything else
  385. 14:38looks good. Now let's go to the position
  386. 14:40sizing which is right here. Now the
  387. 14:43entry price is going to be the lower
  388. 14:45band of the current Ballinger bands. So
  389. 14:47I know this is correct. The stop loss is
  390. 14:50going to be the current entry price
  391. 14:51which would be this minus 6 times of the
  392. 14:54current ATR and we are risking 3% of per
  393. 14:58capita. How? Well by using this utility
  394. 15:00function risk to quantity. First it gets
  395. 15:03the current available margin which is
  396. 15:06how much money your account has for
  397. 15:07trading. Then it is passing the number
  398. 15:09for risk. So you see the second
  399. 15:12parameter is risk per capital. It is
  400. 15:14passing three. Next we have the entry
  401. 15:16price the surplus price and then we
  402. 15:19going to have to set the fee rate and we
  403. 15:22are passing the current fee of the
  404. 15:24current exchange that we're trading. Now
  405. 15:25this one you don't really know. Just
  406. 15:28pass it like this and you're good to go.
  407. 15:30And then to submit the actual buy order
  408. 15:32we're using self buy equals quantity and
  409. 15:35then enterprise. So notice that we are
  410. 15:37not subitting the surplus but we did the
  411. 15:39calculated so that we can use it here to
  412. 15:43estimate how much we should be buying in
  413. 15:45order to risk only 3% of capital per
  414. 15:48that position. Now for a short position
  415. 15:50we're doing the opposite. Now the should
  416. 15:52cancel entry is true. Now about this one
  417. 15:55we need to talk. So let's go back to the
  418. 15:57chart
  419. 16:01and bring back the Ballinger bands. So
  420. 16:03let's remove this. All right. So suppose
  421. 16:06that at this point for example we decide
  422. 16:10to submit the buy order and of course
  423. 16:13because we are setting a price lower
  424. 16:15than the current price it's going to be
  425. 16:16a limit buy order right so it won't be a
  426. 16:19market order and we said that the buy
  427. 16:22price is going to be at the lower band
  428. 16:24of the Ballinger bands. So in this point
  429. 16:26that would have been here right but as
  430. 16:29you can see on the next candle the price
  431. 16:31didn't reach here. Not only it didn't
  432. 16:33reach here but the value of the lower
  433. 16:35band is now here. So we need to update
  434. 16:38the order or submit another one and
  435. 16:41cancel the previous one. Right? And if
  436. 16:43we keep doing this at this point now
  437. 16:46this one actually isn't touching this
  438. 16:48but in this point it is right. So if we
  439. 16:52had kept our earlier entry price which
  440. 16:55was actually here then even at this
  441. 16:58point we would not have opened the
  442. 17:00position. So we want to wave to tell
  443. 17:03Jesse that hey keep cancelelling that
  444. 17:05order and submitting a new one if of
  445. 17:08course the in rules are still valid and
  446. 17:11to do that in Jesse we need to use the
  447. 17:13should cancel entry method and inside it
  448. 17:16simply return true if we were returning
  449. 17:18false it wouldn't have done this which
  450. 17:20means if your strategy is using a market
  451. 17:23order through open positions you're not
  452. 17:25going to even need to define this at all
  453. 17:27anyways after we have an open position
  454. 17:30so Assuming our order have been
  455. 17:32executed,
  456. 17:34we will use this method on open position
  457. 17:37which is where we submit both the stop
  458. 17:39loss and take profit. So we're saying if
  459. 17:41is a long position, this stop loss is
  460. 17:43going to have the quantity of the
  461. 17:45current position which we pass by
  462. 17:48self.position quantity and then the
  463. 17:50price of it is going to be the entry
  464. 17:52price of the current position minus 6
  465. 17:54times of the current ATR. For the
  466. 17:56takerit, the quantity will again be the
  467. 17:59quantity of the current position and the
  468. 18:01price is going to be the upper band of
  469. 18:03the current Ballinger bands. And of
  470. 18:05course, if it's a short position, we
  471. 18:06want to do the opposite. So, very
  472. 18:08simple. And then we also have the update
  473. 18:11position. Now, this one is keep updating
  474. 18:14the takerit price. So, assuming that the
  475. 18:17Ballinger bands is changing, which it
  476. 18:19is, it is also updating our takeprofit
  477. 18:22order. But actually, I don't want it to
  478. 18:25do this. So, it's doing something that I
  479. 18:27didn't ask for. So, I'm going to remove
  480. 18:29this. Now, you see the more specific you
  481. 18:32are with your request to the model, the
  482. 18:34more likely it is to follow it. Exactly.
  483. 18:36Right. And the more vague requests you
  484. 18:39give it, it might do some new stuff like
  485. 18:41this. So, I should have been more
  486. 18:43specific in my request, but it's
  487. 18:44perfectly fine. I'm okay with changing
  488. 18:47some things in the code, especially when
  489. 18:49I'm just removing stuff and all the rest
  490. 18:51is perfectly fine. All right. So now
  491. 18:52that we have everything working, let's
  492. 18:54go to Jesse so we can run our first back
  493. 18:57test. So let's remove these.
  494. 19:00Choose mean reversion RSI. Change the
  495. 19:04time frame to hourly and the
  496. 19:08symbol to BTC.
  497. 19:11And for the duration of the back test,
  498. 19:13I'm going to do since the beginning of
  499. 19:152024 up until 2025.
  500. 19:19And I will also choose
  501. 19:22this state. All right. So let's
  502. 19:25go back and change the fast mode to
  503. 19:29enabled. The benchmark is also on. So
  504. 19:33let's give this a try.
  505. 19:36By the way, the trading fees are set to
  506. 19:40almost the double number that it is
  507. 19:42actually on Binance. I do this on
  508. 19:44purpose because I want to leave some
  509. 19:46room for the slippage when the actual
  510. 19:48trading is happening or for the funding
  511. 19:50fee or any extra fee that isn't visible
  512. 19:53inside my back test. I also change the
  513. 19:55leverage number to 10, but it doesn't
  514. 19:57actually mean that I'm using 10 times
  515. 19:58leverage. Right? So, we'll get to that
  516. 20:00now. Let's take a look. All right. So,
  517. 20:03we took nine trades. We made 9.23%
  518. 20:07in profit. Now, the equity curve looks
  519. 20:10good, but we're not beating the market
  520. 20:13yet, right? So, but let's go down. And
  521. 20:15you see the max rodon is near zero.
  522. 20:18That's ridiculously low, which is
  523. 20:20awesome. And the win rate is 100%. Now,
  524. 20:23of course, this isn't realistic. You
  525. 20:25should never expect a win rate of 100%.
  526. 20:29And the sharp ratio is 2.48, which is
  527. 20:32really good. And all right, so
  528. 20:34everything else looks really good. Now
  529. 20:36the first thing I want to do is to go
  530. 20:38back to the code and increase the size
  531. 20:41of my position. So I'm going to add the
  532. 20:45quantity by five and again for short
  533. 20:47positions. Now one very important thing
  534. 20:49when you do this you need to remember
  535. 20:51that you are multiplying the entire size
  536. 20:54of the order by a specific number. So
  537. 20:57you're not risking 3% per each capital
  538. 21:00anymore. So this is kind of invalid now.
  539. 21:03But I'm doing it because I just want to
  540. 21:05show you the actual equity curve that
  541. 21:07we're getting now because I want to show
  542. 21:08you how you can beat the market returns
  543. 21:11at the end. But in an actual production
  544. 21:13ready, you want to use another formula.
  545. 21:15You want to increase maybe this number
  546. 21:17or even use some other formula for
  547. 21:19position sizing such as the Kelly
  548. 21:21criteria. But that's a topic for another
  549. 21:23video. So let's go back and rerun this
  550. 21:26again.
  551. 21:28Now, by the way, we're getting this
  552. 21:30error because even though the strategy
  553. 21:32is using the bigger time frame, which is
  554. 21:334 hours, I didn't define it here. You
  555. 21:37see, I should have added a data route
  556. 21:39and chosen the same
  557. 21:42route. Sorry, symbol and then it should
  558. 21:45have worked. But I didn't do that. And
  559. 21:47in back test, that's fine. Jess, it will
  560. 21:49give you an error, but it will take care
  561. 21:51of it behind the scenes and the back
  562. 21:52test will go through. But if you don't
  563. 21:55do this in a live trading session,
  564. 21:57you're going to face an issue and your
  565. 21:58session will get terminated. So be
  566. 22:00careful with that. All right. So let's
  567. 22:02remove this. And now the equity curve
  568. 22:04looks significantly better. Right? We
  569. 22:06are closer to beating the market. But
  570. 22:08we're still not fully beating it. Of
  571. 22:10course, I could simply add to the size
  572. 22:11of my position more, especially because
  573. 22:13it has still a very low max draw down,
  574. 22:16but this isn't a realistic number in all
  575. 22:19the periods, right? So I'm not going to
  576. 22:21do that. All right. So first I want to
  577. 22:23show you the result of the back test on
  578. 22:25other periods. So let's change this to
  579. 22:28beginning of 2022 up until 2023
  580. 22:32and the first day of that year. Let's
  581. 22:36run this one more time. I'm going to do
  582. 22:392023 up until 2024 and one more time on
  583. 22:432024 till 2025
  584. 22:46and 2025 until
  585. 22:51almost the 8th month which is just a few
  586. 22:53days ago. So let's run the whole thing.
  587. 22:56All right. So in 2022 you see we are
  588. 22:59ending with losing a little bit by 9%.
  589. 23:03The max draw is minus 22% and the win
  590. 23:07rate is 75%. So even though it's not
  591. 23:10horrible, we're ending with a loss. So
  592. 23:13that's the first lesson that even though
  593. 23:15it has a win rate of 100% in 2025 and
  594. 23:192024, it doesn't mean it's always
  595. 23:22profitable. In 2023, which you can see
  596. 23:24the date here, by the way, we're ending
  597. 23:27with 97% profit. The max thon is minus
  598. 23:3012% and the win rate is still 100%. So
  599. 23:34it looks really great for these three
  600. 23:37years but not so great here but it's not
  601. 23:39bad either. So let's do one interesting
  602. 23:41thing. I want to run the back test
  603. 23:43starting this date until this ending
  604. 23:46date. Right? So we can see the whole
  605. 23:48equity curve.
  606. 23:51So let's run it.
  607. 24:00All right, there we go. So now if we
  608. 24:02were trading this strategy in the past
  609. 24:043.5 years, we would have beaten the
  610. 24:08market, right? Not only with the ending
  611. 24:10P&L, but the max draw down is minus 22%.
  612. 24:14Which is significantly lower than the
  613. 24:16max draw down that BTC had in this
  614. 24:19period. We ended with 176% in P&L. The
  615. 24:22win note is 96% which is absolutely
  616. 24:25amazing. The sharp ratio is 1.47
  617. 24:29which is a realistic number and very
  618. 24:32good. And the average holding time would
  619. 24:34have been 33 hours. But actually I think
  620. 24:36we made a mistake because if we check
  621. 24:39out here it's only taking long
  622. 24:41positions. So no short positions. So
  623. 24:43let's go back to the code and see why
  624. 24:45that is the case. All right. So here for
  625. 24:47the trend value when it is an uptrend
  626. 24:51I'm returning one but when it's a
  627. 24:53downtrend I'm returning minus one. So
  628. 24:56that was my mistake. So let's go back
  629. 24:59and rerun this one more time. And I'm
  630. 25:01also going to rerun this one for 2022.
  631. 25:07There we go. Now we are beating the
  632. 25:09market and making a profit. So we're
  633. 25:13ending with 16% in profit. And in this
  634. 25:16one we are beating the market even
  635. 25:18further. So we ended with 253%
  636. 25:22and the max ardon is still minus 22%.
  637. 25:25Which means the sharp is now better. It
  638. 25:27is 1.57.
  639. 25:29But the win note is slightly lower. It
  640. 25:31is 94.59.
  641. 25:34All right. So this is actually great.
  642. 25:36But there's one problem with this
  643. 25:38strategy. You see, I don't like this
  644. 25:40total closed trade number because over
  645. 25:433.5 years, it only took 37 trades. And I
  646. 25:48think that's very low because the bigger
  647. 25:50the number of trades that the strategy
  648. 25:51is taking during a period, the higher
  649. 25:54the statistical significance of that
  650. 25:57strategy. In simple words, it means you
  651. 25:59can rely more on the result of the back
  652. 26:02test that is in front of you. And 37 for
  653. 26:053.5 years isn't good enough for me. And
  654. 26:08that brings me to the next good thing
  655. 26:10about the strategy that I found, which
  656. 26:12is it doesn't work just with BTC. It
  657. 26:15also works well with other coins. So,
  658. 26:18let's go back and I'm going to add some
  659. 26:21more trading routes. So, I'm going to
  660. 26:22add ETH
  661. 26:24again on the hourly
  662. 26:28and I'm going to add
  663. 26:31Doge again on the hourly. And by the
  664. 26:34way, let's also change this to be mean
  665. 26:37the version RSI. Now, this is enough for
  666. 26:39me for now, but you can find others by
  667. 26:42yourself. And now, if you run it again,
  668. 26:45we're going to get an error because the
  669. 26:47fast mode doesn't work when you are
  670. 26:49trading multiple trading routes, right?
  671. 26:51So, we need to disable this. And I'm
  672. 26:53also going to disable the benchmark so
  673. 26:55we can see the equity chart better. So,
  674. 26:57let's run this one more time.
  675. 27:00Voila, we have something with bigger
  676. 27:02number of trades. So it is 75 now. And
  677. 27:06look at this equity curve. It's
  678. 27:08beautiful.
  679. 27:10Now the P&L is 813%.
  680. 27:14The max RON is still minus 22%. So it
  681. 27:18didn't even get worse.
  682. 27:21And the win rate is 92%.
  683. 27:24And the average holding time is a little
  684. 27:27bit higher. Now by the way, you need to
  685. 27:28be careful with this cuz it is near to
  686. 27:3170 hours, right? So that's a few days.
  687. 27:34So if you trade this strategy at any
  688. 27:36point, you're going to have to hold the
  689. 27:38position for days maybe in a loss and
  690. 27:41psychologically that's going to be
  691. 27:42difficult. So that's something to be
  692. 27:44aware of. And then the sharp is now 1.95
  693. 27:48which is amazing. The winning streak is
  694. 27:5132 and the losing strike is two. Right.
  695. 27:54Overall, I think this looks great.
  696. 27:56Probably the best strategy I've ever
  697. 27:58made so far. And let's not forget that
  698. 28:01I'm still trading only three assets. We
  699. 28:04could continue this for some other
  700. 28:05assets and find some good ones and
  701. 28:07improve the results even further. Now,
  702. 28:09as always, I am going to submit both the
  703. 28:12code and the result of the back test of
  704. 28:14the strategy on other time frames or
  705. 28:16symbols on our strategies page which you
  706. 28:19can check it out. And if you already
  707. 28:21have a free account, you can also
  708. 28:22download the code of the strategy
  709. 28:24assuming it's a free strategy like this
  710. 28:26one that I just shared. Now guys, a lot
  711. 28:28of effort goes into research before
  712. 28:30making a video like this. So if you want
  713. 28:32me to continue making them, please
  714. 28:34support me in any way that you can.
  715. 28:36Liking the video, posting a comment with
  716. 28:38your opinion, and also subscribing to
  717. 28:40the channel helps me a lot. So thank you
  718. 28:42so much for your continued support. I'll
  719. 28:44see you in the next one.
  720. 28:48[Music]
  721. 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.