How to Backtest a Simple Trading Strategy Using ChatGPT
I used ChatGPT to backtest an EMA + ATR system with stop-loss, slippage, commissions, take-profit, and real P&L tracking.

🧠 This post explores concepts from “The Backtester’s Edge: How Code and AI Transform Your Strategy Game,” a deep dive into how traders can leverage programming and AI tools like ChatGPT to build, test, and refine trading strategies.
Pre-requisites and tools:
- CSV data file with OHLC data. This one was NQ data downloaded from TradingView, but you could use any source.
- ChatGPT-4o.
Prompts
The following prompts are the raw inputs I used, without any polishing, and they are presented as is.
If you’d rather watch/listen than read, here’s the video version of this post:
Prompt 1
I attached the CSV file with stock OHLCV columns, and started the conversation like this:
"Read the file and show me whats inside"

Prompt 2
Explaining my strategy so that it can code it.
Add a 9 period EMA to this.
Add 9 period ATR
When the price closes above 9 EMA, take a long.
When the price closes below 9 EMA, take a short.
Stoploss = 2 ATR
TakeProfit = 5 ATR
If we have an existing position, do not create a new one.

It did something, calculated a lot of variables internally to backtest. However the output is not concise. Let's ask it to do that further.
Prompt 3
Show only the trades in a table, and use Eastern Time.
Plot the close values and plot the long and short signals on the chart, along with their exits

The chart X-axis is stretched out, lets try to get it fixed.
Prompt 4
Making additional adjustments.
Short exits should be green Xs, Long exits should be red Xs
Also show me the total PnL
Don't use the time on the X-axis. There are gaps in the time stretching the chart and I don't like that

Finally, trades to look at.

Prompt 5
Show me the profit curve

This is a sample strategy. I did not ask it to include commissions and slippage, which might result in a loss.
Prompt 6
What is the maximum drawdown?

Prompt 7
Show me the stats on this

Prompt 8
Check it with contract size, commissions, and slippages
Let's say I want to use this with 1 E-mini contract. The pnl would be multiplied by 20. The commission per trade is 3$. Assume a uniform slippage of 0.25 on SL execution.
Show the profit curve and the stats


As stated by ChatGPT, it is a bumpy equity line with heavy drawdowns, making it unusable.
Prompt 9
give me the entire code for this

That gives us the code to run it locally. Of course, you should review each line of code.
🧪 Where to Go From Here
This was a simple demonstration of what’s possible with ChatGPT and a bit of Python. If you’d like to go deeper, here are some directions you can explore:
- Parameter Optimization
Test variations of EMA period, ATR length, stop-loss, and take-profit multipliers to see what combinations are most robust. This is known as a parameter sweep or grid search. - Trade Performance Metrics
Add statistics like Sharpe Ratio, Profit Factor, Max Drawdown, and Average Trade Duration to evaluate strategy quality beyond just net profit. - Drawdown and Risk Analysis
Visualize drawdowns and equity curves to understand the emotional pressure and capital requirements of the strategy. - Strategy Enhancements
Try adding filters such as trend direction, volatility conditions, or higher-timeframe confirmation to refine your entries.
Remember: At every step, it is better to confirm the AI outputs. It can forget and revert things midway. If it feels stuck, it's better to restart from scratch.
Comments ()