NIFTY 50 Trading Strategies with ChatGPT

In a recent exploration, a YouTube channel delved into the exciting possibility of leveraging ChatGPT to develop and backtest ten distinct trading strategies for the NIFTY 50 index. The goal was to identify which of these AI-generated strategies could prove most profitable based on historical data using TradingView’s Pine Script editor.

The process involved a consistent workflow: first, asking ChatGPT for strategy ideas, then requesting the Pine Script code for each, manually correcting any errors or outdated syntax in the code, running backtests on TradingView, and finally, meticulously recording the profitability results. Interestingly, ChatGPT often required some “education” or correction to align its code with Pine Script’s version 5 syntax and available libraries, such as the TA (technical analysis) library.

Here’s a breakdown of each strategy, the prompts used, and their backtesting performance:

 

The Prompts are always like: Create the pine script 5 for a strategy which is using moving average crossover for entries and exits”.

These are my findings and learnings during the process of trading strategy creation.

Moving Average Crossover

The Strategy: This is a fundamental trend-following strategy using a fast and a slow simple moving average (SMA). A buy signal is generated when the fast SMA (blue line) crosses above the slow SMA (red line) from below. A sell signal occurs when the fast SMA crosses below the slow SMA from above.

Corrections Needed: ChatGPT initially provided code using “study” instead of “strategy” and didn’t always correctly use the TA library functions (e.g., TA.SMA and TA.crossover), requiring manual adjustments. Plotting for the moving averages also had to be added.

Result: This simple strategy yielded a 120.95% profitability.

Bollinger Bands

The Strategy: Bollinger Bands are a technical indicator designed to measure market volatility. They consist of two lines plotted two standard deviations away from a moving average, helping to identify when prices might be oversold or overbought. The strategy suggested buying when the price reached the lower band and selling when it touched the upper band.

Corrections Needed: The code from ChatGPT contained a function cross above which isn’t valid in Pine Script version 5; it needed to be corrected to crossover or crossunder.

Result: This strategy showed a 73% profitability.

RSI Divergence Strategy

The Strategy: This strategy utilizes the Relative Strength Index (RSI), a momentum oscillator. The video adjusted the overbought level to 65 and the oversold level to 40.

Corrections Needed: Similar to previous strategies, the TA library needed to be explicitly used for crossover and crossunder functions. Furthermore, some of the logical conditions related to RSI levels had to be refined, and the overlay option was removed.

Result: A promising 111% profitability was achieved.

Trend Following Strategy

The Strategy: This is a straightforward strategy based on a 50-period Simple Moving Average (SMA) as a baseline. The rule is to buy when the price crosses above the SMA (going into a long position) and sell when the price crosses below it from above.

Corrections Needed: The TA library for SMA needed to be applied.

Result: This strategy was the standout performer, achieving a remarkable 217% profitability straight out of the box, without any complex modifications.

MACD Strategy

The Strategy: MACD (Moving Average Convergence Divergence) is a trend-following momentum indicator. The strategy involves buying when the MACD line crosses above the signal line from below, and selling when it crosses below from above.

Corrections Needed: ChatGPT generated code using an older Pine Script version for MACD, requiring an update to include the three standard elements: MACD line, signal line, and histogram line. Additionally, the overlay setting needed to be set to false to display the MACD as a separate indicator.

Result: The MACD strategy demonstrated a strong 177% profitability.

Fibonacci Retracement

The Strategy: Fibonacci retracement levels are considered a “golden standard” in trading, defining potential support and resistance areas. The strategy in the video focused on two key levels: 0.618 as a potential buy signal and 0.382 as an oversold/sell level. Buying occurs when crossing the 0.6 level, and selling when crossing the 0.3 level.

Corrections Needed: A significant issue here was ChatGPT’s use of TA.Fibonacci, a function that does not exist in Pine Script (it exists in Python libraries). This required rewriting the code to manually calculate and plot these Fibonacci levels.

Result: Despite the manual intervention, this strategy achieved 170% profitability. Trades occurred less frequently but were generally profitable, suggesting strong signals.

Breakout Strategy

The Strategy: This strategy involves identifying a “channel” based on the highest and lowest prices over a specific lookback period. A buy signal is generated when the price breaks above the upper channel line, and a sell signal occurs when it breaks below the lower channel line.

Corrections Needed: ChatGPT used a non-existent channel library. The code had to be manually rewritten to define the channel_up (highest close price) and channel_bottom (lowest close price) for a given lookback period. Initial parameters suggested by ChatGPT (20-day highest price from 10 days ago) were adjusted to a 1-day lookback for better performance.

Result: Initially, it showed 81% profitability, but with optimization (changing to a 1-day lookback), it increased significantly to 165%.

Candlestick Patterns

The Strategy: This strategy attempts to identify bullish and bearish signals based on specific candlestick formations by comparing opening and closing prices within a bar.

Corrections Needed: ChatGPT incorrectly tried to use TA.CDL3WhiteSoldiers, which is a function found in Python libraries, not Pine Script. The user had to specifically request ChatGPT to use direct comparisons of closing and opening prices to define patterns.

Result: This strategy showed the lowest profitability among the initially successful ones at 27%. The video noted that candlestick patterns might not be well-suited for highly volatile markets like Nifty without more specific research.

Volume Spike Level

The Strategy: This is a momentum-based strategy that suggests buying when trading volume surpasses a certain threshold, typically defined by a simple moving average of volume. The idea is to follow large buyers during volume spikes.

Corrections Needed: No explicit code corrections were mentioned beyond the possibility of customizing the volume threshold.

Result: This strategy performed poorly, yielding only 15% profitability. The video suggested it might not be a proper way to define buy/sell signals on its own.

Mean Reversion

The Strategy: This strategy is described as a basic crossover strategy using SMA, where a buy signal is generated when the closing price is below the moving average, and a sell signal occurs when the closing price is going below the moving average. The speaker found this strategy to be “random” and “stupid” in its logic.

Corrections Needed: Minor code corrections from ChatGPT were required.

Result: As anticipated, this strategy showed almost no profitability, at a mere 3.8%.

 Conclusion

The experiment demonstrated that ChatGPT can indeed be a valuable tool for generating trading strategy ideas and initial Pine Script code. However, it’s crucial to understand the nuances of Pine Script (especially version 5) and be prepared to make significant manual corrections due to ChatGPT sometimes mixing up programming languages (Python vs. Pine Script) or using outdated functions.

Out of the ten strategies tested for Nifty 50, the Trend Following strategy emerged as the clear winner, boasting an impressive 217% profitability without any manual customization of its core logic. Most other strategies also showed positive, often substantial, profitability after corrections. This highlights the potential of AI-assisted strategy development, but also underscores the necessity of human expertise for refinement, error correction, and, importantly, incorporating proper risk management (like stop-loss levels) which wasn’t the focus of these basic tests.

 

Not Financial Advice

This article is for informational purposes only and does not constitute financial advice. Always conduct your own research before making any investment decisions.

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

We get it, ads can be a pain!

But here\'s the thing: we provide all our trading insights and content to you completely free of charge.

To keep it that way, we rely on the support from our advertisers. So, if you find our content valuable, please consider playing fair and disabling your ad blocker for our site. It helps us keep the lights on and continue bringing you the best trading information. Thanks for your understanding!

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock