ChatGPT o1: Turtle Strategy in Python Makes 319%

Algo-trading with Saleh · Intermediate ·🧠 Large Language Models ·1y ago

About this lesson

In this video, I demonstrate how to use the #ChatGPT #O1 model to write and #backtest the #Turtle Trading #Strategy in #Python, yielding an incredible 319% profit! Watch as I walk you through step-by-step, from coding the strategy to implementing a backtest that delivers real results. Join our FREE Discord: https://jesse.trade/discord Strategies listings: https://jesse.trade/strategies

Full Transcript

[Music] hey guys it's Al today I'm going to code the turtle strategy using chat gp01 preview model to guide me for my entry and exit rules it's going to be a fun experiment not only that it actually taught me about this one new indicator that I didn't know before and it improved the results of this strategy and my previous ones all right let's get done it so I simply asked chat GPT how does the turtle strategy work explain to me the entry and exit rules and how to set my stop loss and take profit so in case you don't know the turtle trading system is a classic Trend following system developed in the 1980s by legendary Traders Richard Dennis and William eart it was part of an experiment to determine whether great Traders are born or made the strategy focuses on entering trades when a strong trend is identified and employs specific rules for entry exit position sizing and risk management okay what's my entry role Buy Signal enter a long position when the price exceeds the highest high of the past 20 days okay and for sell signal inter is short position when the price Falls below the lowest low of the past 20 days so basically this is the definition of the donin channel indicator in case you didn't know that's exactly what it does what else it's also saying no re-entry rule If the previous breakout would have resulted in a profitable trade skip the next breakout signal to avoid potential false signals and as for position sizing saying to use the ATR which is what I always do for position sizing risk one or 2% I usually do risk 3% that's fine for exit rule is saying when the price Falls below the lowest low of the past 10 days system to exit when the price Falls below the lowest low of the past 20 days as for take profit saying use a trailing stop adj just a stop- loss order as the order becomes profitable maintaining the two and distance from the highest price achieved for long positions or the lowest price for short positions the position is closed when the exit breakout levels are breached okay so let's go to Jesse and create a new strategy and call it Turtle I because we're using AI for our rules I'm going to also include the name AI in it now let's go to my Pie charm editor and open that so first let me show you how the donchain channel look like on trading view so let's look it up and by default the period is set to 20 which is what chat gbt was also telling us all right so you see when a new high is achieved the upper band goes up so basically the lower band is the low of the past 20 candles and the upper band is the high of the past 20 candles so let's define a new property and call it D chin and I'm going to say return ta dungeon and going to pass the current candles the period is by default 20 so we don't really have to set it but let's just do it anyways and but there's one thing I want to change so for example here we had a breakout right but if you see the upper band is also going up and that's because again it's just simply the highest of the past 20 candles right so if if the new candles is making a new high the value for done chain will also go up and that's not really great it's going to make it a bit difficult for us so instead of using this value I want to use the value of the previous scandle now we can do this in two ways in jessu the first one is that we pass the sequential parameter and set it to true so that this way in instead of giving us one single value is going to give us an array of values and then we're going to use not just the last value but the one before that but we could also do it in another way which is to Simply pass not just all the candles but we could simply not pass the last candle so I'm going to say this and here it is so we're saying give me all the candles up until the last one so it's not going to include the last one and by the way if I wanted to select all the candles of this nonay array I would have done it like this or simply just not pass it at all but now I'm going to do it like this all right so there's that and for our rule as J GPT told us we're going to say if the current price is more than the upper band of the current dchain Channel that's it and for our position sizing we're going to use the current price as our entry so it's going to be like this and as for the stop we're going to use the current price minus the ATR multiplied by 2.5 which works best for me and for our quantity of of the position is going to be utils do risk to quantity our capital is going to be self available margin the risk number is going to be three as for entry price let's say entry and then the Stop and the fee rate is going to be the current fee of the exchange that I'm trading now let's submit the buy order which would be self FL buy equals quantity and then the entry price now for our short position we're going to do the opposite all right now the shoot cancel order it doesn't really matter what we enter here because we're using a market order to enter our positions if you were using a limit order or a sub order the value for this would have mattered but now it doesn't for the subplus I want to do it as soon as the position is open so I'm going to say onopen position if it's a long position my stoploss order is going going to be the quantity of the current position and the price of it just like what we did here it's going to be the current price minus 2.5 times of the current ATR value now we're going to do the opposite for a short position and when I say opposite I mean instead of subtracting the ATR we are adding it all right so this is good now what about our take profit I'm going to do what chat gpg suggested here which is to use the trailing Stu and I actually did try other methods but they didn't really work well and I'm not going to waste your time with it so I will just write this one so I want it to be a trailing stop what does it mean we are already submitting the subplus order when the position opens so now whenever a new candle closes we want to update our suplus right so we will use the update position method which will get executed whenever a new candle is closed so I'm going to say if it's a long position again update my suplus with the current quantity and the price instead of simply giving it the new price well if I do it okay let me show you so if I do this like this and simply update my St plus whenever a new candle closes well the problem is that what if the price is not going above what if we not in profit we're actually sitting in a loss if we update our stplus in that case well the subplus just keep is going down and it never gets hit and that's actually horrible so we want to update our stplus whenever the new price of the subplus is actually better than the previous price so for a long position that would be the maximum price of the current average suplus price which in our case would be this or the new stop loss price which would be this and for a short position we're going to do the opposite so we're going to say the minimum of the current average stop loss or this new suplus price okay so again this is only going to submit it if the price is going in our favor if we are sitting in profit and we for example for a long position we want to move up this stplus price okay and wice Versa for a short position so this looks complete and we can start running it and seeing the results okay so let's go to Jesse to the back testing change the strategy to tal a I and the time frame is set to 1 hour the symbol is BTC UCT the starting point is 2022 up until 2024 all right so the fast mode and Benchmark is also enabled so let's run it okay so these are the results now for the first run is actually not bad especially during the bare Market it was performing really well the max ston is minus 43% which isn't great okay the V is 30% so at this point I went back to chat gbt and I simply said I Cod it as a strategy and it doesn't always work great what other indicators can I use to improve it because as you know guys using simply one indicator never yields the best results we always want to combine at least a couple of them to filter out bad trades so that the volatility would come down the max would come down so I asked this and it gave me this suggestions to use this a simple moving average to use the RSI which I did and it performed horribly to use the macd which again didn't work for me use the adx which always work fine for me at least for train following strategies use the Ballinger bands which didn't help me use the obv support and resistance levels which aren't really that useful in algo trading strategy and that's almost it now so as I start coding these this the moving average actually worked well now I want to use the moving average on a bigger time frame so first let's define a new property and call it long-term candles and in it I'm going to return self get candles and pass the current exchange the current symbol and the 4 hours time frame now let's define another one and call it longterm moving average and I'm going to return T SMA of the long-term candles so instead of passing the current candles I passed long-term candles right and as the period I'm going to use 200 all right so now I'm going to come down and say I want to open long positions when the current price is above the long-term moving average and for shoot short I will do the opposite so let's go back and run it again all right so now we're definitely beating the market we ended up with 37% profit the max starton is down to 26 the win rate is 33% okay now by the way in this strategy because it's a trend following and we are trying to write the trend for as long as possible it's perfectly normal for our win rate to be lower than 50% that's perfectly normal for as long as our average win to loss is more than one and it is in here is 2.21 which is amazing all right so there's that another suggestion by chbt was the adx indicator which is what I always use anyways so let's also add that let's define a new property and return whether or not the current adx number is bigger than 30 and for my in Rule now I'm going to Simply say and self. adx and for short I will also do the same let's go back and run it again so while this is going I wanted to quickly remind you guys that we have a Discord Community with more than 3,000 quants such as you and I we hang out there share ideas and help each other out and I would love to see you guys there all right so now the EO cure is much more smooth than what it was before and I really like that and yes the panel also got a bit better the Maxon came down a bit and the win rate actually went up to 38% so let's go back to the strategy because I want to see the Donan Channel values for it so let's define another method called after and say self add line to Candle chart and I want the dine so the title is going to be the upper band and the value self dunge in Upper band and one more for the lower band so let's go back rerun this if I go back now I can see the upper and lower bands of the dungeon channels and one issue that I had with it which after plotting it on the chart I could see is that sometimes like in here there's a breakout it opens a position it closes it but when it does close it because the price is actually below the lower band it immediately opens another trade and I don't want to open a trade here now in this case it's not really that bad but in some cases it was horrible so I wanted to Cote this so that once you close a trade do not take another trade right right in the same candle all right just wait at least one more so for that we have to define a new variable just to hold the last index that we closed our trade so I'm going to call it last closed index and I'm I'm going to set it to zero and we need to update this whenever a trade is closed right so for that I'm going to say unclosed position I need you to update the last Clos index and set it to the current Index right and for my ING rules let's define a new property and call it past time and in it I'm going to say whether or not the current index minus the last close index is more than zero that's it so if it's even one that's okay but if it's zero it means it's in this current candle and just don't take any trades so let's go back and rerun this all right so I forgot to add it to this Shong and shoot short all right so let's go back and run it again all right so you see the pnl actually went up and the max ORD came down just with this one single change and everything else is actually pretty much the same such as the wing rate so that's why the results improved all right so at this point this is the result that I had but I wasn't super happy with it and I wanted to improve it again so here's what I did I went back and ask the same questions but this time not from chat gbt o1 preview but from chity for all model and it gave me pretty much the same result except it suggested this which changed everything for me it said the choppiness index purpose this is a lesser known but powerful indicator to measure Market consolidation choppy versus trending condition conditions and that's exactly what we always want in a trend following strategy right and that's why we use the adx always so it's saying if the choppiness index is high avoid taking breakout trades because the market is likely to be ranging only take trades when the choppiness index is low indicating that the market is in a training phase so let's go back and code it so I'm going to say the let's call it chop and say whether or not the Chop for the current candles are below 40 and come down here and add this as another entry rule go back to Jesse and rerun this and look at that our panl went up the equity Cur looks much better the max Ron came down to minus 133% so this means we can add to our position to get the same Max Ron as before but this time we would get even better panl I'm going to show you that in a minute so the win rate actually went up to 40 % the average win to loss is 2.49 which is absolutely amazing and the sharp is 1.05 all right so why do I say the max Z here is really important now what did we have before actually let's bring this back and go to here and just return true so that this won't be used anymore and run it again to see what numbers we had now here the Max zon was- 20% and the panl was 40 here the max zon isus 133% so that means I can just go to my code and if I return the chop but this time add to my position let's say multiply it by a number such as 1.8 to use leverage in other words and go back and run it again this time when we get approximately the same Max Thro number the p&l instead of 40% is now at 106% which is pretty amazing all right so I really like these numbers and I'm going to submit this strategy for free on our strategy index page so if you want to use source code you can just copy it from there but I also kept working on it and tweak the parameters a bit did some optimization and I was able to improve the results up to this so instead of 100% for a Max ofus 22% I got it up to 39% with a Max Run of minus 18% so it was a huge jump in the results and I'm going to also submit this one as a premium strategy again to our strategy indexing page I hope you guys learned something from this video I know I did especially the choppiness index indicator which I didn't know about before and if you guys want me to create more videos like this one please let me know and by the way I've been working on some other types of strategies such as GRE trading and also trading using Fibonacci lines and the results of them weren't really great so that's why I haven't created a video about them yet but if you guys want me to make videos about experiments that are really fun and something that you might be curious about too but the results just aren't that profitable let me know down in the comments and as always we're going to have a giveaway a random person who likes this video post a comment and subscrib to the channel is going to receive 1 million bnk token let's pick the winner for my previous video and the winner is this was great thank you so much for your comment please reach out to me so that I can send you your bunk tokens thanks for watching guys and I'll see you in the next one [Music]

Original Description

In this video, I demonstrate how to use the #ChatGPT #O1 model to write and #backtest the #Turtle Trading #Strategy in #Python, yielding an incredible 319% profit! Watch as I walk you through step-by-step, from coding the strategy to implementing a backtest that delivers real results. Join our FREE Discord: https://jesse.trade/discord Strategies listings: https://jesse.trade/strategies
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

📰
Stop Judging Every Run: Eval Sampling Is a Budget Decision, Not a Coverage One
Learn to optimize LLM evaluation by treating sampling as a budget decision, not a coverage requirement, to reduce costs without compromising model performance
Dev.to AI
📰
Unlocking Open-Weight LLMs: Seamless API Integration with NovaPointAI
Integrate open-weight LLMs into Dart applications using NovaPointAI's API, eliminating the need for GPU infrastructure management
Dev.to AI
📰
I Built an Entire Application with ChatGPT 5.5. That Isn’t What Changed Me.
Building an app with ChatGPT 5.5 was a breakthrough, but the real change came from learning to think with AI
Medium · ChatGPT
📰
ferrovec: a Tiny Rust HNSW Vector Index That Runs Semantic Search Inside the Browser Tab
Learn how to use ferrovec, a tiny Rust HNSW vector index for semantic search in the browser, and why it matters for serverless applications
Dev.to · AI Explore
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →