5. Account Balance
Check buying power, positions and validate before placing orders
Objective
Check balance, max buying power, stock positions before placing orders. Block order placement if conditions are not met.
Flow
Trading Service → Account API → Validation Rule EngineSample Code — Sync
from ssi_sdk import Auth, Trading, Config
from auth_helper import ensure_auth
from helpers import enum_value, mask_account
config = Config(
client_id="<your_client_id>",
api_key="<your_api_key>",
api_secret="<your_api_secret>",
private_key="<your_private_key>",
)
ACCOUNT_NO = "<your_account_no>"
with Auth(config) as auth:
ensure_auth(auth, otp="<your_otp>")
with Trading(auth) as trading:
accounts = trading.account.get_account_info()
for acc in accounts:
print(f" - {mask_account(acc.account_no)} ({enum_value(acc.account_type)})")
balance = trading.portfolio.get_equity_balance(ACCOUNT_NO)
print(f" Available cash: {balance.account_balance:,.0f}")
print(f" Buy T0/T1/T2 : {balance.buy_t0:,.0f} / {balance.buy_t1:,.0f} / {balance.buy_t2:,.0f}")
print(f" Sell T0/T1/T2 : {balance.sell_t0:,.0f} / {balance.sell_t1:,.0f} / {balance.sell_t2:,.0f}")
max_bs = trading.trading.get_max_buy_sell(ACCOUNT_NO, "SSI", 26000)
print(f" Max buy: {max_bs.max_buy_quantity} shares")
print(f" Max sell: {max_bs.max_sell_quantity} shares")
print(f" Purchasing power: {max_bs.purchasing_power}") # may be None (derivative accounts)
positions = trading.portfolio.get_equity_positions(ACCOUNT_NO)
for pos in positions:
print(
f" {pos.symbol} | Qty: {pos.quantity} | Sellable: {pos.sellable_quantity} | Cost: {pos.cost_price:,.0f}"
f" | T1/T2 buy: {pos.t1_buy_quantity}/{pos.t2_buy_quantity}"
f" | T1/T2 sell: {pos.t1_sell_quantity}/{pos.t2_sell_quantity}"
)Sample Code — Async
import asyncio
from ssi_sdk import AsyncAuth, AsyncTrading, Config
from auth_helper import ensure_auth_async
config = Config(...)
ACCOUNT_NO = "<your_account_no>"
async def main():
async with AsyncAuth(config) as auth:
await ensure_auth_async(auth, otp="<your_otp>")
async with AsyncTrading(auth) as trading:
balance, max_bs, positions = await asyncio.gather(
trading.portfolio.get_equity_balance(ACCOUNT_NO),
trading.trading.get_max_buy_sell(ACCOUNT_NO, "SSI", 26000),
trading.portfolio.get_equity_positions(ACCOUNT_NO),
)
asyncio.run(main())Notes
- Trading API endpoints require OTP during authentication.
- SDK 3.3.0: use
purchasing_power(numeric, may beNoneon derivative accounts). The oldpurchase_poweris deprecated and emits aDeprecationWarning. - The position fields
t1_buy_quantity/t2_buy_quantity/t1_sell_quantity/t2_sell_quantityare QUANTITIES pending settlement on T+1 / T+2. - Mask the account number before printing it with
mask_account(seehelpers.pyon page 1). - Always check
account_balanceormax_buy_quantitybefore placing orders. - The async version can fetch multiple data in parallel with
asyncio.gather.