Can ChatGPT Backtest a Trading Strategy? How to Do It Without Fooling Yourself (2026 Guide)
What ChatGPT can compute, the code bugs that inflate AI-written backtests, a worked Backtesting.py example, and a checklist to audit any result before you trust it.
ChatGPT can backtest a trading strategy only in the sense that it can write backtest code and, with a code tool, run that code on price data you give it. It has no reliable price history of its own, so any results table produced without code and your data is invented. The numbers are only as honest as the code, and AI-written backtests commonly skip trading costs, fill orders on the signal bar, size positions past the account, and leak test data. Read every line, set costs explicitly, hold out data the strategy never saw, and forward test before risking money.
Yes, with a catch that decides everything: ChatGPT can backtest a trading strategy only by writing code and running it on price data you supply. It does not hold a trustworthy record of past prices, so a results table it types out without running code on your file is a guess formatted to look like a result. When it does run code, the output is exactly as honest as that code, and the code it writes by default tends to leave out trading costs and fill trades at prices you could not have had. This guide covers what the model actually computes, a worked example with real numbers, the bugs to look for line by line, and how to get from an AI-written backtest to a result you can lean on.
What ChatGPT actually does when you ask it to backtest
There are two very different things that can happen when you type "backtest this strategy" into a chat.
In the first, the model answers from text alone. It knows what a moving average crossover is and roughly how markets behaved, so it can produce a paragraph that reads like a summary of results: a win rate, a return, a drawdown. Nothing was computed. Those numbers came from the same process that writes the sentences around them, and they will change if you ask again.
In the second, the model writes Python and runs it. OpenAI describes its Code Interpreter tool as a way for models to "write and run Python code in a sandboxed environment," and its developer documentation calls the container "a fully sandboxed virtual machine." Files you upload go into that container, and the model's code reads them from there. In the API version, a container that sits unused for 20 minutes expires, its data cannot be recovered, and OpenAI tells developers to treat containers as ephemeral and keep their own copies. Plan on the same habit in the chat app: save the code and the data on your machine, because the sandbox is where the test ran, not where it lives.

The practical test is simple. If you cannot see the code, and you did not hand it the price file, treat the answer as an opinion. With both in front of you, you have a real backtest that still needs auditing, which is most of the work in the rest of this post.
That leaves four jobs ChatGPT does well in a backtest workflow:
- Turning plain-language rules into code for a library such as Backtesting.py, or into a Pine Script strategy for the TradingView Strategy Tester.
- Running that code on a CSV of prices you exported yourself, then summarizing trades, drawdowns, and returns.
- Reading code you or someone else wrote and pointing at lines that use future data or ignore costs.
- Explaining a statistics table you do not understand yet, such as what exposure time or a Sharpe ratio is telling you.
The job it does badly is the one most people ask for first: "Tell me if this strategy works." That question needs data, costs, and a sample of trades large enough to mean something, and the model has none of them until you provide them.
Why non-coders reach for it, and where that goes wrong
A thread on r/Trading put the question plainly: "If you're not a coder, how do you actually backtest a trading idea?" The answers split three ways. Some traders go back a year or two on a chart and step forward bar by bar, marking entries and exits in a spreadsheet; one called it slow but said it "forces you to actually see how the setup behaves instead of just trusting a backtest output." Some use TradingView's Strategy Tester or TradeStation, whose built-in language they learned over years. A few suggested ChatGPT, with the warning that "it's easy to miss biases or misinterpret results."
That warning is the whole story. ChatGPT removes the coding barrier, so a trader with no programming background can have a working backtest in an afternoon. It does not remove the knowledge barrier. If you do not know what look-ahead bias looks like in code, you cannot spot it in code the model wrote, and the model will not always flag it on its own. The result is a non-coder holding a confident-looking number with no way to check it.
The manual method in our guide on how to backtest a trading strategy avoids this, because you see every trade the rules take. It is slow. An AI-written script is fast. The best use of ChatGPT is to keep the speed and add back the checking.
A worked example: one moving average crossover, real numbers
The documentation for Backtesting.py, a popular open-source Python library, includes a standard example. It is a useful test case, because the code is public and the output is printed, so you can see what a clean result looks like before you ask a model to write your own.
The setup: daily Google (GOOG) prices from 19 August 2004 to 1 March 2013, 3,116 days. The strategy goes long when a 10-day simple moving average crosses above a 20-day one and short when it crosses below. Starting cash is $10,000, with a commission of 0.2%.
The output table reports:
- Return: 462.64%
- Buy and hold return over the same period: 607.37%
- Maximum drawdown: -33.93%
- Number of trades: 93
- Win rate: 52.69%
- Final equity: $56,263.52
- Exposure time: 94.27%
Bar chart comparing the Backtesting.py Quick Start results on daily GOOG prices from August 2004 to March 2013: the 10/20 SMA crossover strategy returned 462.64 percent with 0.2 percent commission, while simply buying and holding returned 607.37 percent. The strategy's maximum drawdown was 33.93 percent.
Three things in that table are worth slowing down for, because they are exactly what a quick summary from a chatbot tends to skip.
First, the strategy made a lot of money and still underperformed holding the stock. A summary that says "the strategy returned over 460%" is true and misleading at the same time. Always ask for the buy and hold line next to the strategy line, on the same dates.
Second, the guide's own prose describes the result as "almost 600% return in the period of 9 years," while the table above it says 462.64%. That is a documentation slip in a well-maintained project, and it makes a good habit obvious: read the numbers from the output, never from the sentence describing the output. The same rule applies when ChatGPT summarizes a run for you. Ask for the raw stats table and compare.
Third, 93 trades over roughly eight and a half years is about 11 a year. That is a reasonable sample for a daily strategy, but a single stock in a single long bull run is one market regime. The result tells you how this rule behaved on GOOG from 2004 to 2013. It does not tell you how it behaves on another ticker, or in a falling market, and past results do not guarantee future ones.
The defaults that quietly flatter a backtest
Backtesting.py's API reference lists the defaults for its Backtest class, and they matter because AI-written code often calls the class with only the data and the strategy:
commission=0.0andspread=0.0. Unless the code sets them, every trade is free and fills with no bid-ask cost. The documentation notes that commission is charged on both entry and exit when you do set it.trade_on_close=False. Market orders fill on the next bar's open, which is the honest default. If a prompt says "enter at the close of the signal bar" and the model flips this toTrue, the strategy now decides and fills on the same closing price.margin=1.0, meaning no leverage. The docs give0.02as the setting for 50:1. A model asked to "test with leverage" can set this without telling you what it does to the drawdown.cash=10000, andexclusive_orders=False, which allows overlapping trades unless you change it.
None of this is a flaw in the library. The defaults are documented. The problem is a script written in seconds by someone, or something, that never chose them.
Here is what the cost line does to a strategy that trades often. Take an illustrative strategy that averages a 0.3% gain per trade before costs. At the 0.2% commission from the example, charged on entry and on exit, each round trip costs about 0.4%, so the same strategy loses roughly 0.1% per trade after costs. Nothing about the rules changed. Only the line of code that was missing did. Strategies with small average gains and many trades, such as most intraday ideas, sit right on that edge, which is why a free-market backtest of a scalping system tells you almost nothing.
What an audit found in two ChatGPT-built bots
The most useful recent example is a post on r/Trading by a self-described non-programmer titled "My ChatGPT-built trading bots looked profitable. A proper audit said otherwise." Over a year they built two intraday bots with ChatGPT, one for Nordic stocks and one for US stocks, each taking one trade a day on paper. The paper results looked encouraging. Then they had the code audited, with help from Claude, and rebuilt the testing from scratch.
The audit found:
- No trading costs at all, so the net profit and the gross profit were the same number.
- Position sizes with no cap, averaging 1.35 times account equity per trade, with leverage up to 3x.
- Test data leaking into the results reported as live.
Once costs were added, the Nordic bot went from roughly break-even to clearly negative. The author then built a stricter research setup: more than 20 years of daily data, delisted stocks included, point-in-time S&P 500 membership, real broker fees, walk-forward testing, a deflated Sharpe ratio, and a sealed holdout period the strategies never saw during development. Three textbook strategies (short-term mean reversion, momentum, and ETF trend following) all failed the pass bar the author set in advance, after costs.

Two details from the post are worth remembering. A bad dividend record in the data created a fake +150% day, and one strategy appeared to gain 70% in 2008 when it had actually lost 33%. Neither was a coding error in the strategy logic. The data was wrong and the backtest believed it. And on Stockholm stocks, a minimum commission of SEK 49 per trade erased strategies that were profitable before costs, because a fixed minimum fee weighs far more on a small position than on a large one.
The author's own summary of the lessons: backtests without realistic costs reveal little, minimum commissions can wipe out small accounts, and testing many ideas makes it easier to find one that looks great by chance. That last one is the subject of the next section, and it is the one ChatGPT makes worse, because it makes testing many ideas so cheap.
How to backtest with ChatGPT, step by step
This is the workflow that keeps the model's speed and adds back the checking.
- Write the rules first, in plain language. Every entry, exit, stop, and size, specific enough that a stranger could trade them. "Buy when it looks strong" cannot be coded honestly. "Buy at the next open after the close crosses above the 50-day simple moving average, stop 2 ATR below entry, exit at the close on the day the close falls back below the average" can.
- Get the data yourself. Export a daily or intraday CSV from your broker or charting platform, with open, high, low, close, and volume. Check whether it is adjusted for splits and dividends, and say which in the prompt. Do not ask the model to supply price history.
- Split the data before you start. Set aside the most recent 20 to 30% of the history and do not show it to the model, or yourself, until the rules are final. That is your holdout.
- Prompt for code with every assumption stated. Name the library, the commission, the spread or slippage, the fill rule, the position size as a percent of equity, and the maximum leverage. A sample prompt is below.
- Run it and save everything. Keep the script, the data file, and the full stats table on your own machine.
- Read the code line by line against the checklist in the next section. Ask the model to explain any line you do not follow, then check its explanation against the library's documentation.
- Run the holdout once. If the result falls apart on data the rules never saw, the in-sample result was mostly fit to noise.
- Forward test. Trade the same rules on a simulator or at very small size, logged the same way, until you have a meaningful number of trades. Our guide to how long you should paper trade sets out what "meaningful" means.
A prompt that forces the assumptions into the open looks like this:
Write a Backtesting.py script for the rules below. Use the CSV I uploaded
(daily, split- and dividend-adjusted). Set commission=0.001 and spread=0.0005.
Market orders must fill on the next bar's open (trade_on_close=False).
Risk 1% of equity per trade, no leverage (margin=1.0), one position at a time
(exclusive_orders=True). Print the full stats table, including Buy & Hold
Return, and list every line where the code reads a value from the current or
a future bar to make a decision.
The cost numbers in that prompt are placeholders. Replace them with your broker's actual fees and a slippage estimate that fits the market you trade, and for a small account add any minimum commission per trade, which the Nordic example shows can decide the result by itself.
The audit checklist: bugs to find in AI-written backtest code
Run every script ChatGPT writes for you through this list before you believe the output. Most of these show up as a single line.
Look-ahead bias. The code uses information on a bar that was not available until that bar closed, then trades at a price from earlier in the same bar. A common form in pandas looks like this:
df["signal"] = df["close"] > df["close"].rolling(20).mean()
df["return"] = df["signal"] * df["close"].pct_change()
The signal on a given day uses that day's close, and the return on the same row is the move into that close, so the strategy is paid for a move it could only have known about after it happened. The fix is to shift the signal one bar, df["signal"].shift(1), so today's position comes from yesterday's information. Libraries such as Backtesting.py handle this for you with next-open fills, which is one reason to ask the model to use one instead of hand-rolled pandas.
Same-bar fills. A stop and a target both inside one bar's range, with the code assuming the target filled first. On daily data you cannot know the order from the OHLC values alone. Ask the model to assume the stop filled first whenever both levels sit inside the same bar.
No costs, no slippage. Covered above. Check that commission and spread are set to non-zero numbers that match your broker.
Position size and leverage. Search for how many shares or contracts each trade buys. The audited bots averaged 1.35 times equity per trade, which is leverage nobody chose.
Survivorship bias. A test on today's list of index members ignores the stocks that were dropped or delisted along the way, which tend to be the losers. The audit fixed this with delisted stocks and point-in-time index membership. If your data does not include them, say so when you report the result.
Bad data. Unadjusted splits show up as huge one-day crashes or gains, and a bad dividend record produced the fake +150% day above. Ask the model to list the ten largest daily moves in the file and check them against a chart.
Data leakage. Any step that uses the full dataset to set a parameter, such as normalizing prices with the mean of the whole series, lets the future into the past. So does picking the strategy's settings on the same data you report results on.
Overfitting: the problem ChatGPT makes cheaper
Once a backtest takes seconds to write, the temptation is to try everything: every moving average length, every RSI threshold, every stop distance, and keep the best. The Backtesting.py Quick Start shows the mechanism in miniature. It sweeps the fast average from 5 to 30 and the slow one from 10 to 70 in steps of 5, keeping only pairs where the fast one is shorter. By our count that is 63 combinations, and the search picks whichever produced the most final equity. The guide's authors note that the optimized version improved the result on the history it was fitted to, and warn that real-world use needs steps to avoid overfitting.

The academic version of this problem is in "The Probability of Backtest Overfitting" by David H. Bailey, Jonathan Borwein, Marcos López de Prado, and Qiji Jim Zhu (abstract at Western Michigan University ScholarWorks). The paper argues that standard techniques built to prevent overfitting in regression, such as a simple hold-out, tend to be unreliable for investment backtests, and it proposes a method for estimating how likely it is that a chosen strategy is overfit. You do not need the math to apply the lesson. Every variation you try is another lottery ticket, and the best ticket of many will look better than the strategy deserves.
What helps, in order of effort:
- Count your attempts. Write down every variation you tested, including the ones you threw away. A result picked from 63 tries means less than the same result from three.
- Prefer broad plateaus. If 20/50 works, 18/45 and 22/55 should work almost as well. A setting that only works at one exact number is fitted to noise.
- Keep the holdout sealed. Look at it once, after the rules are frozen. Change the rules after seeing it and it stops being a holdout.
- Test on more than one market and more than one regime, including a falling market.
- Treat a deflated Sharpe ratio or walk-forward test, as the audited trader used, as the next level up once the basics hold.
ChatGPT compared with dedicated backtesting tools
ChatGPT is a code writer and an explainer. It is not a backtesting platform, and for most traders the better setup is the model plus a real engine.
Backtesting.py and similar Python libraries handle fills, position tracking, and the stats table, so the model's job shrinks to writing the strategy class, which is where it is most reliable. TradingView's Strategy Tester runs Pine Script on the platform's own data, so you skip the data step, and ChatGPT can draft the Pine Script for you to paste in, though you still need to read it, because Pine has its own look-ahead traps in how it requests higher-timeframe data. MetaTrader 5's Strategy Tester does the same for forex brokers' quote history. Manual bar replay is the honest choice for discretionary chart reading, such as pattern trades, where the rule depends on judgment that does not reduce to a formula.
We compare the free options in detail in the best free backtesting apps, including what each one charges for deeper history.
If your edge comes from reading charts by eye, be wary of any tool, AI or not, that claims to backtest it automatically. The code has to turn "a clean bull flag" into exact rules, and the version it codes may not be the pattern you actually trade. Test the coded rule, and separately log your own discretionary trades, then compare.
Common mistakes
- Asking for results without uploading data. Whatever comes back is invented, however precise it looks.
- Accepting a summary sentence. Ask for the full stats table, including buy and hold, and read it yourself.
- Leaving costs at zero. Set commission and spread to your broker's real numbers, plus any minimum fee per trade.
- Letting the model choose position size. Specify a risk per trade, such as 1% of equity, and a maximum leverage. Our risk management guide covers how to pick those numbers.
- Re-prompting until the result looks good. Every re-prompt with tweaked rules is another attempt, and it counts toward overfitting even if the chat makes it feel like a conversation.
- Trusting a short live record. A few weeks of an AI-designed strategy beating the market is too small a sample to separate skill from luck.
- Going live on a backtest. A good backtest earns a forward test, not real money.
Frequently asked questions
Can ChatGPT backtest a trading strategy?
It can write backtest code and, when it has a code tool and your price data, run it and summarize the results. It cannot reliably backtest from memory, because it does not have accurate price history of its own. If no code ran on a file you supplied, the numbers are not a backtest.
As a non-coder, how do you actually backtest a trading idea?
Three routes work. Step through a chart bar by bar and log every trade in a spreadsheet, which is slow but shows you how the setup behaves. Use a built-in tester such as TradingView's Strategy Tester. Or have ChatGPT write code for a library like Backtesting.py and audit it with the checklist above. Whichever you pick, write the rules down first, precisely enough that someone else could follow them.
Are ChatGPT backtest results accurate?
Only as accurate as the code, the data, and the cost assumptions. The common errors in AI-written backtests (no costs, same-bar fills, uncapped position sizes, leaked test data) all make results look better than they are, so assume an unaudited result is optimistic.
Can ChatGPT write a Pine Script strategy for TradingView?
Yes, and it is a quick way to test a rule on TradingView's data without setting up Python. Read the script before trusting it. Check how it handles costs in the strategy settings and whether any higher-timeframe data request lets the script see a bar before it closed.
Is Claude or Gemini better than ChatGPT for backtesting?
The same limits apply to all of them: none holds reliable price history, and each can write code with the same bugs. Using a second model to review the first model's code can catch errors, which is how the audited trader found theirs, but the review is only as good as the checklist you give it.
How many trades does a backtest need?
As a working minimum, aim for at least 100 trades before trusting the direction of a result, more before trusting the win rate, spread across rising, falling, and sideways markets. Our backtesting guide walks through the math. Fewer trades than that is an anecdote.
Can ChatGPT find me a profitable strategy?
It can generate ideas and test them quickly, which also means it can generate many ideas until one looks good by chance. Used that way, count every idea you tried, keep a sealed holdout, and expect the best-looking result to shrink live. If the edge is real, a forward test on small size will show it, and if it is not, the forward test is the cheapest place to find out. Our guide for traders who can't find an edge covers building one from your own trade log instead.
Where Quant AI fits
ChatGPT is good at turning rules into code. It cannot tell you whether the chart you are looking at right now matches the rules you tested. Quant AI reads a screenshot of your chart and marks the patterns and levels it finds, which gives you a second read to check against your own before you take a trade from a tested plan. Writing the rules, auditing the backtest, sizing the position, and accepting the risk are still your job.