Skip to main content

Package is a Python client library designed to interact with a broker API service. It provides functionalities to manage and execute trading strategies, handle orders, and retrieve market data.

Project description

📦 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)

🔄 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

📊 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.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brokerpackage-0.4.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

brokerpackage-0.4-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file brokerpackage-0.4.tar.gz.

File metadata

  • Download URL: brokerpackage-0.4.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.7

File hashes

Hashes for brokerpackage-0.4.tar.gz
Algorithm Hash digest
SHA256 60373f81aa00244dfc05ae34437ff88ccb75d4275d789e5bece231e4dc2d8d51
MD5 1b7207954a95ff5c2ed4bb010a15209f
BLAKE2b-256 82aa93929575a0c4c3a8a8b25ddebee2ff7a5826ed79c97cff736e5291a02f60

See more details on using hashes here.

File details

Details for the file brokerpackage-0.4-py3-none-any.whl.

File metadata

  • Download URL: brokerpackage-0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.7

File hashes

Hashes for brokerpackage-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 dc2a8a6bdb7beb132a7aa459cc923c8629cc4485c4f3aa817566bc7240da45b2
MD5 1a82802c5b5bf9492903b3d254d2e782
BLAKE2b-256 3e14a3aa6dd8ec516fcfe9c73b27deec500983258a75ea11d9fc7550554af05e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page