MCP GUIDE
Backtest Indian Stocks with AI: An Agent, Python and Point-in-Time NSE Data
Backtest NSE stocks with an AI coding agent and Python: adjusted prices, point-in-time fundamentals and survivorship checks over MCP and the Tapetide API.
To backtest Indian stocks with AI, use an AI coding agent to write and run a Python backtest against real NSE data, not a chat assistant to imagine one. Claude Code, Cursor or Codex can connect to the Tapetide MCP server to inspect data, check corporate actions and confirm index membership on past dates, then pull adjusted daily prices and point-in-time fundamentals from the Tapetide REST API into pandas. This guide shows the workflow, a worked example, the bias checks that matter for Indian markets and the limits.
Why a chatbot cannot backtest for you
Ask a chat assistant "backtest a 50/200 moving average crossover on Nifty stocks since 2015" and it will often produce a table of returns. Those numbers are generated, not computed. The model has no price database and does not step through thousands of trading days.
A real backtest needs three things:
- Clean historical data: prices adjusted for splits and bonuses, results dated by when they became public, and the right company behind every historical symbol.
- Code that simulates trades day by day, with costs.
- Checks for bias: look-ahead, survivorship and missing data treated as zero.
An AI coding agent can do the second part well and help you with the third. Tapetide supplies the first.
What the data tools give the agent
Among the server's 48 tools, these are built for historical research:
get_price_history: daily or weekly OHLCV with delivery percentage, up to 2,000 calendar days per call, with pagination for earlier history. Close is adjusted for splits and bonuses.get_adjustment_factors: split and bonus adjustments with dates and cumulative factors, plus events with no reliable numeric adjustment.get_observation_status: whether a day has a price, or is a weekend, holiday, pre-listing date, delisted period or a day with no recorded trade.get_index_membership_asof: whether a stock was in an index on a given date, with explicit uncertainty.resolve_identifier_asofandget_identifiers_asof: map historical symbols and ISINs to companies, and back.get_financials: quarterly and annual results with publication dates where available.
These tools provide inputs. They do not run backtests, and they say so in their own guide. A missing price is not a zero return, and out_of_coverage is not the same as confirmed absence.
Set up the agent
Add the MCP server to your coding agent: Claude Code, Cursor, Codex or VS Code. Then create a personal token at /settings/tokens and export it as TAPETIDE_TOKEN so your Python code can call the REST API at https://api.tapetide.com/v1. The same token and the same daily allowance cover both.
The Cursor and VS Code guide has config blocks for each editor, and MCP vs API explains when to use which surface.
A worked example: momentum on a fixed universe
The plan: test a simple 12-month momentum rule on ten large caps, rebalanced monthly, from 2018.
Step 1: Check the data with MCP
Ask the agent, in plain English:
For these 10 NSE stocks, use the Tapetide tools to check: any splits or
bonuses since 2018 with their factors, the first date each has price
data, and whether each was in the Nifty 50 on 1 January 2018. Put the
results in a markdown table.
The agent calls get_adjustment_factors, get_price_history and get_index_membership_asof. This step catches problems before you write any strategy code: a stock that listed in 2019, an unquantified corporate action, or a company that was not in the index at the start date.
Step 2: Pull prices in Python
Write panel.py: pull full daily price history for the 10 symbols from
the Tapetide REST API, paging back with end_date until has_more is
false. Use the adjusted close. Save one parquet file per symbol. Read
the token from TAPETIDE_TOKEN.
The REST pattern is documented in Build a backtest with point-in-time data, which the agent can read directly. Adjusted close keeps returns continuous across splits and bonuses. It does not include dividends; add them separately if you want total return.
Step 3: Write the backtest
Write backtest.py: each month end, rank the 10 stocks by 12-month
return skipping the latest month, hold the top 3 equally weighted until
the next month end. Charge 0.2% per trade. Treat missing prices as
missing, never as zero. Report CAGR, max drawdown and turnover against
an equal-weight buy-and-hold of all 10.
Ask the agent to print a few intermediate rows so you can see real dates and prices. If a rebalance date falls on a holiday, get_observation_status explains why there is no price.
Step 4: Add fundamentals without look-ahead
Extend the backtest to skip any stock whose trailing 4-quarter EPS was
negative, using only results that were public on each rebalance date.
Join on available_from, not on the quarter end.
Each financial period carries the date it became public. Joining on that date, with a backward as-of merge, keeps the backtest from using a result weeks before it was announced.
Bias checks for Indian markets
- Survivorship. A universe built from today's Nifty 50 drops companies that left the index. Use
get_index_membership_asofto build the universe as of each start date. - Symbol changes. Indian companies rename and relist.
resolve_identifier_asofmaps an old symbol to the company that used it on that date. - Look-ahead on results. Join on the publication date. Where the date is an estimate rather than reported, the developer guide shows how to treat it.
- Corporate actions. Check
get_adjustment_factorsfor events without a numeric factor; those need manual handling. - Costs. Include brokerage, taxes and slippage. A momentum rule that trades monthly can lose a meaningful share of its edge to costs.
Limits
- The MCP server does not run backtests. It supplies and checks data; your code does the simulation.
- Daily and weekly bars only over MCP. There are no intraday candles over MCP, so intraday strategies are out of scope here.
- Not a live trading feed, and nothing places orders.
- Free plan: 50 successful calls a day, shared between MCP and the REST API, with no monthly cap. Bulk history pulls add up; higher limits are on the pricing page.
- Past results do not predict future returns. A backtest is a test of an idea on history, not advice.
The data the backtest uses also feeds Tapetide's published research reports. To compare AI research tools for Indian stocks, see best AI stock analysis apps in India. For more developer recipes, start at developers.tapetide.com.
Questions people ask
Can ChatGPT or Claude backtest a strategy on Indian stocks?
Does the Tapetide MCP server run backtests?
Where can I get historical NSE data for backtesting?
What is survivorship bias in an Indian stock backtest?
How much of the free plan does a backtest use?
Tapetide provides research data and tools. Nothing on this page is investment advice.