How I backtested a simple strategy using ChatGPT

A hands-on walkthrough demonstrating how I utilized ChatGPT to backtest a straightforward EMA + ATR strategy, complete with stop-loss, take-profit, slippage, and realistic P&L tracking.

Simple strategy backtested using ChatGPT
Simple strategy using ChatGPT

🧠 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:

  1. CSV data file with OHLC data. This one was NQ data downloaded from TradingView, but you could use any source.
  2. 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.
Showing trades with the candles and indicators.
Showing trades with the candles and indicators.

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
Trade Signals and Close Prices
Trade Signals and Close Prices

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
Executed Trades with PnL
Executed Trades with PnL

Finally, trades to look at.

Positions taken by the strategy generated by ChatGPT
Positions taken by the strategy generated by ChatGPT

Prompt 4

Show me the profit curve
Profit Curve
Profit Curve

This is a sample strategy. I did not ask it to include commissions and slippage, which might result in a loss.

Prompt 5

What is the maximum drawdown?
Showing max drawdown

Prompt 6

Show me the stats on this
Performance summary
Performance summary

Prompt 7

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

Profit curve after commissions and slippages
Profit curve after commissions and slippages

As stated by ChatGPT, it is a bumpy equity line with heavy drawdowns, making it unusable.

Prompt 8

give me the entire code for this
Code for the strategy
Code for the strategy

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.