Skip to content

API Reference


API Reference / MarginCalculator

Class: MarginCalculator

Defined in: src/resources/MarginCalculator.ts:32

Margin Calculator API wrapper.

Supports both single-order and multi-leg margin calculations with hedge benefits. For F&O strategies (spreads, iron condors, etc.), use the multi-leg endpoint to get combined margin requirements with hedging benefits applied.

See

https://dhan.co/help/docs/api/v2/#margin-calculator

Constructors

Constructor

new MarginCalculator(httpClient): MarginCalculator

Defined in: src/resources/MarginCalculator.ts:33

Parameters

httpClient

HttpClient

Returns

MarginCalculator

Methods

calculateSingle()

calculateSingle(request): Promise<MultiScripMarginCalcResponse>

Defined in: src/resources/MarginCalculator.ts:52

Calculate margin for a single order.

Parameters

request

MarginScriptItem

Returns

Promise<MultiScripMarginCalcResponse>

Example

ts
const margin = await client.marginCalculator.calculateSingle({
  exchangeSegment: "NSE_FNO",
  productType: "INTRADAY",
  optionType: "CALL",
  instrumentType: "OPTIDX",
  securityId: "44000", // NIFTY 24500 CE
  transactionType: "BUY",
  quantity: 50,
  price: 150.00,
});

calculateMulti()

calculateMulti(legs): Promise<MultiScripMarginCalcResponse>

Defined in: src/resources/MarginCalculator.ts:98

Calculate margin for multiple legs with hedge benefits.

This is essential for multi-leg strategies like spreads, straddles, and iron condors, where margin benefits are applied due to hedging.

Parameters

legs

MarginScriptItem[]

Array of order legs for the strategy

Returns

Promise<MultiScripMarginCalcResponse>

Example

ts
// Bull Call Spread: Buy lower strike call, sell higher strike call
const margin = await client.marginCalculator.calculateMulti([
  {
    exchangeSegment: "NSE_FNO",
    productType: "INTRADAY",
    securityId: "44000", // NIFTY 24500 CE
    transactionType: "BUY",
    quantity: 50,
    price: 150.00,
  },
  {
    exchangeSegment: "NSE_FNO",
    productType: "INTRADAY",
    securityId: "44050", // NIFTY 24600 CE
    transactionType: "SELL",
    quantity: 50,
    price: 100.00,
  },
]);

// The response will show reduced margin due to hedge benefits

bullCallSpread()

bullCallSpread(options): Promise<KnowYourMarginResponse>

Defined in: src/resources/MarginCalculator.ts:125

Helper: Build a bull call spread margin calculation.

Parameters

options
longStrikeSecurityId

string

shortStrikeSecurityId

string

quantity

number

longPrice

number

shortPrice

number

productType?

"CNC" | "INTRADAY" | "MARGIN" | "MTF" | "CO" | "BO"

Returns

Promise<KnowYourMarginResponse>

Example

ts
const margin = await client.marginCalculator.bullCallSpread({
  longStrikeSecurityId: "44000", // Lower strike (buy)
  shortStrikeSecurityId: "44050", // Higher strike (sell)
  quantity: 50,
  longPrice: 150.00,
  shortPrice: 100.00,
});

bullPutSpread()

bullPutSpread(options): Promise<KnowYourMarginResponse>

Defined in: src/resources/MarginCalculator.ts:176

Helper: Build a bull put spread margin calculation.

Parameters

options
longStrikeSecurityId

string

shortStrikeSecurityId

string

quantity

number

longPrice

number

shortPrice

number

productType?

"CNC" | "INTRADAY" | "MARGIN" | "MTF" | "CO" | "BO"

Returns

Promise<KnowYourMarginResponse>

Example

ts
const margin = await client.marginCalculator.bullPutSpread({
  longStrikeSecurityId: "43950", // Lower strike (buy put)
  shortStrikeSecurityId: "44000", // Higher strike (sell put)
  quantity: 50,
  longPrice: 80.00,
  shortPrice: 120.00,
});

ironCondor()

ironCondor(options): Promise<KnowYourMarginResponse>

Defined in: src/resources/MarginCalculator.ts:233

Helper: Build an iron condor margin calculation (4 legs).

Parameters

options
longPutSecurityId

string

shortPutSecurityId

string

shortCallSecurityId

string

longCallSecurityId

string

quantity

number

prices

{ longPut: number; shortPut: number; shortCall: number; longCall: number; }

prices.longPut

number

prices.shortPut

number

prices.shortCall

number

prices.longCall

number

productType?

"CNC" | "INTRADAY" | "MARGIN" | "MTF" | "CO" | "BO"

Returns

Promise<KnowYourMarginResponse>

Example

ts
const margin = await client.marginCalculator.ironCondor({
  longPutSecurityId: "43900",
  shortPutSecurityId: "43950",
  shortCallSecurityId: "44050",
  longCallSecurityId: "44100",
  quantity: 50,
  prices: {
    longPut: 50.00,
    shortPut: 80.00,
    shortCall: 70.00,
    longCall: 40.00,
  },
});

Community project — not affiliated with Dhan. MIT License.