> For the complete documentation index, see [llms.txt](https://docs.openalgo.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.openalgo.in/trading-platform/python/backtesting/support-functions/cost-calculator.md). Agents can also search and read this documentation through its [MCP server](https://docs.openalgo.in/~gitbook/mcp).

# Cost Calculator

`openalgo.backtest.costs` works out the full charges of an Indian trade: brokerage, STT, exchange charges, the SEBI fee, stamp duty, GST and the DP charge, at the rates in force on the trade date. The backtest charges every fill with it, and you can use it on its own to see what a trade will cost or to check your broker's contract note.

The first example prices a real intraday trade: 400 SBIN shares bought at the open of 11 November 2026 and sold at that day's close, at prices from your OpenAlgo server. Put the API key from your OpenAlgo dashboard in place of `OPENALGO_API_KEY`, and run the blocks in order: each continues from the ones before.

```python
# Load the OpenAlgo client and the cost calculator
from openalgo import api
from openalgo.backtest import costs

# Connect to your OpenAlgo server
client = api(api_key="OPENALGO_API_KEY", host="http://127.0.0.1:5000")

# SBIN's daily bar of 11 November 2026; iloc[0] picks its one row
sbin = client.history(symbol="SBIN", exchange="NSE", interval="D", start_date="2026-11-11", end_date="2026-11-11").iloc[0]

# The charges of buying 400 shares at the open and selling them at the close, intraday (MIS)
trip = costs.round_trip_cost(sbin["open"], sbin["close"], 400, "2026-11-11", exchange="NSE", product="MIS")

# The profit before and after charges
gross = (sbin["close"] - sbin["open"]) * 400
print(f"Bought at {sbin['open']:.2f}, sold at {sbin['close']:.2f}")
print(f"Profit before charges : Rs {gross:,.2f}")
print(f"Charges               : Rs {trip.total:,.2f}")
print(f"Profit after charges  : Rs {gross - trip.total:,.2f}")
```

```
Bought at 1056.00, sold at 1061.20
Profit before charges : Rs 2,080.00
Charges               : Rs 197.67
Profit after charges  : Rs 1,882.33
```

The quantity is in units (shares), and prices and charges are in rupees. For futures and options you can count in lots instead: see [Counting in lots](#counting-in-lots).

## What each charge is

`as_dict()` lists the charges of the round trip one by one:

```python
# A for loop runs the print once for each charge and its amount
for name, amount in trip.as_dict().items():
    print(f"{name:13} Rs {amount:8.2f}")
```

```
brokerage     Rs    40.00
stt           Rs   106.12
exchange_txn  Rs    26.00
sebi_fee      Rs     0.85
stamp_duty    Rs    12.67
gst           Rs    12.03
total         Rs   197.67
```

* `brokerage`: your broker's fee. The default preset (`"standard"`) charges 0.1 percent of each intraday order, at most Rs 20.
* `stt`: Securities Transaction Tax, paid on the sell side of an intraday share trade.
* `exchange_txn`: the exchange's transaction charges (with the IPFT charge).
* `sebi_fee`: the fee of SEBI, the market regulator.
* `stamp_duty`: paid on the buy side.
* `gst`: 18 percent GST on the brokerage, exchange charges and SEBI fee.

## A delivery (CNC) trade

A delivery trade holds the shares overnight. It pays STT on both sides, and selling shares from your holdings also pays the **DP charge**, a fixed fee per stock per day. Here, 100 SBIN shares bought at the close of 2 November 2026 and sold at the close of 11 November:

```python
# The close of 2 November 2026, the day of the buy
bought = client.history(symbol="SBIN", exchange="NSE", interval="D", start_date="2026-11-02", end_date="2026-11-02").iloc[0]

# A buy and a later sell of 100 shares, delivery (CNC), and the DP charge of the sell day
cnc = costs.round_trip_cost(bought["close"], sbin["close"], 100, "2026-11-02", sell_date="2026-11-11", exchange="NSE", product="CNC")
dp = costs.dp_charge("2026-11-11")
print(f"Round trip : Rs {cnc.total:,.2f} (STT Rs {cnc.stt:,.2f})")
print(f"DP charge  : Rs {dp.total:,.2f}")
```

```
Round trip : Rs 274.49 (STT Rs 204.82)
DP charge  : Rs 15.34
```

The default preset charges Rs 20 brokerage on each delivery order (2.5 percent of the order value when that is lower), and STT, 0.1 percent of each side, is most of the rest. The DP charge (Rs 13 plus GST) is paid once per stock on each day you sell it from your holdings.

## Another broker's brokerage

`brokerage=` takes a preset. The default (`"standard"`) charges an intraday order 0.1 percent of its value, at most Rs 20; `"zerodha"` charges 0.03 percent, at most Rs 20. The difference shows on small trades, here 20 SBIN shares bought and sold intraday:

```python
small = costs.round_trip_cost(sbin["open"], sbin["close"], 20, "2026-11-11", exchange="NSE", product="MIS")
zerodha = costs.round_trip_cost(sbin["open"], sbin["close"], 20, "2026-11-11", exchange="NSE", product="MIS",
                                brokerage="zerodha")
print(f"Default preset : Rs {small.total:,.2f}")
print(f"zerodha preset : Rs {zerodha.total:,.2f}")
```

```
Default preset : Rs 54.72
zerodha preset : Rs 22.51
```

On 20 shares (about Rs 21,000 an order) the `"zerodha"` preset charges about Rs 6 an order and the default Rs 20, its cap, which 0.1 percent of an order above Rs 20,000 reaches. On orders above Rs 66,667 both charge Rs 20. `costs.flat(amount)`, `costs.percent(percent, cap)` and `costs.custom(...)` describe any other broker. To use a preset in a backtest, see [Costs, Slippage and Margin](https://docs.openalgo.in/trading-platform/python/backtesting/costs-slippage-and-margin.md).

## Many trades at once

`trade_costs` prices a whole list of trades in one call, one row per trade. Here the day's four intraday orders, 400 SBIN and 100 RELIANCE shares, each bought at the open and sold at the close:

```python
reliance = client.history(symbol="RELIANCE", exchange="NSE", interval="D", start_date="2026-11-11", end_date="2026-11-11").iloc[0]

# Four lists of the same length: the sides, prices, quantities and dates, one entry per order
trades = costs.trade_costs(
    ["BUY", "SELL", "BUY", "SELL"],
    [sbin["open"], sbin["close"], reliance["open"], reliance["close"]],
    [400, 400, 100, 100],
    ["2026-11-11"] * 4,  # the same date four times
    "NSE_EQ_INTRADAY",  # the segment: NSE shares, intraday
)
print(trades[["brokerage", "stt", "stamp_duty", "total"]].round(2).to_string(index=False))
```

```
 brokerage    stt  stamp_duty  total
      20.0   0.00       12.67  52.07
      20.0 106.12        0.00 145.60
      20.0   0.00        4.00  32.59
      20.0  33.38        0.00  61.98
```

## Counting in lots

Commodity and currency traders count in lots, as a broker's online calculator does. Pass `lots=` with the contract's `symbol=` and `exchange=` instead of a quantity: the lot size and price multiplier for the trade date come from the built-in reference tables, so no server is needed. Here one lot of each, bought and sold on 23 November 2026, priced the way the broker's online calculator prices them (`brokerage="zerodha"` and `rounding="calculator"`):

```python
# The cost calculator alone: this block needs no OpenAlgo server
from openalgo.backtest import costs

# The online calculator's brokerage and rounding, for futures and options held overnight (NRML)
calculator = {"brokerage": "zerodha", "rounding": "calculator", "product": "NRML"}

# One lot each: an aluminium future and a copper call option on MCX, a dollar-rupee future and call option on NSE
aluminium = costs.round_trip_cost(250, 252, date="2026-11-23", lots=1, symbol="ALUMINI31DEC26FUT", exchange="MCX", **calculator)
copper = costs.round_trip_cost(100, 110, date="2026-11-23", lots=1, symbol="COPPER23DEC261360CE", exchange="MCX", **calculator)
usdinr = costs.round_trip_cost(88.33, 88.3525, date="2026-11-23", lots=1, symbol="USDINR29DEC26FUT", exchange="CDS", **calculator)
usdinr_call = costs.round_trip_cost(88.33, 88.3525, date="2026-11-23", lots=1, symbol="USDINR29DEC2688.5CE", exchange="CDS", **calculator)
print(f"ALUMINI future, 1 lot     : Rs {aluminium.total:,.2f}")
print(f"COPPER 1360 call, 1 lot   : Rs {copper.total:,.2f}")
print(f"USDINR future, 1 lot      : Rs {usdinr.total:,.2f}")
print(f"USDINR 88.50 call, 1 lot  : Rs {usdinr_call.total:,.2f}")
```

```
ALUMINI future, 1 lot     : Rs 90.43
COPPER 1360 call, 1 lot   : Rs 452.28
USDINR future, 1 lot      : Rs 48.14
USDINR 88.50 call, 1 lot  : Rs 116.42
```

Lots become price units, the unit a price is quoted in: one ALUMINI lot is 1,000 (one tonne, quoted per kilogram), one COPPER lot 2,500 (kilograms) and one USDINR lot 1,000 (dollars), so `quantity=1000` prices the same ALUMINI or USDINR lot. Pass one of `quantity=` and `lots=`, not both. Lots of a share, part lots, and a contract the tables do not cover raise an error. Currency futures and options on NSE are costed from openalgo 2.4.1; they can be backtested, but `bt.live` does not trade them yet.

## More options

* Segments: NSE and BSE equity (delivery and intraday; BSE needs `bse_group=`), NFO and BFO futures and options, MCX futures and options (they need `underlying=`, the commodity, or `symbol=`), and NSE currency futures and options (CDS). Name one with `segment=` (one of `costs.SEGMENTS`, or `costs.segment_for(exchange, product, instrument)`) or with `exchange=` and `product=` (plus `instrument="FUT"` or `"OPT"`). BSE currency derivatives (BCD) and options on agri commodities raise `CostError`, as does anything the calculator does not recognise.
* `trade_cost(side, price, quantity, date, ...)` prices one order. A `CostBreakdown` has `brokerage`, `stt`, `exchange_txn`, `sebi_fee`, `stamp_duty`, `gst`, `total`, the `schedules` that priced it and `verified`.
* `rounding="contract_note"` (default) returns unrounded amounts to be rounded per contract note; `rounding="calculator"` rounds each trade to the paisa the way the broker's online calculator does.
* `costs.contract_note(trades)` rounds a day's trades the way a contract note (the statement your broker sends after each trading day) does: each charge summed per date and segment, STT and stamp duty to the rupee, the others to the paisa. That rule is an assumption, stated in `costs.CONTRACT_NOTE_RULE`.
* Presets: `costs.standard()` (the default), `zerodha()`, `zero_brokerage()`, `flat(amount)`, `percent(percent, cap)` and `custom(...)`.
* Rates are dated: `costs.schedules()` lists the built-in schedules from 1 July 2020, each with the date its rates were last checked. A trade before 1 July 2020 raises `CostError` unless the model is `CostModel(backfill=True)`. Rates not verified for a date still price the trade, with `verified=False` and an `UnverifiedScheduleWarning`.
* `costs.CostModel(brokerage=..., rounding=..., schedules=[...], backfill=...)` builds a model with its own preset or corrected rates; pass it to `bt.backtest(costs=model)`.
* `costs.exercise_stt(intrinsic, quantity, date)` is the STT on an exercised option.
