Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

    How to get fed_token and how to use token and task and how to subscribe and unsubscribe a script?????

    General Discussion
    5
    23
    463
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • James Bond
      James Bond @ashmita_ last edited by James Bond

      @ashmita_

      You can use following code to get Feed Token and subscribe any scrip.

      from smartapi import SmartConnect 
      from smartapi import WebSocket 
      
      obj=SmartConnect(api_key="your_api_key")
      data = obj.generateSession('client_id', 'password')
      feedToken=obj.getfeedToken()
      
      FEED_TOKEN= feedToken
      CLIENT_CODE= 'client_id'
      task="mw" #'mw'|'sfi'|'dp'
      token="nse_cm|26009"
      WS = WebSocket(FEED_TOKEN, CLIENT_CODE)
      
      def on_tick(ws, tick):
          print(tick)
      
      def on_connect(ws, response):
          ws.send_request(token)
      
      def on_close(ws, code, reason):
          ws.stop()
      
      # Assign the callbacks.
      WS.on_ticks = on_tick
      WS.on_connect = on_connect
      WS.on_close = on_close
      WS.connect()
      

      To subscribe Reliance (NSE), first you need to find out Reliance scrip / symbol token from following JSON file.

      https://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json

      then you need to change token in above code. In case of RELIANCE-EQ token is 2885
      so change:

      token="nse_cm|26009"
      

      to

      token="nse_cm|2885"
      

      To use above code, you need to update above code with your api key, client id and password.

      1 Reply Last reply Reply Quote 0
      • James Bond
        James Bond @ashmita_ last edited by

        @ashmita_ said in How to get fed_token and how to use token and task and how to subscribe and unsubscribe a script?????:

        FEED_TOKEN= "your feed token"

        SmartAPI SDK has inbuilt method to generate feed token, so you don't have to worry about it.

        A 1 Reply Last reply Reply Quote 0
        • A
          ashmita_ @James Bond last edited by

          @James-Bond Sir what is the meaning of task (task="mw" #'mw'|'sfi'|'dp')???
          And sir can we subscribe a script using symbol name like this "nse_cm|RELIANCE" instead of using token number like this way "nse_cm|2885" ???

          James Bond 2 Replies Last reply Reply Quote 0
          • James Bond
            James Bond @ashmita_ last edited by James Bond

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • James Bond
              James Bond @ashmita_ last edited by

              @ashmita_ said in How to get fed_token and how to use token and task and how to subscribe and unsubscribe a script?????:

              Sir what is the meaning of task (task="mw" #'mw'|'sfi'|'dp')???

              Screenshot 2021-03-17 at 12.22.55 AM.png

              More info here: https://smartapi.angelbroking.com/docs/WebSocket

              @ashmita_ said in How to get fed_token and how to use token and task and how to subscribe and unsubscribe a script?????:

              can we subscribe a script using symbol name like this "nse_cm|RELIANCE" instead of using token number like this way "nse_cm|2885" ???

              No, we can't use something like nse_cm|RELIANCE. Only scrip token will be considered as acceptable value.

              A 1 Reply Last reply Reply Quote 0
              • A
                ashmita_ @James Bond last edited by ashmita_

                @James-Bond Sir can you please tell me how to get the instruments token list including F&O contract's........
                I'm using python sdk....
                Some one give me this link https://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json but it's looks messy and confusing I can't understand how can I use it sir......
                Also how to subscribe multiple script at the same time mean token format like this
                token="nse_cm|2885, token=nse_cm|5285, token=nse_cm|2521, token=nse_cm|2854"

                S James Bond 2 Replies Last reply Reply Quote 0
                • S
                  Surya 1 @ashmita_ last edited by

                  @ashmita_

                  You can read them in pandas.. Its in json format

                  df = pd.read_json('http://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json')
                  

                  Python dictionaries are hashed.. so its faster. But if you prefer csv you can convert them like the following

                  df.to_csv(file_name)
                  
                  A 1 Reply Last reply Reply Quote 0
                  • James Bond
                    James Bond @ashmita_ last edited by James Bond

                    @ashmita_

                    To subscribe multiple scrips at same time, use following format:

                    "nse_cm|2885&nse_cm|5285&nse_cm|2521&nse_cm|2854"
                    

                    It should be separated by &

                    Getting token list:

                    @ashmita_ said in How to get fed_token and how to use token and task and how to subscribe and unsubscribe a script?????:

                    Some one give me this link https://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json but it's looks messy and confusing I can't understand how can I use it sir......

                    @rajanprabu answered your query in above post which is easiest approach to get scrip names and token lists.

                    A 1 Reply Last reply Reply Quote 0
                    • A
                      ashmita_ @James Bond last edited by

                      @James-Bond Sir I fetched this data in a dataframe but I'm not found instrument like Reliance_EQ, Reliance_Future, Reliance_Options
                      GFGFG.PNG
                      And sir can you please tell me what is the difference between Trading API and Market Feeds API
                      Captudddddre.PNG
                      when I hover the cursor on I button it's show__
                      Trading API:- Access full fledged APIs to execute orders i real time, manage use portfolio, stream live market data and more.
                      Market Feeds APIs:- Access real time market data for various stocks.

                      A S James Bond 3 Replies Last reply Reply Quote 0
                      • A
                        admin @ashmita_ last edited by admin

                        Hi @ashmita_ please mail us your piece of code @ api@angelbroking.com.

                        1 Reply Last reply Reply Quote 0
                        • S
                          Surya 1 @ashmita_ last edited by

                          @ashmita_

                          possible reliance search terms

                          RELIANCE-EQ
                          RELIANCE29APR211660CE
                          RELIANCE29APR21FUT

                          If one just wants to use the data they saw market API, otherwise trading API.

                          1 Reply Last reply Reply Quote 0
                          • James Bond
                            James Bond @ashmita_ last edited by James Bond

                            @ashmita_ said in How to get fed_token and how to use token and task and how to subscribe and unsubscribe a script?????:

                            I'm not found instrument like Reliance_EQ,

                            Do you converted json into csv ?

                            You should use RELIANCE-EQ for equity.
                            RELIANCE25MAR21FUT for current month contract

                            Can you share your code, so we can help you.

                            1 Reply Last reply Reply Quote 0
                            • A
                              admin @ashmita_ last edited by admin

                              Hi @ashmita Please refer the following code to generate feed token
                              ######################################################
                              from smartapi import SmartConnect

                              obj=SmartConnect(api_key="put your api key here")

                              data = obj.generateSession("put your client id here","put your passwd here")
                              refreshToken= data['data']['refreshToken']

                              #fetch the feedtoken

                              feedToken=obj.getfeedToken()
                              print ("this is my feed token" ,feedToken )

                              #fetch User Profile
                              userProfile= obj.getProfile(refreshToken)

                              now you will have your feed token with you
                              ##########################################################
                              now use below piece of code to use web socket use feed token which you will get from above code.

                              from smartapi import WebSocket

                              FEED_TOKEN= ""
                              CLIENT_CODE=""

                              token= "nse_cm|2885&nse_cm|1594&nse_cm|11536"

                              task="mw" #'mw'|'sfi'|'dp'

                              ss = WebSocket(FEED_TOKEN, CLIENT_CODE)

                              def on_tick(ws, tick):
                              print("Ticks: {}".format(tick))

                              def on_connect(ws, response):
                              ws.send_request(token,task)

                              def on_close(ws, code, reason):
                              ws.stop()

                              Assign the callbacks.

                              ss.on_ticks = on_tick
                              ss.on_connect = on_connect
                              ss.on_close = on_close

                              ss.connect( )

                              1 Reply Last reply Reply Quote 0
                              • A
                                ashmita_ @Surya 1 last edited by ashmita_

                                @rajanprabu Sir it's showing like this____
                                gfgfg.png
                                my code is____

                                import pandas as pd
                                
                                df = pd.read_json('http://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json')
                                print(df)
                                
                                S 1 Reply Last reply Reply Quote 0
                                • S
                                  Surya 1 @ashmita_ last edited by

                                  @ashmita_

                                  IDE may not be showing all entries of the dataframe. If you are looking for RELAINCE-EQ then

                                  df.loc[df['symbol'] == "RELIANCE-EQ"] 
                                  
                                  
                                       token       symbol      name expiry  strike  lotsize instrumenttype exch_seg  tick_size
                                  3514  2885  RELIANCE-EQ  RELIANCE             -1        1                     NSE          5
                                  

                                  all entries which match RELIANCE

                                  df.loc[df['symbol'].str.contains("RELIANCE")]                                                                                                                               
                                  
                                          token                 symbol        name     expiry  strike  lotsize instrumenttype exch_seg  tick_size
                                  2513    18974          RELIANCEPP-E1  RELIANCEPP                 -1        1                     NSE          5
                                  2578    12713            RELIANCE-BL    RELIANCE                 -1        1                     NSE          5
                                  3280    22319          RELIANCEPP-BL  RELIANCEPP                 -1        1                     NSE          5
                                  3514     2885            RELIANCE-EQ    RELIANCE                 -1        1                     NSE          5
                                  11238  500325               RELIANCE    RELIANCE                 -1        1                     BSE          5
                                  ...       ...                    ...         ...        ...     ...      ...            ...      ...        ...
                                  71420  120461  RELIANCE27MAY212540CE    RELIANCE  27MAY2021  254000      250         OPTSTK      NFO          5
                                  71553  112789  RELIANCE29APR211960PE    RELIANCE  29APR2021  196000      250         OPTSTK      NFO          5
                                  71559  105028  RELIANCE25MAR211980PE    RELIANCE  25MAR2021  198000      250         OPTSTK      NFO          5
                                  71742  105005  RELIANCE25MAR211760CE    RELIANCE  25MAR2021  176000      250         OPTSTK      NFO          5
                                  71928  112813  RELIANCE29APR212200PE    RELIANCE  29APR2021  220000      250         OPTSTK      NFO          5
                                  
                                  [421 rows x 9 columns]
                                  
                                  R 1 Reply Last reply Reply Quote 0
                                  • R
                                    rjamitsharma01 @Surya 1 last edited by

                                    @admin is there any way to do this in java

                                    A 1 Reply Last reply Reply Quote 0
                                    • A
                                      admin @rjamitsharma01 last edited by

                                      Hi @rjamitsharma01 You need to find a feasible way to do it in java its a one time process let me text you in pc.

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post