---
title: "Backtest Indian Stocks with AI and Python"
canonical: "https://tapetide.com/mcp/guides/backtest-indian-stocks-ai"
description: "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: "2026-10-25"
generated_at: "2026-10-29T05:21:54.071Z"
---

# Backtest Indian Stocks with AI: An Agent, Python and Point-in-Time NSE Data

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](/mcp) 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](/mcp/claude-code), [Cursor](/mcp/cursor), [Codex](/mcp/codex) or [VS Code](/mcp/vscode). Then create a personal token at [/settings/tokens](/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](/mcp/guides/cursor-vscode-indian-stock-data) has config blocks for each editor, and [MCP vs API](/mcp/guides/mcp-vs-api-indian-stock-data) 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:

```text
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

```text
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](https://developers.tapetide.com/guides/backtest-point-in-time/), 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

```text
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

```text
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](/pricing).
- **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](/insight/reports). To compare AI research tools for Indian stocks, see [best AI stock analysis apps in India](/compare/best-ai-stock-analysis-app-india). For more developer recipes, start at [developers.tapetide.com](https://developers.tapetide.com/recipes/).

## Frequently asked questions

### 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.

## More MCP guides

- [AI Portfolio Analysis for Indian Stocks with MCP](https://tapetide.com/mcp/guides/ai-portfolio-analysis-india): Analyse your Indian stock portfolio with Claude or ChatGPT: load holdings over MCP, check sector weights, P&L, results and ownership, with prompts to copy.
- [Claude and ChatGPT Prompts for Indian Stocks](https://tapetide.com/mcp/guides/ai-stock-analysis-prompts): Copy-ready Claude and ChatGPT prompts for Indian stock analysis: fundamentals, technicals, ownership and filings, each run on real NSE and BSE data over MCP.
- [Option Chain Analysis with AI: Nifty, Bank Nifty](https://tapetide.com/mcp/guides/option-chain-analysis-ai): Analyse Nifty and Bank Nifty option chains with Claude or ChatGPT: open interest, PCR, max pain, IV rank and skew from end-of-day data, with prompts to copy.
- [Claude for Stock Analysis in India: MCP Setup](https://tapetide.com/mcp/guides/claude-indian-stock-research): Use Claude for stock analysis in India: add the Tapetide MCP connector to Claude.ai, Desktop or Code for NSE and BSE data. Setup, a worked session, prompts.
- [What Is an MCP Server for Stocks? India Guide](https://tapetide.com/mcp/guides/indian-stock-market-mcp-server): What an MCP server for stocks is and how it works in India: how Claude or ChatGPT calls tools for NSE and BSE data, a worked example and what to check.
- [NSE & BSE MCP Server: Tools and Example Prompts](https://tapetide.com/mcp/guides/nse-mcp-server): Every tool the Tapetide NSE and BSE MCP server gives Claude, ChatGPT and Cursor, grouped by data type, with example prompts to copy and the limits to know.
- [ChatGPT for Indian Stocks with Real NSE Data](https://tapetide.com/mcp/guides/chatgpt-indian-stock-market): Connect ChatGPT to real NSE and BSE stock market data through a custom MCP app. Step-by-step setup, example prompts, limits and what ChatGPT cannot do.
- [Cursor and VS Code MCP for Indian Stock Data](https://tapetide.com/mcp/guides/cursor-vscode-indian-stock-data): Add Tapetide MCP to Cursor, VS Code, Codex or Claude Code for Indian stock data. Config blocks, auth options, rate limits and three developer workflows.
- [FII and DII Data, Inside Your AI Assistant](https://tapetide.com/mcp/guides/fii-dii-data-ai-assistant): Understand FII and DII data, what net buy and sell figures mean, and how to ask your AI assistant for daily flows, monthly trends and sector-wise FPI data.
- [MCP vs API for Indian Stock Market Data](https://tapetide.com/mcp/guides/mcp-vs-api-indian-stock-data): MCP vs REST API for Indian stock data: how each works, who should use which, auth, rate limits, freshness, and agent tasks that only work with MCP.
- [Stock Screener MCP: Screen NSE Stocks with AI](https://tapetide.com/mcp/guides/stock-screener-mcp): Use the Tapetide stock screener MCP to screen NSE and BSE stocks with AI on fundamental ratios, technical filters and preset screens, in plain English.

Connect your AI to Indian stock market data: https://tapetide.com/mcp
