A ChatGPT way of trading strategy
Today we are going to learn a very simple yet power trading strategy which I created using chatGPT (obviously with some tweaks).
This strategy is a moving average crossover of two magical numbers 20 and 45. I will also share the backtesting result of this strategy with all the required rules.
So without any delay lets get started.
Steps to run the Pine Script
- Lets login to openAI website and open chatGPT. Post that we will ask a simple question.
create a 20 and 45 EMA crossover strategy in pine script and also plot these moving average
- ChatGPT will give you the entire strategy in pine script which can easily be copy pasted in TradingView. (Just create an account in TradingView, if not already created, and open charts. In the bottom bar you can easily view the PineScript terminal)
- Click on open and new strategy and copy paste the above code generated by chatGPT.
- As we know chatGPT is a bit old and data train is not updated, PineScript Version 5 has some new changes which are not incorporated in the above code. Thus few minor changes are required.
1. In the commented lines below "Define EMAs" - Change "ema" to "ta.ema".
2. In the commented lines below "Define Buy and Sell Conditions" - Change "crossver" to "ta.crossover"
3. In the commented lines below "Define Inputs" - Delete "type=input.integer,"
- Do the above changes for both the lines. (check the above screenshot for reference)
- Once the above changes are made, code would be compilable and you can easily save the code, give the strategy a name and ‘add to charts’.
- Here we go the pine script is ready and you have created your strategy in TradingView.
- There would corresponding buy and sell signals generated and visible on the charts.
- Below is the screenshot of the chart on it would look.
Strategy Description
- This strategy is a based on the moving average crossover.
- When the slow moving average (20 EMA) is below fast moving average (45 EMA) and it crosses up the fast moving average from below, thats a Buy signal.
- When the slow moving average (20 EMA) is above fast moving average (45 EMA) and it crosses down the fast moving average from above, thats a Sell signal.
- Once Buy or Sell signal is trigger, simple way to exit the trade is when the opposite trade happens.
- For Example — If the 20 EMA crosses 40EMA from below, then its a buy signal and order is executed and you entered the trade. Now until 20 EMA is above 45 EMA you stay in the trade. Once the trend reverses and 20 EMA breaks 45 EMA, thats the place where your trade ends and you exit your position and then take a Sell side position at the same time.
- Thus you will always be inside one trade, either on buy side or sell side.
- Below is the backtested data of the same strategy which we discussed above.
- The same can be found in Strategy Tester column in the bottom bar of charts screen.
- We can see this a quite profitable strategy which has a success ratio of 41% and profit factor of 1.8
- You could have easily made 430% return in 3 years with this strategy by investing 1.5 lacs rupees and taking 1 lot (25 qty) in BankNifty at a time.
- Above screenshot shows the entire details of all the trades in BankNifty futures. This Strategy is based on 1hr time frame and chart used is of 1hr.
That should be it for this article. Feel free to reach out for any clarifications or doubt.
If you visited my article for the first time, do follow me and share this to your friends. I will be bringing more new and interesting articles in coming days.
Until then………