Skip to content

Latest commit

Β 

History

History
379 lines (286 loc) Β· 7.46 KB

File metadata and controls

379 lines (286 loc) Β· 7.46 KB

πŸ“˜ 5paisa Xtream Open APIs Documentation websocket


Table of Contents

WebSocket


WebSocket


🌐 Overview of WebSocket

Whether it's fetching live market data or receiving instant trade confirmations, real-time streaming support greatly enhances the experience for traders and investors.

The 5paisa Xstream WebSocket API allows seamless integration of real-time data feeds into trading applications with minimal latency.

πŸ”‘ Key Features

  • πŸ“ˆ Live Streaming of Market Data: Access real-time price movements, quotes, and market depth.
  • πŸ”” Order & Trade Confirmations: Get immediate updates on all order lifecycle events (Place, Modify, Cancel, Trigger).
  • πŸ” Secure & Authenticated Access: Requires valid access_token and client_code.

βš™οΈ How It Works

πŸ”— Step 1: Connect to WebSocket Server

Use the following URL with query parameters:

wss://openfeed.5paisa.com/feeds/api/chat?Value1=<access_token>|<client_code>
  • Replace <access_token> and <client_code> with your credentials (from the access token API).

πŸ”” Step 2: Subscribe to Data Streams

Once connected, the client sends subscription requests to receive specific types of data (market feed, depth, trade confirmations, etc.).

πŸ” Step 3: Manage Subscriptions

Clients can send Subscribe or Unsubscribe operations dynamically throughout the session.


πŸ’“ Heartbeat Mechanism

  • When the client sends PING, the server responds with PONG.
  • Ensures the connection remains alive.

πŸ” WebSocket Methods

All messages are JSON objects with this structure:

{
  "Method": "<method_name>",
  "Operation": "Subscribe | Unsubscribe",
  "ClientCode": "<client_code>",
  "MarketFeedData": [
    {
      "Exch": "N",
      "ExchType": "C",
      "ScripCode": 1660
    }
  ]
}

πŸ“‹ Supported Methods Overview

Method Purpose
MarketFeedV3 Real-time LTP, OHLC, bid/offer details
MarketFeedLite Minimal quote data (LTP, % change, last qty)
MarketDepthService Order book (bid/ask) depth
GetScripInfoForFuture Open interest & OI high/low for derivatives
OrderTradeConfirmations Order lifecycle updates (placed, traded, SL)

πŸ“ˆ Method: MarketFeedV3

  • Purpose: Real-time quotes, LTP, OHLC, bid/ask

βœ… Subscribe Request

{
  "Method": "MarketFeedV3",
  "Operation": "Subscribe",
  "ClientCode": "<client_code>",
  "MarketFeedData": [
    {"Exch": "N", "ExchType": "C", "ScripCode": 1660}
  ]
}

πŸ”„ Unsubscribe Request

{
  "Method": "MarketFeedV3",
  "Operation": "Unsubscribe",
  "ClientCode": "<client_code>",
  "MarketFeedData": [
    {"Exch": "N", "ExchType": "C", "ScripCode": 1660}
  ]
}

πŸ“€ Response Payload

{
  "Exch": "N",
  "ExchType": "C",
  "Token": 1660,
  "LastRate": 440.1,
  "TotalQty": 1000,
  "High": 445.5,
  "Low": 435.1,
  "OpenRate": 438,
  "PClose": 440,
  "AvgRate": 441.2,
  "Time": 35950,
  "TickDt": "/Date(1718052145000)/"
}

πŸ”’ Method: MarketFeedLite

  • Purpose: Lightweight version of MarketFeedV3 for minimal bandwidth use

βœ… Subscribe Request

{
  "Method": "MarketFeedLite",
  "Operation": "Subscribe",
  "ClientCode": "<client_code>",
  "MarketFeedData": [
    {"Exch": "N", "ExchType": "C", "ScripCode": 1333},
    {"Exch": "N", "ExchType": "D", "ScripCode": 148108},
    {"Exch": "B", "ExchType": "C", "ScripCode": 500112}
  ]
}

πŸ”„ Unsubscribe Request

{
  "Method": "MarketFeedLite",
  "Operation": "Unsubscribe",
  "ClientCode": "<client_code>",
  "MarketFeedData": [
    {"Exch": "N", "ExchType": "C", "ScripCode": 1333}
  ]
}

πŸ“€ Response Payload

{
  "Exch": "N",
  "ExchType": "C",
  "Token": 1333,
  "LastRate": 523.8,
  "LastQty": 10,
  "TickDt": "/Date(1718052145000)/",
  "ChgPcnt": 0.45
}

πŸ“Š Method: MarketDepthService

  • Purpose: Order book depth (bid/ask levels)

βœ… Subscribe Request

{
  "Method": "MarketDepthService",
  "Operation": "Subscribe",
  "ClientCode": "<client_code>",
  "MarketFeedData": [
    {"Exch": "N", "ExchType": "C", "ScripCode": 2885}
  ]
}

πŸ”„ Unsubscribe Request

{
  "Method": "MarketDepthService",
  "Operation": "Unsubscribe",
  "ClientCode": "<client_code>",
  "MarketFeedData": [
    {"Exch": "N", "ExchType": "C", "ScripCode": 2885}
  ]
}

πŸ“€ Response Payload

{
  "Exch": "N",
  "ExchType": "C",
  "Token": 2885,
  "TBidQ": 75000,
  "TOffQ": 82000,
  "Details": [
    {"Quantity": 500, "Price": 350.5, "NumberOfOrders": 3, "BbBuySellFlag": 66},
    {"Quantity": 450, "Price": 350.6, "NumberOfOrders": 2, "BbBuySellFlag": 83}
  ],
  "Time": "/Date(1718052145000)/"
}

πŸ“‰ Method: GetScripInfoForFuture

  • Purpose: Real-time Open Interest (OI) info

βœ… Subscribe Request

{
  "Method": "GetScripInfoForFuture",
  "Operation": "Subscribe",
  "ClientCode": "<client_code>",
  "MarketFeedData": [
    {"Exch": "N", "ExchType": "D", "ScripCode": 48508}
  ]
}

πŸ”„ Unsubscribe Request

{
  "Method": "GetScripInfoForFuture",
  "Operation": "Unsubscribe",
  "ClientCode": "<client_code>",
  "MarketFeedData": [
    {"Exch": "N", "ExchType": "D", "ScripCode": 48508}
  ]
}

πŸ“€ Response Payload

{
  "Exch": "N",
  "ExchType": "D",
  "Token": 48508,
  "OpenInterest": 2388700,
  "DayHiOI": 2400000,
  "DayLoOI": 2345100
}

πŸ”” Method: OrderTradeConfirmations

  • Purpose: Lifecycle status of orders (place/modify/cancel/trade/SL)

βœ… Subscribe Request

{
  "Method": "OrderTradeConfirmations",
  "Operation": "Subscribe",
  "ClientCode": "<client_code>"
}

πŸ”„ Unsubscribe Request

{
  "Method": "OrderTradeConfirmations",
  "Operation": "Unsubscribe",
  "ClientCode": "<client_code>"
}

πŸ“€ Response Payload (Place Order Example)

{
  "ReqType": "P",
  "Status": "Placed",
  "ScripCode": 1660,
  "Price": 425,
  "Qty": 1,
  "BuySell": "B",
  "ReqStatus": 0
}

πŸ“€ Response Payload (Trade Example)

{
  "ReqType": "T",
  "Status": "Fully Executed",
  "TradedQty": 1,
  "Price": 434.2,
  "ExchTradeTime": "2024-05-09 11:32:34"
}

βœ… Testing Considerations

  • Validate correct fields only (minimal subset)
  • Compare consistency with MarketFeedV3
  • Validate against large inputs (up to 3000 scrips)
  • Test concurrent subscriptions for stability

πŸ“˜ Parameter Summary

πŸ”Έ Exch (Exchange)

Code Description
N NSE
B BSE
M MCX

πŸ”Έ ExchType (Segment)

Code Description
C Cash
D Derivatives (F&O)
U Currency Derivatives

πŸ”Έ Operation

Value Description
Subscribe Start receiving live data
Unsubscribe Stop receiving live data

πŸ“˜ Best Practices

  • Subscribe only to required scrips to manage bandwidth and rate limits.
  • Use heartbeat (PING/PONG) to maintain connection health.
  • Monitor server responses for real-time margin alerts or trade confirmations.
  • Avoid exceeding 3000 subscriptions per session.