Skip to content

MCX margin_calculator + get_margin return HTTP 401 / 'Object reference not set' while NFO works #241

Description

@gurusura

Summary

Both margin_calculator() and get_margin() return HTTP 401 (wrapped server-side as {"Status": 500, "Error": "Object reference not set to an instance of an object."}) whenever exchange_code="MCX" is passed. The same SDK + API key + session token works correctly for NFO. MCX trading is fully active on the account (place_order for MCX options/futures works, MCX historical data works, capital is allocated to commodity segment on iDirect web).

Environment

  • breeze-connect version: 1.0.64 (also reproduced on the latest from PyPI)
  • Python 3.12 on Linux
  • Account: live MCX trading active, ~₹98K allocated to commodity segment on iDirect web
  • Same session token works for NFO calls in the same Python process

Reproduction

import os
from breeze_connect import BreezeConnect

b = BreezeConnect(api_key=os.environ["BREEZE_API_KEY"])
b.generate_session(api_secret=os.environ["BREEZE_API_SECRET"],
                   session_token=os.environ["BREEZE_SESSION_TOKEN"])

# === Control: NFO margin_calculator — HTTP 200 ===
nfo_leg = {
    "stock_code": "NIFTY", "exchange_code": "NFO", "product": "options",
    "expiry_date": "2026-04-28T07:00:00.000Z", "right": "call",
    "strike_price": "24050", "action": "buy", "quantity": "75",
    "price": "0", "cover_order_flow": "N", "fresh_order_type": "N",
    "cover_limit_rate": "0", "cover_sltp_price": "0",
    "fresh_limit_rate": "0", "open_quantity": "0",
}
print(b.margin_calculator(list_of_positions=[nfo_leg], exchange_code="NFO"))
# {'Success': {'margin_calulation': [...], ...}, 'Status': 200, 'Error': None}

# === MCX margin_calculator — HTTP 401 ===
mcx_leg = {**nfo_leg, "stock_code": "CRUDEOIL", "exchange_code": "MCX",
           "expiry_date": "2026-05-14T07:00:00.000Z",
           "strike_price": "9000", "quantity": "10"}
print(b.margin_calculator(list_of_positions=[mcx_leg], exchange_code="MCX"))
# {'Status': 500, 'Error': 'Object reference not set to an instance of an object.'}

# === Control: account-level get_margin — HTTP 200 ===
print(b.get_margin())
# {'Success': {'cash_limit': 293374.73, 'amount_allocated': 287938.67, ...},
#  'Status': 200, 'Error': None}

# === MCX get_margin — HTTP 401 ===
print(b.get_margin(exchange_code="MCX"))
# {'Status': 500, 'Error': 'Object reference not set to an instance of an object.'}

What's been tested (rules out parameter-shape issues)

12 distinct parameter combinations on margin_calculator for MCX, all return identical 401:

Field Variations tried
Product field name product=options, product_type=options, product_type=futureplus_options, product_type=futures
Expiry format ISO 8601 (T07:00:00.000Z), T06:00:00.000Z, DD-MMM-YYYY (14-May-2026), strike_date instead of expiry_date
Quantity field quantity="10" (lot size) vs lots="1"
Stock code CRUDEOIL vs CRUDEOILM (mini)
Style Options-style (right="call", strike="9000") vs futures-style (right="others", strike="0")

The 401 returns BEFORE any param validation runs, so the .NET ref error is the gateway's fallback message rather than a real null dereference.

Expected behaviour

margin_calculator and get_margin should accept exchange_code="MCX" for accounts with active MCX trading entitlement, the same way they accept "NFO" today.

Workaround

Falling back to a static SPAN approximation for required margin and to account-level get_margin() (no exchange_code) for available capital. Functional but loses per-exchange precision.

Notes

  • This is not reported on this repo or on r/algotrading / StackOverflow / forum.icicidirect.com as of April 2026. May be a recent server-side regression.
  • Happy to provide additional logs (request headers, full response body) if useful — please let me know what's safe to share publicly.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions