Data Services Streaming (WebSocket) - All Segments-Subscribe Data

URL

WebSocket - Streaming- Steps to Subscribe the Market Data

  • WebSocket Connection
  • Subscription for the required Segment and contract symbol
  • Unsubscribe the Requested Segment and Symbol

Request

Key Required Value Discription
Segments All 2, 3, 4, 12, 13, 17, 35 Segments
Instrument TPIL_Instruments 1 Instruments
Otherinfo Optional 0 Timestamp-Seconds

Description

  • Action: Subscribe/Unsubscribe
  • Mode: TouchLine/Market Depth
  • Exchange, SegmentID, InstrumentID, Symbol, Series, Expiry, StrikePrice, oInfo: TimeStamp[XXXXX]

Example

  • Subscribe: NSE, 4, 1, SBIN,EQ, -1, -1, 1637620155
  • Subscribe: BSE, 3, 1, SBIN,EQ, -1, -1, 1637620156
  • Subscribe:Domestic Forex, 17,1, USDINRCOMP,-1,-1,T1637620155
  • Subscribe:International Forex, 12,1, EURUSD,-1,-1,1637620155
  • UnSubscribe: NSE, 4, 1, SBIN,EQ, -1, -1, 1637620155

Note: Otherinfo parameter will be Timestamp in seconds


Data Messages:

MessageHeader = "mgh";
MsgTimeStamp = "mts";
ClosingPrice = "clp";
OpeningPrice = "onp";
HighPrice = "hip";
LowPrice = "lop";
PreviousClosePrice = "pcp";
TotalTradedQty = "ttq";
MsgTimeStamp = "sts";
DivisionFactor = "dif";
trKeyOfData = "kod";
SegmentId = "sid";
BestBuyOrderQty = "bbq";
BestBuyOrderPrice = "bbp";
BestSellOrderQty = "bsq";
BestSellOrderPrice = "bsp";
TotalSellQty = "tsq";
TotalBuyQty = "tbq";
AverageTradedRate = "atr";
                            
                              {
                                 {
                                    rqh: //Request Header
                                     {
                                        mcd: 1001,   // TouchLine_short
                                        tip: 1654325456 // Time Stamp
                                       },
                                       rqd: //Request Data
                                        {
                                           key:
                                            {
                                              sid: 1, //Segment Id
                                              iid: 1, //Instrument Id
                                              sym: 'TCS', //Symbol
                                              ser: 'EQ', //Series
                                              exd: -1, //Expiry
                                              stp: -1, //Strick Price
                                              opt: 'XX', //Option Type
                                              }
                                             oif: 1654325456, //Last Updated Time Stamp for incremental info.
                                             act:  "subscribe", // subscribe or unsubscribe action
                                            }
                                         }
                              }
                            
                        

                        //NSE Equity
                        [
                         {
                            "rsh": {
                               "mcd": 0,
                               "tim": 0,
                               "erc": 0,
                               "ers": "0"
                              },

                              "rsd": {
                                 key:{4082-SBIN},
                                 "mts: "1345697895",
                                 "clp": "530.45",
                                 "opn": "520.25",
                                 "hip": "545.00",
                                 "lop": "522.25",
                                 "pcp": "515.25",
                                 "ttq": "1055022",
                                 "mst": "20220727-11.56.45.909",
                                 "div": "4",
                                 "sid": "4",
                                 "bbp": "545.30",
                                 "bbq": "12",
                                 "bsp": "547.55",
                                 "bsq": "45",
                                 "tsq": "4900",
                                 "tbq": "6400",
                                 "atr": "542.50",
                                },
                               }
                        ]

                        //INT Forex
                        [
                         {
                            "rsh": {
                               "mcd": 0,
                               "tim": 0,
                               "erc": 0,
                               "ers": "0"
                              },

                              "rsd": {
                                 key:{5949-SEURUSD},
                                 "mts: "1345697895",
                                 "clp": "1.0055",
                                 "opn": "1.0040",
                                 "hip": "1.0065",
                                 "lop": "1.0035",
                                 "pcp": "1.0045",
                                 "ttq": "10550",
                                 "mst": "20220727-11.56.45.909",
                                 "div": "4",
                                 "sid": "12",
                                 "bbp": "1.0055",
                                 "bbq": "1542",
                                 "bsp": "1.0060",
                                 "bsq": "4856",
                                 "tsq": "5623458",
                                 "tbq": "56478",
                                 "atr": "1.005225",
                                },
                               }
                        ]
                      
                    
                      
    //1.WS connect - Hand Shake Connection
   //2.Heart Beat connection
   //3.Subscribe the Data  
var ws;
function setConnected(connected) {
     $("#connect").prop("disabled", connected);
       $("#disconnect").prop("disabled", !connected);
       if (connected) {
          $("#conversation").show();
        }
        else {
           $("#conversation").hide();
         }
         $("#greetings").html("");
}

function connect() {
    	var inputval = document.getElementById("myInput").value;
    	var utoken = document.getElementById("Token").value;
    	var userName = document.getElementById("Username").value;

     //UAT
    	ws = new WebSocket('ws://uat.tickermarket.com/TPILWebSocket/user?username='+userName+'&jwt='+Jwttoken+"&token="+utoken);

    	ws.onmessage = function(data){
     	showGreeting(data.data);
    	}
    	 setConnected(true);
}

function exchsend(){
    	if (ws != null) {
           ws.close();
      }

    	var exdata = JSON.stringify({'exchname': $("#exchanges").val()})
    	 alert(exdata);
        ws.send(exdata);
}

function disconnect() {
      if (ws != null) {
          ws.close();
      }
     setConnected(false);
     console.log("Disconnected");
     document.getElementById("myInput").value="";
     document.getElementById("Token").value="";
}


//  Submit function - send subscription data
$(function () {
      $("form").on('submit', function (e) {
         e.preventDefault();
  });
  $( "#connect" ).click(function() { connect(); });
  $( "#disconnect" ).click(function() { disconnect(); });
  $( "#send" ).click(function() { sendName(); });

  const btn = document.querySelector("#exchsend");

  const sb = document.querySelector('#exchanges');

  const unsub = document.querySelector('#Unsub');

  btn.onclick = (event) => {

           event.preventDefault();
  }
//Heart Beat - Every 30 Seconds need to send the request continously

var subscribe_HB =
      {
           		"rqh": {
              	"mcd": "1010",
              	"tst" :"1654325456"
         },

      "rqd":{


          	  "key": {

         		"sid": "-1"
         		  iid: "-1",
         		  sym: "-1",
         		  ser: "-1",
         		  exd: "-1",
         		  stp: "-1",
       	 },
           	 "act":"beat",
           	 "oif":"1654325456"
        }

      };
         var exdata =  JSON.stringify(subscribe_HB);
         ws.send(exdata);



//subscribe FX Data- Request Header, Body and Data


     if($("#exchanges").val()=="INTLFX"){
             var symbol = $("#symbols").val();

           alert( $("#symbols").val());
           var subscribe_intlfx =
          	{
         	"rqh": {
             	"mcd": "1001",
             	"tst" :"1654325456"
              },

      "rqd":{


        "key": {

             	"sid": "12",
             	  iid: "2",
             	  sym: "EURUSD",
             	  ser: "-1",
             	  exd: "-1",
             	  stp: "-1",
         },
         "act":"subscribe",
         "oif":"1654325456"
        }

      };
         var exdata =  JSON.stringify(subscribe_intlfx);
         ws.send(exdata);

     }

//subscribe NSE Data- Request Header, Body and Data

else if($("#exchanges").val()=="NSE"){
          var symbol = $("#symbols").val();
          alert( $("#symbols").val());


         var subscribe_nse =
         {
             "rqh": {
                   "mcd": "1001",
                   "tst" :"1654325456"
                },

         "rqd":{


           "key": {

                   "sid": "4",
                     iid: "1",
                     sym: "SBIN",
                     ser: "-1",
                     exd: "-1",
                     stp: "-1",
              },
                 "act":"subscribe",
                 "oif":"1654325456"
             }

         };


          var exdata =  JSON.stringify(subscribe_nse);
          alert($("#exchanges").val());
          ws.send(exdata);
     }

//subscribe NSE Data- Request Header, Body and Data
else if($("#exchanges").val()=="BSE"){
          var symbol = $("#symbols").val();
     	var subscribe_bse =
         {
                "rqh": {
                   "mcd": "1001",
                   "tst" :"1654325456"
              },

         "rqd":{


           "key": {

                    "sid": "3",
                      iid: "1",
                      sym: "SBIN",
                      ser: "-1",
                      exd: "-1",
                      stp: "-1",
              },
              "act":"subscribe",
              "oif":"1654325456"
             }

         };


       var bseexdata =  JSON.stringify(subscribe_bse);
       alert($(subscribe_bse));
       ws.send(bseexdata);

    }else if($("#exchanges").val()=="MCX"){
            var symbol = $("#symbols").val();
              var exdata =  JSON.stringify(subscribe_mcx);



            ws.send(exdata);
          }
   };


   //unsubscribe - Fx

   unsub.onclick = (event) =>{
          var symbol = $("#symbols").val();
          alert( $("#symbols").val());

       if($("#exchanges").val()=="INTLFX"){

          var intlfxdata =     {
          	"rqh": {
          		"mcd": "1001",
          		"tst" :"1654325456"
         },

      "rqd":{


        "key": {

          		"sid": "12",
          		  iid: "2",
        			  sym: symbol,
          		  ser: "-1",
          		  exd: "-1",
          		  stp: "-1",
         },
         "act":"unsubscribe",
         "oif":"1654325456"
        }

      };


              var exdata =  JSON.stringify(intlfxdata);
              ws.send(exdata);


//unsubscribe - NSE

       }else if($("#exchanges").val()=="NSE"){
          var symbol = $("#symbols").val();
          var subscribe_nse =
          {
          "rqh": {
                    "mcd": "1001",
                    "tst" :"1654325456"
             },

          "rqd":{


            "key": {

                    "sid": "4",
                      iid: "1",
                      sym: symbol,
                      ser: "-1",
                      exd: "-1",
                      stp: "-1",
               },
               "act":"unsubscribe",
               "oif":"1654325456"
              }

          };


          var exdata =  JSON.stringify(subscribe_nse);
          ws.send(exdata);
       }

// Unsubscribe BSE

else if($("#exchanges").val()=="BSE"){
          var symbol = $("#symbols").val();
     var subscribe_bse =
         {
         "rqh": {
                   "mcd": "1001",
                   "tst" :"1654325456"
            },

         "rqd":{


                 "key": {

                   "sid": "3",
                     iid: "1",
                     sym: "SBIN",
                     ser: "-1",
                     exd: "-1",
                     stp: "-1",
            },
              "act":"subscribe",
              "oif":"1654325456"
               }

                     };

                  };

});