Historical Data via API: Backtesting Your Strategy

Smart traders research the past before taking financial risks. That replay is known as a backtest in API trading API trading. You add historical pricing into your rules and observe the possible results. This guide walks you through everything in five simple phases.

Step one: choose your data

Ensure data is appropriate. Historical data is available for NSE/BSE stocks, derivatives, US stocks, crypto and forex from brokers/data providers. Some of this data is free, and some requires a premium subscription.

Choose a bar interval. Daily bars summarise data per day, whereas 1-minute bars give much more data. However, they will take up more storage and running time. Each bar contains OHLCV data. Choose the largest bar interval possible for your strategy.

Step two: download, save and size

Download data in chunks using the REST API. Store data in CSV or Parquet format on your computer. This will allow you to perform multiple tests without exceeding the request limit.

Choose trade size. The margin calculator will allow you to see the amount of cash required per trade. To get an accurate idea of how the strategy would perform in real life, ensure that you do not exceed the amount of money in your real account. If the strategy requires more money than you have, it is invalid.

Step three: clean the data

Check for three problems:

  • Missing timestamps
  • Duplicate bars
  • Prices not adjusted for splits and dividends

One bad bar can create a fake signal. Clean first, then test.

Step four: run the test

Run the strategy. For each bar, enter, exit, apply stop-loss and sizing rules as defined in the strategy. These can be done using libraries in Python (Backtrader, VectorBT, Zipline).

Apply real-world factors. Include commission, taxes (STT) and a reasonable estimate of slippage for all trades. If we do not include these, profits will be artificially inflated.

Step five: read the results

Net profit alone is a poor guide. Look at the full picture:

MetricWhat it tells you
Net profitTotal gain after costs
Maximum drawdownThe biggest fall from a peak
Win rateShare of winning trades
Profit factorGross profit divided by gross loss
Sharpe ratioReturn for each unit of risk

If your strategy has a 40% drawdown, it may be difficult to stick with. It is useful to know the drawdown of a strategy.

Two mistakes that fool even experts

Survivorship bias. By only including stocks currently in existence, we are excluding those which have failed or been delisted. This means that our results will be optimistically biased compared to real life. Where possible, include delisted stock names.

Overfitting. If we change parameters in a rule enough, we may overfit. The strategy may perform perfectly on historical data, but very poorly on future data. To prevent this:

  1. Split your data into a training part and a test part.
  2. Keep your rules few and simple.
  3. Wait until the very end to touch a period.

If a result looks too good, doubt it.

Before real money

Try paper trading for a short period of time, or with very small amounts. Compare performance in live trading with backtesting. If there are discrepancies, this indicates some hidden issues in the strategy. Make sure to fix these first.

Conclusion

A good backtest is realistic. Make sure to use clean data, include all costs, and ensure the strategy is tested on untuned data. Start small, and validate the strategy with live trading.

Explore more technology guides