Skip to main content

📦 BrokerPackage

BrokerPackage is a Python client library designed to interact with a broker API service. It provides a structured interface to perform trading operations such as login, fetching quotes, placing orders, managing positions, and more.


🚀 Features

  • 🔐 Login & session handling
  • 📊 Fetch market quotes (API + local fallback)
  • 📈 View orders, positions, and portfolio
  • 💰 Check available cash & margin requirements
  • 🛒 Place, modify, and cancel orders
  • 🔄 Automatic failover (primary → secondary server)
  • 🎯 Token-based instrument identification
  • ⚡ Tick-size rounding utility for F&O (.05 precision)

This script requires the following libraries:

  • openpyxl==3.1.2
  • pandas==1.5.3
  • numpy==1.23.5

📦 Installation

pip install openpyxl==3.1.2 pandas==1.5.3 numpy==1.23.5 

🧑‍💻 Usage

1. Initialize

from BrokerPackage import BrokerPackage

broker = BrokerPackage(username="your_username")

2. Login

broker.login()

3. Get Token

token = broker.get_token(name="RELIANCE")

4. Get Quote

quote_df = broker.get_quote(name="RELIANCE")
print(quote_df)

5. Get LTP (Last Traded Price)

ltp = broker.get_ltp(name="RELIANCE")
print(ltp)

6. Place Order

order_id = broker.place_order(
    transaction_type="BUY",
    price_=2500,
    quantity=10,
    name="RELIANCE"
)
print(order_id)

7. Modify Order

broker.modify_order(order_id="12345", price=2550, quantity=10)

8. Cancel Order

broker.cancel_order(order_id="12345")

9. View Orders / Positions / Portfolio

orders = broker.orders()
positions = broker.positions()
portfolio = broker.portfolio()

10. Account Info

cash = broker.get_available_cash()
instruments = [
{
    'token': '72268',
    'lots': 1*65,
    'transaction_type': 'BUY',
    'token_detail': ['NIFTY', 'NFO', '28APR26', '24100.0', 'PE'],
    'lot_size': 65
},
{
    'token': '72267',
    'lots': 2*65,
    'transaction_type': 'SELL',
    'token_detail': ['NIFTY', 'NFO', '28APR26', '24100.0', 'CE'],
    'lot_size': 65
},
{
    'token': '99926000',
    'lots': 1,
    'transaction_type': 'BUY',
    'token_detail': ['NIFTY', 'NSE', '', '', ''],
    'lot_size': 1
},
{
    'token': '66691',
    'lots': 1*65,
    'transaction_type': 'BUY',
    'token_detail': ['NIFTY', 'NFO', '28APR26', '', 'FUT'],
    'lot_size': 65
}
]
margin_details = broker.get_required_margin(instruments = instruments)

freeze_quantity = broker.get_freeze_quantity(name = "RELIANCE")

🔄 Failover Mechanism

All API calls:

  • Try Primary URL
  • On failure → fallback to Secondary URL

🌐 API Endpoints Used

Endpoint Description
/get_file Fetch user configuration
/login Login using config
/get_token Get instrument token
/get_quote Fetch market data
/place_order Place trade
/modify_order Modify trade
/cancel_order Cancel trade
/orders Get order book
/positions Get positions
/portfolio Get holdings
/get_available_cash Cash balance
/get_required_margin Margin calculation
/get_freeze_quantity Freeze Quantity

📊 Local Quote Fallback

Method: get_quote_api()

Flow:

  1. Try local service (quotetickcandle-secvice)
  2. If failed → fallback to broker API

🎯 Tick Size Rounding

Utility method:

broker.round_to(12569.67)
  • Default precision: 0.05 (F&O tick size)
  • Supports dynamic precision via column or value

⚠️ Error Handling

  • All API responses checked for "error"
  • Raises Exception if any issue occurs
  • Network failures auto-switch to secondary server

🧩 Dependencies

  • pandas
  • requests
  • numpy
  • typing

📝 Notes

  • Ensure API services are running:

    • broker-api-service
    • quotetickcandle-secvice
  • URLs can be overridden via config file (get_file response)


📌 Example Workflow

broker = BrokerPackage("user1")
broker.login()

token = broker.get_token("NIFTY")
ltp = broker.get_ltp(name="NIFTY")

order_id = broker.place_order(
    transaction_type="BUY",
    price_=ltp,
    quantity=50,
    name="NIFTY"
)

📄 License

This project is intended for internal/private trading system usage.


Metadata

Release files for brokerpackage 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for brokerpackage 0.5
File Size Uploaded Provenance Actions
brokerpackage-0.5.tar.gz 5.9 kB Without provenance Details for brokerpackage-0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for brokerpackage 0.5
File Interpreter ABI Platform Actions
brokerpackage-0.5-py3-none-any.whl Python 3 none any Details for brokerpackage-0.5-py3-none-any.whl

Total release size: 12.3 kB

Release files / brokerpackage-0.5.tar.gz

Download URL brokerpackage-0.5.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
200fd52024839f2225b6799656aa170b1b41ae4b9d6ef627e1cb2bcb90aff751
BLAKE2b-256 checksum
How to use checksums
f8e1b4ef53d4e4fd7821fffc56bdf133c71e369b679bd6f1a71a5c0aa72d2e8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.7

Release files / brokerpackage-0.5-py3-none-any.whl

Download URL brokerpackage-0.5-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c96aaa8730b886d9c6d63be08aae7fd3ecf296ac55d7b0cf5f9fb717ffa815e
BLAKE2b-256 checksum
How to use checksums
36268b3fcf411a728ddaf8247a3ef302638d6ad0ca5148f7c1b9089c8946b02e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.7

Security for brokerpackage 0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page