Skip to content
View as Markdown

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.

Updated 25 October 20264 min read

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:

  1. Clean historical data: prices adjusted for splits and bonuses, results dated by when they became public, and the right company behind every historical symbol.
  2. Code that simulates trades day by day, with costs.
  3. 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_asof and get_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_asof to build the universe as of each start date.
  • Symbol changes. Indian companies rename and relist. resolve_identifier_asof maps 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_factors for 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?
Not by themselves. A chat assistant has no price database and cannot reliably simulate trades in its head. What works is an AI coding agent, such as Claude Code, Cursor or Codex, writing and running a Python backtest against real data. The Tapetide MCP tools give the agent the inputs; the backtest itself runs in your code.
Does the Tapetide MCP server run backtests?
No. Its historical tools provide inputs for research, such as adjusted price history, split and bonus factors, observation status, index membership on a date and historical identifiers. They do not simulate a trading strategy. Use them to check data, then use the REST API from Python for the bulk price pulls a backtest needs.
Where can I get historical NSE data for backtesting?
Tapetide's get_price_history tool and the matching REST route return daily or weekly open, high, low, close and volume with delivery percentage, up to 2,000 calendar days per call with pagination for earlier history. Prices are adjusted for splits and bonuses. Financial results carry the date they became public, which avoids look-ahead bias.
What is survivorship bias in an Indian stock backtest?
It is testing only on companies that exist today, which drops the ones that were delisted, merged or fell out of an index. That makes results look better than they were. get_index_membership_asof checks whether a stock was in an index on a past date, and resolve_identifier_asof maps an old symbol to the right company.
How much of the free plan does a backtest use?
It depends on the universe. A single stock over many years takes a few calls. A 50-stock universe needs at least one price call per stock, more for longer histories. The free plan allows 50 successful calls a day, shared between MCP and the REST API, so test the pipeline on a few stocks before scaling up.

Tapetide provides research data and tools. Nothing on this page is investment advice.