Skip to content

Zerodha funds: availablecash equals opening balance; expose live_balance + openingbalance for exact total/free margin #2137

Description

@arsalanansari17

Summary

For Zerodha, /api/v1/funds cannot give a caller either the live cash balance or the exact total/free margin once positions are open. Two small changes to broker/zerodha/api/funds.py would fix both:

  1. availablecash = Kite's available.live_balance (real intraday cash), and
  2. a new, additive field openingbalance = Kite's available.opening_balance.

Current behaviour

availablecash is computed as net + debits - collateral (the #1582 fix). Kite's margins response satisfies net = opening_balance + collateral - debits, so this simplifies to opening_balance: the static start-of-day balance, not the cash available now. It doesn't move as margin is used or cash is spent, which was the original complaint in #1582.

Switching availablecash to live_balance alone doesn't fix it either, because of how Zerodha blocks margin. Zerodha takes margin roughly 50:50 from cash and collateral (and credits option premium received to cash). So live_balance drops by only the cash share of the margin in use, and a caller can no longer rebuild the account's total or free margin from the five returned fields:

Illustrative account, iron fly open Value
Opening balance 4,00,000
Collateral (pledged holdings) 5,00,000
Used margin (SPAN + exposure − premium) 1,30,000
Kite "Available cash" (live_balance) ~3,43,000 (dropped by only ~57K)
Kite "Available margin" 7,70,000 = 4,00,000 + 5,00,000 − 1,30,000
Total rebuilt as live_balance + collateral + debits ~9,73,000 vs the real 9,00,000 (counts the collateral half twice)
Free rebuilt from that ~8,43,000 vs Kite's 7,70,000

We checked this against the Kite Funds page on a live account: opening balance + collateral − used margin matched Kite's "Available margin" to the rupee, and the cash drop equalled 50% of (SPAN + exposure) minus the option premium received.

Proposed change (Zerodha only, get_margin_data)

"availablecash": sum(available.live_balance for equity, commodity),   # real cash now
# additive, omitted if Kite leaves it out:
"openingbalance": sum(available.opening_balance for equity, commodity),

Callers then get Kite's own numbers:

  • live cash = availablecash
  • total capital = openingbalance + collateral (static intraday)
  • available margin = openingbalance + collateral - utiliseddebits

The openingbalance field is purely additive. The availablecash change does alter what the Dashboard / Telegram /funds show during the day, which is why this is an issue first rather than a PR. Happy to open a PR with both changes (we've run them on our own fork) if this direction is acceptable.

Related: #1582

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