GPT: Triple moving average strategy in Python makes 4983%
About this lesson
In this video, you'll see a Python trading strategy that incorporates the TEMA, CMO, and ADX indicators. It achieved a 4983% PNL in its final backtest. Rather than writing the code completely from the beginning, I will initially show the entry rules on TradingView. Following that, our own GPT will be used to convert these into Python code, allowing us to perform a backtest. 👉 JesseGPT (requires sign up): https://jesse.trade/gpt 👉 Join our FREE Discord community: https://jesse.trade/discord 👉 Apex signup URL for fee discounts: https://jesse.trade/apex 👉 Bybit signup URL for fee discounts: https://jesse.trade/bybit 👉 Explore our strategy listings: https://jesse.trade/strategies
Full Transcript
Hey guys, it's So today I'm going to show you a strategy which is super simple and yet the result of it is pretty great. It's a trend following strategy, but you could kind of say it's a mini version because we're going to use limit order to open the position at a pullback. Now, I'm not going to write the code of the strategy myself. Instead, I will be using GPT to convert the logic of the strategy. So that means the entry and the exit rules of it into Python code so we can run some back test on it and see some charts and numbers. So you're going to see that even if your Python knowledge isn't great, you can get started. So if that sounds good, let's get to it. The main indicator of this strategy is called TEMO, which is stands for triple exponential moving average. So let's look it up on Trading View and add two of it. Change the period of them to be 10 and 80. Now the entry rule is very simple. When this orange line which is temo with the period of 10 goes above the purple line which is temo with the period of 80, we consider that an uptrend. When it's vice versa, we consider it a downtrend. But first I want to show you the difference between tema indicator and the good old EMA. So let's disable the one with the period of 80 for a second. Add a regular EMA. change its period to equal the same which is 10. All right. So this orange line here is tema with the period of 10 and the blue one is EMA with the period of 10. So they have the same period but let's see how they behave. So for instance in here you see when the price starts to go up the TMA indicator which again is this orange line it starts to go up way faster. And for example, in here where the price starts to go down, you can see the Tema line goes down way faster. Now to summarize it, you know the issue with moving averages, which is their lagging indicators. Well, if you use Tema instead of the good old EMA, you're going to have less lag. Or in other words, it moves significantly faster. All right, so let's change this back to what it was. bring back the ter with the period of 80 to have our inter rule. Now the second rule of the strategy is that we don't want to just look into the short-term trend. We also want to look at the long-term trend. Now for that I'm going to use the same indicator except with values of 20 and 70 but on the bigger time frame specifically I want to use the 4our time frame. Now before I forget the trading time frame for this strategy is 30 minutes which is why it is considered a day trading strategy. All right. Next, we want to use a volatility filter. And as most of my strategies, I'm going to use the ADX in here. So, basically, whenever this red line goes above the green line here, which is our threshold, we want to take the trade or for example in here. All right. Next, I want to use some kind of oscillator. And for this strategy, I'm going to use the Shae momentum indicator. So, let's look it up on Trading View. Now it is some kind of oscillator very similar to RSI. But the main difference you should care about is that it moves faster. It is more sensitive to the price change. So just like the TME indicator which was more sensitive to the good old EMA, the Shanda momentum oscillator or CMO for short is also more sensitive compared to a traditional oscillator such as the RSI. And the inter rule is very simple. So the threshold is at 40 and also minus 40. So basically whenever this line is above our threshold we want to look for long trades and whenever it is below minus 40 we want to look for short trades. All right. So now we have all the entry rules of the strategy. And as for the position sizing as always I'm going to risk 3% of my capital per each trade. But for the entry price what I want to do is I want to use a limit order because I want to buy at a pullback. So I'm going to keep it simple. I will buy at 180R below the current price for a long position and do the opposite for a short position. All right. So I think now is a good time to actually go and code this thing so we can run some back test. Now to use this service you have two options. One is to use the cloud version which is hosted by us which you need to sign up for free to get started with or you can watch my previous video where I explain how to self-hosted by yourself which is better if you have privacy concerns or let's say you want to use premium models without paying us anything. So choose one of the two but in this video I'm going to use the cloud version as the LLM model. I have selected cloud 3.7 set. You can select whichever you think works best but in my cases cloud usually works best. And as for the instruction here it is. So right trend following strategy the short-term trend must be one representing an uptrend. To determine this use the tema indicator with a period of 10 which should be above the temo with a period of 80. If it's vice versa the trend is down. So you know basically just what I showed you. For the long-term trend, use the same indicator over the period of 20 and 70. So, let's also ask it to use the 4 hours time frame except to use the 4 hours time frame. For volatility filter, use the ADX indicator. Its values must be above 40 to take any trades. Also, use the CH momentum indicator as an oscalator. For long trades, it value must be above 40. For short trades, do the exact opposite of the above conditions. For position sizing, risk 3% of the available margin per trade. For the entry price of the trades, use a limit order ATR below the entry price. For the stop loss, you use four times of the current ATR below the entry price. For exiting the trade, the stop loss must be as described above. And the takerit must be three times the ATR indicator. And that's it. So, I just told it basically what I need, just the entry and exit rules. So, let's hit enter and see how it writes the code. All right. So, here it is. This is the short-term trend. It's using Tema with a period of 10 and 80. It's returning either one or minus one. For long-term trend, it is using the first time frame and periods of 20 and 70, which is exactly what I ask for. Then need to find the ATR, the ADX, the CMO, and for shoot long, which is where we specify the inter rules of the strategy. For short positions, it's saying the short-term trend must be one, the long-term trend must be one, the ADX must be above 40, and so should the CMO. For uh for short positions, it's saying that the trends must be minus one, the ADX must be above 40, the CMO must be below minus 40. For position sizing, the entry price is a limit order one ATR below the current price. This is correct. The stop loss is four times of the current ATR minus the entry price. It is risking 3% using the riskto quantity utility function of Jesse to make sure we never lose more than 3% of our total capital per each trade. By the way, this doesn't mean to close the position when it goes 3% against us. It closes it when we are losing 3% of our total capital. It's a bit different. And then it is submitting the order using a buy order. The first parameter is the quantity of it and the second is the price. Now for short trades, it is doing the opposite which is correct. Now this one also must be here and return true. Now what it does is because we're using limit orders. What would happen if we submit the order but it doesn't get filled during that candle? Well, when the next candle comes, we want to cancel the previous one and submit a new order using the current conditions such as the current ATR value. And when the position opens, it submits a stop loss like this. Four times the current ATR minus the enterprise of the position. And it is also using the quantity of the current position for the stop loss. For the take profit, it's doing exactly the same except three times the ATR. Also, instead of subtracting the price, it is adding it. And for a short position, it's doing exactly the opposite. So, this is perfect. So, let's go up here and copy the whole thing. Go to J's dashboard and go to new strategy. I'm going to call it tema trend following. Let's paste the whole thing here. Go to back testing page. The exchange is Binance perpetual futures. The route BTC UCT is correct. The trading time frame must be 30 minutes. Here, let's choose tema trend following. We also have one data route because we're using the bigger time frame which is 4 hours with the same symbol. And let's begin by back testing the first six month of 2025. Let's enable the interactive charts, the fast mode, and the benchmark feature. So, let's run it. And there it is. So, let's scroll down. The P&L is 32%. The maxon is minus 3%, which is crazy. The win rate is 81%. The average holding time is 12 hours. The sharp ratio is 4.34. Now, by the way, this is a bit too good to be true. Like, yes, we're getting it on 2025, but I don't expect it to be this good in all the periods. And I'm going to back test it on other periods just in a second. Let's take a look at the interactive charts. So, we can see the type of trades that it took. So, for instance, it opens it here and closes it here. It opens it here and closes it here. We can also see the trades here. And let's see there's one that I really want to see like this one. I can just click on it and it will take me to it. Right? So this is where we sold and this is where we close our short with a loss. Then again we have here and if you want to see the details of any trade by the way click on it and you can see the orders. And then if you click on just any of them, you can also see other metrics such as the quantity of that trade, the entry price, exit price, the fees that we paid, and the P&L. But actually something really helpful with these strategies is to see the indicator values when we are taking the trade. So let's go back to Jessie GPT and simply ask the AI to add the values for us. I need you to add values for the charts so I can debug the strategy. All right. So it added this after function here which will do just that. So let's copy the whole thing. Go to this is dashboard. Press new session. Click here to see the code of the strategy and paste the whole thing here. Now, by the way, this watch list that it added is really helpful, but only if you are trading the strategy live. So, it's not really for back testing. So, I'm going to remove this. And let's go back here and click the back test one more time. Let's go to view charts. And now, not only we can see the trades, we can also see the demo values. So, 10 and 80 periods. We can also see the ADX. So for example when it opened this long position we can see the ADX value is indeed above the threshold. So it was at 50 and the value for CMO was also above 40. So let's take a look at the short trade like this one. So again the ADX is above 40 and the value for CMO is above minus 40. All right. So now let's back test it on other periods. In fact, I'm going to back test it as early as 2021, which is the amount of candles that I have up until the 6 month of 2025. Now, to make sure the back test will go faster, I'm going to disable this line here, which is for charts. Go back here. And by the way, if you don't want to use the editor inside Jess's dashboard, you can always use something such as the VS code and look up your strategy. So for instance, tema trend following and you can see the code here and you can edit it right from here instead of the dashboard. All right, so everything looks good. Now let's start it. Now while this is going, I want to also run another one without this benchmark value here. Let's run it. So, while that's going, I want to quickly remind you guys about Apex Omni, which is my favorite deck made by the team behind Bybit. It has low trading fees, instant order execution, and of course, no KYC whatsoever. If you're going to check them out, please consider using our link, which not only will give you trading fee discounts, but it will also support me in order to make more videos like this. And there we go. So, by the way, this max draw down here is really awesome. It allows me to easily add to the size of my position. In fact, let's go here and increase the size of the position with a number such as three. And also do the same thing in my short positions. So, and if I go to the benchmark page, I can just rerun both of them simultaneously, which is really cool. And you know what? While this is going, let's also duplicate this one. Go to here and back test each year separately. So, starting 2021 up until 2022. Again, let's enable benchmark. Disable interactive charts started. Let's do the same since the beginning of 2022 up until 2023. 2023 up until 2024. and since the beginning of 2025 up until the sixth month. All right. So this is for 2021. We are clearly beating the market. The draw down is minus 20%, which is great. The P&L is 167%. We paid this much in fees. So that means if you started with $10,000, you would have ended with 26,700. Now the win to loss ratio is 0.65 and that is because for our take profit we're using three times of the ATR and for stop loss we're using four times. But on the other hand, the win rate is 71%. So you could say it's more similar to a mean reversion rather than a typical trend following. The average holding time is 44 hours. The sharp ratio is 1.98 which is really great. Let's look at 2022. So here's the equity curve. We ended with 35% P&L. The max return is minus 30%. Actually let's just look at the equity curves. So this is for 2023. We didn't beat the market. I mean in the beginning we did but in the end we didn't. And this is for 2025, which I mean this is just off the charts. Now, let's also compare them from the benchmark page. Oh, I forgot 2024. So, let's also back this one. All right, there we go. So, this is also really good. So, we made 300% P&L. The max done is minus 25%. The winner is 75% and the win to loss ratio is still 63. The sharp ratio is near three which is again off the charts. So let's also compare them inside the benchmarking page. So here are the P&L. They're all positive. The maxon is also really good. On our best year it was - 9.2%. On our worst year it was minus 37%. The sharp at the worst year it is 0.8. At the best year it is 4.3 and the annual return is on average above 100% for sure. All right. So this looks really great. Also let's take a look at the equity curve of the whole 4.42 years. So this is it and this is if we consider the price of BTC itself. So you see as soon as here we are beating the market and then after that we were always in profit. But here's the thing some people only care about the ending number the P&L. They read this and they're like wow close to 5,000%. So that means this strategy must be making money for me all the time. Well that's not true. So if you take a closer look at this equity curve, yes we ended here which is amazing but we had so many draw down periods. So for example in here this is 1 2 3 4 5 6 7 8 9 10. So you see for about eight or nine months I didn't calculate it exactly we were in a draw down period. So that means if we started trading the strategy let's say from here up until this moment we would have been in a loss or if we started here we would have been in a loss for two or 3 months. Now in here we didn't lose much. You could say this is a range but still this took one two three four five six seven for about seven or eight months our equity curve would have been flat. So trust me when I say if you were trading in this live you would not have been happy during this period. In fact most of us wouldn't have continued trading it and would have never seen this huge jump in our equity. Now in here it was really great. So no complaints. It also did pretty well here and here. So yeah, most of the times it's making money. It's going up and that's really great. But there are draw down periods. And I really want to emphasize on this that if you are going to trade a strategy like this, you shouldn't trade all your capital just with this one single strategy. In fact, you want to have a basket of strategies like this one. So that while one of them is going through a draw down period like in this case for example, your other strategy is making money during the same period and they're going to offset each other. In fact, maybe even instead of two strategies, you could run 10 of them simultaneously. Now this is a big topic that I am going to talk about more maybe in a separate video but I just want to emphasize that just don't only look at the ending number especially P&L because at least you want to consider the risk and for that read another number such as the sharp ratio or calma ratio. I'm going to submit the results of this strategy and the code on our website's strategy page where you can find the results of this strategy on different symbols and time frames if you are curious about that. Also, you can find my other strategies and their metrics to maybe create your own basket of strategies as I just described. Now, before I leave, we're going to have a giveaway. A random person who likes this video, post a comment, and subscribes to the channel is going to win 1 million bunk token. All right, let's pick the winner for the previous video. And the winner is Malik. Sorry I cannot pronounce your whole name. Thank you so much for your comment. Please reach out to me so that I can send you your bunk tokens. Thanks for watching and I'll see you in the next one. [Music]
Original Description
In this video, you'll see a Python trading strategy that incorporates the TEMA, CMO, and ADX indicators. It achieved a 4983% PNL in its final backtest. Rather than writing the code completely from the beginning, I will initially show the entry rules on TradingView. Following that, our own GPT will be used to convert these into Python code, allowing us to perform a backtest.
👉 JesseGPT (requires sign up):
https://jesse.trade/gpt
👉 Join our FREE Discord community:
https://jesse.trade/discord
👉 Apex signup URL for fee discounts:
https://jesse.trade/apex
👉 Bybit signup URL for fee discounts:
https://jesse.trade/bybit
👉 Explore our strategy listings:
https://jesse.trade/strategies
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Related Reads
🎓
Tutor Explanation
DeepCamp AI