## Funds
Balances and pre-trade margin checks, from the [Funds](/api/v1/reference/funds/)
endpoints. Margin checks take `place_order`'s arguments and are never retried.
Examples use `api` and `quote` from [Getting started](/sdk/python/).

### api.get_funds(...) — Available balances

**Calls:** [`GET /v1/funds`](/api/v1/reference/funds/#get-v1funds--available-balances) · **Scope:** `funds.read`

There is no single balance: `available` has one figure per kind of order.

#### Signature

```python
api.get_funds(*, timeout: float | None = None) -> dict
```

#### Returns

`available`, `balance`, `utilised` and `option_premium`, in paise, as
[the endpoint](/api/v1/reference/funds/#get-v1funds--available-balances)
documents. The `available` keys:

| Key | Spendable on |
|---|---|
| `stocks_delivery` | Delivery buys. Pledge collateral does not count. |
| `intraday_and_fno_selling` | Intraday, futures and option selling. Pledge collateral counts. |
| `options_buying` | Option buys, per premium pool: `equity_fno`, `mcx`, `ncom`. |

#### Raises

- [`SahiNotFoundException`](/sdk/python/errors/#sahinotfoundexception): the account is not set up for trading.
- [`SahiRateLimitException`](/sdk/python/errors/#sahiratelimitexception): over 1/s.

#### Example

```python
available = api.get_funds()["available"]
print(available["stocks_delivery"] / 100, "INR for delivery buys")
```

### api.get_order_margin(...) — Order margin

**Calls:** [`POST /v1/margins/order`](/api/v1/reference/funds/#post-v1marginsorder--order-margin) · **Scope:** `funds.read`

The margin one order would need. Nothing is placed, and tick size, price band
and lot size are not checked.

#### Signature

```python
api.get_order_margin(*, exchange_segment: str, symbol: str, txn_type: str, qty: int, order_type: str, product: str, validity: str, price: int, trigger_price: int | None = None, disclosed_qty: int | None = None, market_protection: float | None = None, variant: str | None = None, timeout: float | None = None) -> dict
```

#### Returns

`{"required_margin": ..., "available_margin": ...}`, in paise.

#### Raises

- [`SahiMaintenanceException`](/sdk/python/errors/#sahimaintenanceexception): 04:45–07:00 IST, during start-of-day processing. Retry after 07:00.
- [`SahiValidationException`](/sdk/python/errors/#sahivalidationexception) and [`SahiNotFoundException`](/sdk/python/errors/#sahinotfoundexception): as for `place_order` and `get_funds`.
- [`SahiRateLimitException`](/sdk/python/errors/#sahiratelimitexception): over 5/s, 250/min or 10,000/day, shared with the basket check.

#### Example

```python
margin = api.get_order_margin(
    exchange_segment=SahiAPI.EXCHANGE_SEGMENT_NSE,
    symbol="RELIANCE",
    txn_type=SahiAPI.TXN_TYPE_BUY,
    qty=10,
    order_type=SahiAPI.ORDER_TYPE_LIMIT,
    product=SahiAPI.PRODUCT_INTRADAY,
    validity=SahiAPI.VALIDITY_DAY,
    price=quote["dpr_lower"],
)
print(margin["required_margin"] / 100, "INR needed")
```

### api.get_basket_margin(...) — Basket margin

**Calls:** [`POST /v1/margins/basket`](/api/v1/reference/funds/#post-v1marginsbasket--basket-margin) · **Scope:** `funds.read`

The margin for one to ten orders together, netted against each other and your
positions. `available_margin` is a single pool, so it can overstate a basket
that mixes pools.

#### Signature

```python
api.get_basket_margin(legs: list[dict], *, timeout: float | None = None) -> dict
```

| Parameter | Type | Required | Notes |
|---|---|---|---|
| `legs` | list[dict] | yes | One to ten orders, each a dict of `place_order`'s arguments. |
| `timeout` | float | no | Seconds. |

#### Returns

`{"required_margin": ..., "available_margin": ...}`, in paise.

#### Raises

- `TypeError`: a leg is missing a key or has an unknown one, raised before sending.
- [`SahiValidationException`](/sdk/python/errors/#sahivalidationexception): a leg breaks a rule, or the list is empty or over ten.
- Otherwise as for `get_order_margin`.

#### Example

```python
leg = dict(
    exchange_segment=SahiAPI.EXCHANGE_SEGMENT_NSE, symbol="RELIANCE",
    txn_type=SahiAPI.TXN_TYPE_BUY, qty=1, order_type=SahiAPI.ORDER_TYPE_LIMIT,
    product=SahiAPI.PRODUCT_INTRADAY, validity=SahiAPI.VALIDITY_DAY, price=quote["dpr_lower"],
)
margin = api.get_basket_margin([leg, {**leg, "txn_type": SahiAPI.TXN_TYPE_SELL}])
print(margin["required_margin"] / 100, "INR needed")
```

